public void TestLife(float deltaTime) { List <uint> list = new List <uint>(); foreach (uint num in this.seqMessageCollection.Keys) { ApolloMessage message = this.seqMessageCollection[num]; if (message != null) { message.Life -= deltaTime; if (message.Life <= 0f) { list.Add(num); if (message.Handler != null) { TalkerEventArgs e = new TalkerEventArgs(ApolloResult.Timeout) { Context = message.Context, Response = null }; message.Handler(message.Request, e); } } } } foreach (uint num2 in list) { this.seqMessageCollection.Remove(num2); } }
private void pushApolloMessage(ApolloMessage msg) { if (msg != null) { this.apolloMessageCollections.Add(msg); } }
private void onRecvedData() { ADebug.Log("onRecvedData OnDataRecvedProc"); while (true) { int num; byte[] buffer = null; if (this.connector.ReadData(out buffer, out num) != ApolloResult.Success) { return; } try { ApolloMessage message = ApolloMessageManager.Instance.UnpackResponseData(buffer, num); if (message != null) { ADebug.Log(string.Concat(new object[] { "Recved:", message.Command, " and resp is:", message.Response })); ADebug.Log(string.Concat(new object[] { "OnDataRecvedProc: apolloMessage.Handler != null?: ", message.Handler != null, " apolloMessage.HandlerWithReceipt != null?: ", message.HandlerWithReceipt != null, " apolloMessage.HandlerWithoutReceipt != null?: ", message.HandlerWithoutReceipt != null })); message.HandleMessage(); } else { ADebug.LogError("OnDataRecvedProc UnpackResponseData error"); } } catch (Exception exception) { ADebug.LogException(exception); } } }
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); }
private ApolloResult SendNotice(IPackable request) { ADebug.Log("SendNotice:" + request); ApolloMessage message = new ApolloMessage(); return(this.Send(TalkerMessageType.Notice, new TalkerCommand(TalkerCommand.CommandDomain.App, request), request, message)); }
internal ApolloResult SendNotice(TalkerCommand command, IPackable request) { ADebug.Log("SendNotice:" + request); ApolloMessage message = new ApolloMessage(); return(this.Send(TalkerMessageType.Notice, command, request, message)); }
internal ApolloResult SendReceipt <TResp>(IPackable request, uint asyncFlag) where TResp : IUnpackable { ApolloMessage apolloMessage = new ApolloMessage(); apolloMessage.AsyncFlag = asyncFlag; return(this.Send(TalkerMessageType.Response, new TalkerCommand(TalkerCommand.CommandDomain.App, request), request, apolloMessage)); }
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); }
public void RemoveMessage(ApolloMessage msg) { if (ApolloMessageManager.Instance.SeqMessageCollection.ContainsKey(msg.SeqNum)) { ApolloMessageManager.Instance.Remove(msg.SeqNum); } else if (ApolloMessageManager.Instance.Exist(msg.Command)) { ApolloMessageManager.Instance.Remove(msg.Command); } }
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); }
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); }
private void onRecvedData() { ADebug.Log("onRecvedData OnDataRecvedProc"); while (true) { byte[] data = null; int realSize; ApolloResult apolloResult = this.connector.ReadData(out data, out realSize); if (apolloResult != ApolloResult.Success) { break; } try { ApolloMessage apolloMessage = ApolloMessageManager.Instance.UnpackResponseData(data, realSize); if (apolloMessage != null) { ADebug.Log(string.Concat(new object[] { "Recved:", apolloMessage.Command, " and resp is:", apolloMessage.Response })); ADebug.Log(string.Concat(new object[] { "OnDataRecvedProc: apolloMessage.Handler != null?: ", apolloMessage.Handler != null, " apolloMessage.HandlerWithReceipt != null?: ", apolloMessage.HandlerWithReceipt != null, " apolloMessage.HandlerWithoutReceipt != null?: ", apolloMessage.HandlerWithoutReceipt != null })); apolloMessage.HandleMessage(); } else { ADebug.LogError("OnDataRecvedProc UnpackResponseData error"); } } catch (Exception exception) { ADebug.LogException(exception); } } }
public void Add(ApolloMessage message) { if (message != null) { if (message.IsRequest) { this.SeqMessageCollection.Add(message.SeqNum, message); } else if (this.CmdMessageCollection.ContainsKey(message.Command)) { this.CmdMessageCollection[message.Command] = message; } else { this.CmdMessageCollection.Add(message.Command, message); } } }
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); }
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)); }
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); }
private bool popAndHandleApolloMessage() { if (this.apolloMessageCollections.Count == 0) { return(false); } ADebug.Log("popAndHandleApolloMessage: " + this.apolloMessageCollections.Count); ApolloMessage next = this.apolloMessageCollections.Next; if (next != null) { if (next.Handler != null) { TalkerEventArgs e = new TalkerEventArgs(next.Response, next.Context); next.Handler(next.Request, e); } if (next.HandlerWithoutReceipt != null) { next.HandlerWithoutReceipt(next.Response); } if (next.HandlerWithReceipt != null) { IPackable receipt = null; next.HandlerWithReceipt(next.Response, ref receipt); if (receipt != null) { ADebug.Log("HandlerWithReceipt receipt:" + receipt); this.SendReceipt <NullResponse>(receipt, next.AsyncFlag); } } if (next.IsRequest) { ApolloMessageManager.Instance.RemoveMessage(next); } } return(true); }
public void TestLife(float deltaTime) { List <uint> list = new List <uint>(); using (Dictionary <uint, object> .KeyCollection.Enumerator enumerator = this.seqMessageCollection.Keys.GetEnumerator()) { while (enumerator.MoveNext()) { uint current = enumerator.get_Current(); ApolloMessage apolloMessage = this.seqMessageCollection[current]; if (apolloMessage != null) { apolloMessage.Life -= deltaTime; if (apolloMessage.Life <= 0f) { list.Add(current); if (apolloMessage.Handler != null) { TalkerEventArgs talkerEventArgs = new TalkerEventArgs(ApolloResult.Timeout); talkerEventArgs.Context = apolloMessage.Context; talkerEventArgs.Response = null; apolloMessage.Handler(apolloMessage.Request, talkerEventArgs); } } } } } using (List <uint> .Enumerator enumerator2 = list.GetEnumerator()) { while (enumerator2.MoveNext()) { uint current2 = enumerator2.get_Current(); this.seqMessageCollection.Remove(current2); } } }
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); }
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); }