示例#1
0
 public ApolloResult Send(TalkerMessageType type, TalkerCommand command, byte[] bodyData, int usedSize, ApolloMessage message)
 {
     if (command == null || bodyData == null || usedSize <= 0)
     {
         return(ApolloResult.InvalidArgument);
     }
     byte[] array = null;
     if (message == null)
     {
         message = new ApolloMessage();
     }
     if (message.PackRequestData(command, bodyData, usedSize, out array, type))
     {
         ADebug.Log(string.Concat(new object[]
         {
             "Sending:",
             command,
             " data size:",
             array.Length
         }));
         return(this.connector.WriteData(array, -1));
     }
     ADebug.LogError("Send: " + command + " msg.PackRequestData error");
     return(ApolloResult.InnerError);
 }
示例#2
0
 public ApolloResult Send(TalkerMessageType type, TalkerCommand command, IPackable request, ApolloMessage message)
 {
     if (command == null || request == null)
     {
         return(ApolloResult.InvalidArgument);
     }
     byte[] array = null;
     if (message == null)
     {
         message = new ApolloMessage();
     }
     if (message.PackRequestData(command, request, out array, type))
     {
         ADebug.Log(string.Concat(new object[]
         {
             "Sending:",
             command,
             " data size:",
             array.Length
         }));
         ApolloResult apolloResult = this.connector.WriteData(array, -1);
         if (apolloResult == ApolloResult.Success && message.Handler != null)
         {
             ApolloMessageManager.Instance.SeqMessageCollection.Add(message.SeqNum, message);
         }
         return(apolloResult);
     }
     ADebug.LogError("Send: " + command + " msg.PackRequestDat error");
     return(ApolloResult.InnerError);
 }
示例#3
0
 private void setFlag(ref byte flag, TalkerMessageType type, TalkerCommand.CommandValueType commandType)
 {
     flag  = 0;
     flag |= ((byte)type & 7);
     if (commandType == TalkerCommand.CommandValueType.Raw)
     {
         flag |= 16;
     }
 }
示例#4
0
 private void setFlag(ref byte flag, TalkerMessageType type, TalkerCommand.CommandValueType commandType)
 {
     flag = 0;
     flag = (byte)(flag | ((byte)(((byte)type) & 7)));
     if (commandType == TalkerCommand.CommandValueType.Raw)
     {
         flag = (byte)(flag | 0x10);
     }
 }
示例#5
0
        public ApolloResult Send <TResp>(TalkerMessageType type, TalkerCommand command, IPackable request, TalkerMessageHandler <TResp> handler, object context, float timeout) where TResp : IUnpackable
        {
            ApolloMessage apolloMessage = new ApolloMessage();

            apolloMessage.RespType = typeof(TResp);
            apolloMessage.Context  = context;
            apolloMessage.Life     = timeout;
            if (handler != null)
            {
                apolloMessage.Handler = delegate(object req, TalkerEventArgs loginInfo)
                {
                    if (handler != null)
                    {
                        TalkerEventArgs <TResp> talkerEventArgs = new TalkerEventArgs <TResp>(loginInfo.Result, loginInfo.ErrorMessage);
                        talkerEventArgs.Response = (TResp)((object)loginInfo.Response);
                        talkerEventArgs.Context  = loginInfo.Context;
                        handler(req, talkerEventArgs);
                    }
                };
            }
            return(this.Send(type, command, request, apolloMessage));
        }
示例#6
0
        public bool PackRequestData(TalkerCommand command, byte[] body, int usedBodySize, out byte[] data, TalkerMessageType type)
        {
            if (command == null || command.Command == null)
            {
                throw new Exception("Invalid Argument!");
            }
            data = null;
            if (body == null || usedBodySize <= 0)
            {
                ADebug.Log("PackRequestData request.pack error");
                return(false);
            }
            TalkerHead talkerHead = new TalkerHead();

            this.setFlag(ref talkerHead.bFlag, type, command.Command.Type);
            ADebug.Log(string.Concat(new object[]
            {
                "PackRequestData head flag:",
                talkerHead.bFlag,
                " commandType:",
                command.Command.Type
            }));
            this.Command                 = command;
            talkerHead.bDomain           = (byte)command.Domain;
            talkerHead.bCmdFmt           = (byte)command.Command.Type;
            talkerHead.stCommand.iIntCmd = (int)command.Command.IntegerValue;
            if (command.Command.StringValue != null)
            {
                talkerHead.stCommand.szStrCmd = Encoding.get_UTF8().GetBytes(command.Command.StringValue);
            }
            if (type == TalkerMessageType.Response)
            {
                talkerHead.dwAsync = this.AsyncFlag;
            }
            else
            {
                talkerHead.dwAsync = this.SeqNum;
            }
            ADebug.Log(string.Concat(new object[]
            {
                "PackRequestData cmd: ",
                this.Command.ToString(),
                " type:",
                type,
                " async:",
                talkerHead.dwAsync
            }));
            int num = this.calculateHeadSize(talkerHead);

            byte[] array = new byte[num];
            int    num2  = 0;

            TdrError.ErrorType errorType = talkerHead.packTLV(ref array, array.Length, ref num2, true);
            if (errorType != TdrError.ErrorType.TDR_NO_ERROR)
            {
                ADebug.Log(string.Concat(new object[]
                {
                    "PackRequestData head.pack error:",
                    errorType,
                    " usedBodySize:",
                    usedBodySize,
                    " usedHeadSize:",
                    num2,
                    " headBufflen:",
                    array.Length
                }));
                return(false);
            }
            int num3 = usedBodySize + num2;

            data = new byte[num3];
            Array.Copy(array, data, num2);
            Array.Copy(body, 0, data, num2, usedBodySize);
            this.Request = null;
            return(true);
        }
示例#7
0
        public bool PackRequestData(TalkerCommand command, IPackable request, out byte[] data, TalkerMessageType type)
        {
            if (command == null || command.Command == null)
            {
                throw new Exception("Invalid Argument!");
            }
            data = null;
            byte[] array        = new byte[ApolloCommon.ApolloInfo.MaxMessageBufferSize];
            int    usedBodySize = 0;

            if (request.packTLV(ref array, array.Length, ref usedBodySize, true) == TdrError.ErrorType.TDR_NO_ERROR && this.PackRequestData(command, array, usedBodySize, out data, type))
            {
                this.Request = request;
                return(true);
            }
            ADebug.Log("PackRequestData request.pack error");
            return(false);
        }
示例#8
0
 public bool PackRequestData(TalkerCommand command, byte[] body, int usedBodySize, out byte[] data, TalkerMessageType type)
 {
     if ((command == null) || (command.Command == null))
     {
         throw new Exception("Invalid Argument!");
     }
     data = null;
     if ((body != null) && (usedBodySize > 0))
     {
         TalkerHead head = new TalkerHead();
         this.setFlag(ref head.bFlag, type, command.Command.Type);
         ADebug.Log(string.Concat(new object[] { "PackRequestData head flag:", head.bFlag, " commandType:", command.Command.Type }));
         this.Command           = command;
         head.bDomain           = (byte)command.Domain;
         head.bCmdFmt           = (byte)command.Command.Type;
         head.stCommand.iIntCmd = (int)command.Command.IntegerValue;
         if (command.Command.StringValue != null)
         {
             head.stCommand.szStrCmd = Encoding.UTF8.GetBytes(command.Command.StringValue);
         }
         if (type == TalkerMessageType.Response)
         {
             head.dwAsync = this.AsyncFlag;
         }
         else
         {
             head.dwAsync = this.SeqNum;
         }
         ADebug.Log(string.Concat(new object[] { "PackRequestData cmd: ", this.Command.ToString(), " type:", type, " async:", head.dwAsync }));
         byte[]             buffer = new byte[this.calculateHeadSize(head)];
         int                used   = 0;
         TdrError.ErrorType type2  = head.packTLV(ref buffer, buffer.Length, ref used, true);
         if (type2 != TdrError.ErrorType.TDR_NO_ERROR)
         {
             ADebug.Log(string.Concat(new object[] { "PackRequestData head.pack error:", type2, " usedBodySize:", usedBodySize, " usedHeadSize:", used, " headBufflen:", buffer.Length }));
             return(false);
         }
         int num3 = usedBodySize + used;
         data = new byte[num3];
         Array.Copy(buffer, data, used);
         Array.Copy(body, 0, data, used, usedBodySize);
         this.Request = null;
         return(true);
     }
     ADebug.Log("PackRequestData request.pack error");
     return(false);
 }
        public ApolloMessage UnpackResponseData(byte[] data, int realSize)
        {
            TalkerHead talkerHead = new TalkerHead();
            int        num        = 0;

            TdrError.ErrorType errorType = talkerHead.unpackTLV(ref data, realSize, ref num);
            if (errorType != TdrError.ErrorType.TDR_NO_ERROR)
            {
                ADebug.LogError("UnpackResponseData head.unpack error:" + errorType);
                return(null);
            }
            TalkerCommand.CommandDomain bDomain = (TalkerCommand.CommandDomain)talkerHead.bDomain;
            TalkerCommand talkerCommand         = null;
            CMD_FMT       bCmdFmt = (CMD_FMT)talkerHead.bCmdFmt;

            if (bCmdFmt == CMD_FMT.CMD_FMT_INT)
            {
                if (talkerHead.stCommand != null)
                {
                    talkerCommand = new TalkerCommand(bDomain, (uint)talkerHead.stCommand.iIntCmd);
                }
            }
            else if (bCmdFmt == CMD_FMT.CMD_FMT_NIL)
            {
                talkerCommand = new TalkerCommand(bDomain, TalkerCommand.CommandValueType.Raw);
            }
            else if (talkerHead.stCommand != null && talkerHead.stCommand.szStrCmd != null)
            {
                int num2 = 0;
                for (int i = 0; i < talkerHead.stCommand.szStrCmd.Length; i++)
                {
                    num2 = i;
                    if (talkerHead.stCommand.szStrCmd[i] == 0)
                    {
                        break;
                    }
                }
                string @string = Encoding.get_UTF8().GetString(talkerHead.stCommand.szStrCmd, 0, num2);
                talkerCommand = new TalkerCommand(bDomain, @string);
            }
            if (talkerCommand == null)
            {
                ADebug.LogError("With command is null");
                return(null);
            }
            ApolloMessage     apolloMessage = null;
            TalkerMessageType messageType   = ApolloMessage.GetMessageType((int)talkerHead.bFlag);

            if (messageType == TalkerMessageType.Response)
            {
                if (ApolloMessageManager.Instance.SeqMessageCollection.ContainsKey(talkerHead.dwAsync))
                {
                    apolloMessage = ApolloMessageManager.Instance.SeqMessageCollection[talkerHead.dwAsync];
                }
                if (apolloMessage != null)
                {
                    apolloMessage.AsyncFlag = talkerHead.dwAsync;
                    if (apolloMessage.IsRequest && talkerHead.dwAsync != apolloMessage.SeqNum)
                    {
                        if (talkerHead.dwAsync != apolloMessage.SeqNum)
                        {
                            ADebug.Log(string.Format("UnpackResponseData error: if(head.dwSeqNum({0}) != seqNum({1})", talkerHead.dwAsync, apolloMessage.SeqNum));
                        }
                        else
                        {
                            ADebug.Log(string.Concat(new object[]
                            {
                                "UnpackResponseData error compare result:",
                                talkerCommand.Equals(apolloMessage.Command),
                                " msg.command:",
                                apolloMessage.Command,
                                " cmd:",
                                talkerCommand
                            }));
                        }
                        return(null);
                    }
                }
            }
            else
            {
                apolloMessage = this.Get(talkerCommand);
                if (apolloMessage != null)
                {
                    ADebug.Log(string.Concat(new object[]
                    {
                        "cmd:",
                        talkerCommand,
                        " msg receipt handler:",
                        apolloMessage.HandlerWithReceipt != null,
                        " without receipt handler:",
                        apolloMessage.HandlerWithoutReceipt != null
                    }));
                }
            }
            if (apolloMessage == null)
            {
                ADebug.LogError(string.Concat(new object[]
                {
                    "UnpackResponseData error: msg == null while seq:",
                    talkerHead.dwAsync,
                    " cmd:",
                    talkerCommand,
                    " type:",
                    ApolloMessage.GetMessageType((int)talkerHead.bFlag)
                }));
                return(null);
            }
            ADebug.Log(string.Concat(new object[]
            {
                "UnpackResponseData msg.Command:",
                apolloMessage.Command,
                " type:",
                ApolloMessage.GetMessageType((int)talkerHead.bFlag)
            }));
            if (realSize < num)
            {
                ADebug.LogError(string.Format("realSize{0} < usedSize({1})", realSize, num));
                return(null);
            }
            byte[] array = new byte[realSize - num];
            Array.Copy(data, num, array, 0, array.Length);
            if (apolloMessage.RespType != null)
            {
                apolloMessage.Response = (Activator.CreateInstance(apolloMessage.RespType) as IUnpackable);
                if (apolloMessage.Response != null)
                {
                    errorType = apolloMessage.Response.unpackTLV(ref array, array.Length, ref num);
                    if (errorType != TdrError.ErrorType.TDR_NO_ERROR)
                    {
                        ADebug.Log("UnpackResponseData resp.unpack error:" + errorType);
                        return(null);
                    }
                    return(apolloMessage);
                }
            }
            else
            {
                apolloMessage.RawData = array;
            }
            return(apolloMessage);
        }