Beispiel #1
0
 public void Unregister(TalkerCommand command)
 {
     if (command != null)
     {
         ApolloMessageManager.Instance.Remove(command);
     }
 }
Beispiel #2
0
        internal ApolloResult SendNotice(TalkerCommand command, IPackable request)
        {
            ADebug.Log("SendNotice:" + request);
            ApolloMessage message = new ApolloMessage();

            return(this.Send(TalkerMessageType.Notice, command, request, message));
        }
Beispiel #3
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);
 }
 public void Remove(TalkerCommand command)
 {
     if (this.CmdMessageCollection.ContainsKey(command))
     {
         this.CmdMessageCollection.Remove(command);
     }
 }
Beispiel #5
0
 private ApolloResult SendReceipt <TResp>(TalkerCommand command, IPackable request, uint asyncFlag) where TResp : IUnpackable
 {
     return(this.Send(TalkerMessageType.Response, command, request, new ApolloMessage
     {
         AsyncFlag = asyncFlag
     }));
 }
Beispiel #6
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);
 }
Beispiel #7
0
 public bool Exist(TalkerCommand command)
 {
     if (command == null)
     {
         return(false);
     }
     return(this.CmdMessageCollection.ContainsKey(command));
 }
 public ApolloMessage Get(TalkerCommand command)
 {
     if (this.CmdMessageCollection.ContainsKey(command))
     {
         return(this.CmdMessageCollection[command]);
     }
     return(null);
 }
Beispiel #9
0
 public ApolloResult Register(TalkerCommand command, RawMessageHandler handler)
 {
     if (command != null && handler != null)
     {
         if (ApolloMessageManager.Instance.Exist(command))
         {
         }
         ApolloMessage apolloMessage = new ApolloMessage(command);
         apolloMessage.RawMessageHandler = handler;
         ApolloMessageManager.Instance.Add(apolloMessage);
         return(ApolloResult.Success);
     }
     return(ApolloResult.InvalidArgument);
 }
Beispiel #10
0
        public override bool Equals(object obj)
        {
            TalkerCommand command = obj as TalkerCommand;

            if ((command == null) || (command.Command == null))
            {
                return(false);
            }
            if (this.Domain != command.Domain)
            {
                return(false);
            }
            return(this.Command.Equals(command.Command));
        }
Beispiel #11
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);
 }
Beispiel #12
0
        public ApolloResult Register(TalkerCommand command, RawMessageHandler handler)
        {
            if ((command == null) || (handler == null))
            {
                return(ApolloResult.InvalidArgument);
            }
            if (ApolloMessageManager.Instance.Exist(command))
            {
            }
            ApolloMessage message = new ApolloMessage(command)
            {
                RawMessageHandler = handler
            };

            ApolloMessageManager.Instance.Add(message);
            return(ApolloResult.Success);
        }
Beispiel #13
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);
        }
Beispiel #14
0
 public ApolloResult Register <TResp>(TalkerCommand command, TalkerMessageWithoutReceiptHandler <TResp> handler) where TResp : IUnpackable
 {
     if (command != null && handler != null)
     {
         ADebug.Log("Register:" + command);
         if (ApolloMessageManager.Instance.Exist(command))
         {
         }
         ApolloMessage apolloMessage = new ApolloMessage(command);
         apolloMessage.RespType = typeof(TResp);
         apolloMessage.HandlerWithoutReceipt = delegate(IUnpackable resp)
         {
             handler((TResp)((object)resp));
         };
         ApolloMessageManager.Instance.Add(apolloMessage);
         return(ApolloResult.Success);
     }
     return(ApolloResult.InvalidArgument);
 }
Beispiel #15
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));
        }
Beispiel #16
0
 internal ApolloResult Register <TResp, TReceipt>(TalkerCommand command, TalkerMessageWithReceiptHandler <TResp, TReceipt> handler) where TResp : IUnpackable where TReceipt : IPackable
 {
     if (command != null && handler != null)
     {
         if (ApolloMessageManager.Instance.Exist(command))
         {
         }
         ApolloMessage apolloMessage = new ApolloMessage(command);
         apolloMessage.RespType           = typeof(TResp);
         apolloMessage.ReceiptType        = typeof(TReceipt);
         apolloMessage.Talker             = this;
         apolloMessage.HandlerWithReceipt = delegate(IUnpackable resp, ref IPackable receipt)
         {
             TReceipt tReceipt = default(TReceipt);
             handler((TResp)((object)resp), ref tReceipt);
             receipt = tReceipt;
             ADebug.Log("Register receipt:" + (receipt != null));
         };
         ApolloMessageManager.Instance.Add(apolloMessage);
         return(ApolloResult.Success);
     }
     return(ApolloResult.InvalidArgument);
 }
Beispiel #17
0
        public override bool Equals(object obj)
        {
            TalkerCommand talkerCommand = obj as TalkerCommand;

            return(talkerCommand != null && talkerCommand.Command != null && this.Domain == talkerCommand.Domain && this.Command.Equals(talkerCommand.Command));
        }
Beispiel #18
0
 public ApolloResult Register <TResp>(TalkerCommand command, TalkerMessageWithoutReceiptHandler <TResp> handler) where TResp : IUnpackable
 {
 public bool Exist(TalkerCommand command)
 {
     return(command != null && this.CmdMessageCollection.ContainsKey(command));
 }
        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);
        }
Beispiel #21
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);
        }
Beispiel #22
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);
        }
Beispiel #23
0
 public ApolloMessage(TalkerCommand command)
 {
     this.IsRequest = false;
     this.Command   = command;
 }
Beispiel #24
0
 public ApolloResult Send <TResp>(TalkerCommand command, IPackable request, TalkerMessageHandler <TResp> handler, object context, float timeout) where TResp : IUnpackable
 {
     return(this.Send <TResp>(TalkerMessageType.Request, command, request, handler, context, timeout));
 }