Example #1
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);
 }
Example #2
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);
        }
Example #3
0
 private int calculateHeadSize(TalkerHead head)
 {
     return(100);
 }
        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);
        }
Example #5
0
        public ApolloMessage UnpackResponseData(byte[] data, int realSize)
        {
            int        num2;
            TalkerHead head = new TalkerHead();
            int        used = 0;

            TdrError.ErrorType type = head.unpackTLV(ref data, realSize, ref used);
            if (type != TdrError.ErrorType.TDR_NO_ERROR)
            {
                ADebug.LogError("UnpackResponseData head.unpack error:" + type);
                return(null);
            }
            TalkerCommand.CommandDomain bDomain = (TalkerCommand.CommandDomain)head.bDomain;
            TalkerCommand command = null;

            switch (((CMD_FMT)head.bCmdFmt))
            {
            case CMD_FMT.CMD_FMT_INT:
                if (head.stCommand != null)
                {
                    command = new TalkerCommand(bDomain, (uint)head.stCommand.iIntCmd);
                }
                goto Label_0103;

            case CMD_FMT.CMD_FMT_NIL:
                command = new TalkerCommand(bDomain, TalkerCommand.CommandValueType.Raw);
                goto Label_0103;

            default:
                if ((head.stCommand == null) || (head.stCommand.szStrCmd == null))
                {
                    goto Label_0103;
                }
                num2 = 0;
                for (int i = 0; i < head.stCommand.szStrCmd.Length; i++)
                {
                    num2 = i;
                    if (head.stCommand.szStrCmd[i] == 0)
                    {
                        break;
                    }
                }
                break;
            }
            string str = Encoding.UTF8.GetString(head.stCommand.szStrCmd, 0, num2);

            command = new TalkerCommand(bDomain, str);
Label_0103:
            if (command == null)
            {
                ADebug.LogError("With command is null");
                return(null);
            }
            ApolloMessage message = null;

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