private void OnMessageSend(ServiceDesc desc) { //lock (mWaitingMessages) { mWaitingMessages.Enqueue(new MessageSentEvent(this, desc)); } }
private void ProcessReplyMessage(ServiceDesc desc) { OutMessage outMessage; if (!mDispatcher.TryGetValue(desc.PacketId, out outMessage)) { return; } //Logger.Warn("continue: " + desc.FuncId); mDispatcher.Remove(desc.PacketId); try { outMessage.SetResponse(desc.Error, desc.Data); } catch (Exception ex) { Logger.Error(ex.ToString()); if (outMessage.ErrorCode == 0) { // 如果服务器没有报错,修改错误码,因为网络包无法解析 outMessage.ErrorCode = 1004; } } OutMessage.Delay += Time.fixedTime - outMessage.mStartTime; c++; return; }
public MessageRecievedEvent(object sender, ServiceDesc msg, object data) { Type = EventType.MessageRecieved; Sender = sender; Message = msg; Data = data; }
public void SendMessage(ServiceDesc protobufMessage) { if (mClient == null) { return; } mClient.SendMessage(protobufMessage); }
public OutMessage(IAgentBase agent, ServiceType serviceType, uint funcId) { mAgent = agent; mMessage = new ServiceDesc(); mMessage.ServiceType = (int)serviceType; mMessage.FuncId = funcId; mMessage.Type = (int)MessageType.CS; mMessage.PacketId = GetUniquePacketId(); }
public void SendMessage(ServiceDesc message) { // When we want to close all these, just stop to do anything more. if (closing) { return; } mMessageQueue.Enqueue(message); }
public static ServiceDesc Deserialize(Stream s) { ServiceDesc desc = new ServiceDesc(); byte[] buffer = new byte[9]; s.Read(buffer, 0, 1); byte mask = buffer[0]; s.Read(buffer, 0, 1); desc._Type = buffer[0]; if ((mask & 1) != 0) { s.Read(buffer, 0, 1); desc._ServiceType = buffer[0]; } if ((mask & 2) != 0) { s.Read(buffer, 0, 4); desc._FuncId = (uint)SerializerUtility.ReadInt(buffer); } if ((mask & 4) != 0) { s.Read(buffer, 0, 4); desc._PacketId = (uint)SerializerUtility.ReadInt(buffer); } if ((mask & 8) != 0) { s.Read(buffer, 0, 2); desc._Error = (uint)SerializerUtility.ReadShort(buffer); } if ((mask & 16) != 0) { desc._ClientId = (ulong)SerializerUtility.ReadLongOpt(s, buffer); } if ((mask & 32) != 0) { desc._CharacterId = (ulong)SerializerUtility.ReadLongOpt(s, buffer); } if ((mask & 64) != 0) { s.Read(buffer, 0, 2); var length = SerializerUtility.ReadShort(buffer); ulong l; if (desc._Routing == null) { desc._Routing = new List <ulong>(); } for (int i = 0; i < length; i++) { l = (ulong)SerializerUtility.ReadLongOpt(s, buffer); desc._Routing.Add(l); } } if ((mask & 128) != 0) { s.Read(buffer, 0, 4); var length = SerializerUtility.ReadInt(buffer); desc.Data = new byte[length]; s.Read(desc.Data, 0, length); } else { desc._Data = sEmptyByteArray; } return(desc); }
public static void Serialize(Stream s, ServiceDesc desc) { if (desc == null) { return; } if (!s.CanWrite || !s.CanSeek) { return; } byte[] buffer = new byte[9]; var pos = s.Position; s.WriteByte(0); byte mask = 0; buffer[0] = (byte)desc._Type; s.Write(buffer, 0, 1); if (desc._ServiceType != default(int)) { mask |= 1; buffer[0] = (byte)desc._ServiceType; s.Write(buffer, 0, 1); } if (desc._FuncId != default(int)) { mask |= 2; SerializerUtility.WriteInt(buffer, (int)desc._FuncId); s.Write(buffer, 0, 4); } if (desc._PacketId != default(int)) { mask |= 4; SerializerUtility.WriteInt(buffer, (int)desc._PacketId); s.Write(buffer, 0, 4); } if (desc._Error != default(int)) { mask |= 8; SerializerUtility.WriteShort(buffer, (short)desc._Error); s.Write(buffer, 0, 2); } if (desc._ClientId != default(ulong)) { mask |= 16; SerializerUtility.WriteLongOpt(s, buffer, (long)desc._ClientId); } if (desc._CharacterId != default(ulong)) { mask |= 32; SerializerUtility.WriteLongOpt(s, buffer, (long)desc._CharacterId); } if (desc._Routing != null && desc._Routing.Count != 0) { mask |= 64; SerializerUtility.WriteShort(buffer, (short)desc._Routing.Count); s.Write(buffer, 0, 2); { var __list1 = desc._Routing; var __listCount1 = __list1.Count; for (int __i1 = 0; __i1 < __listCount1; ++__i1) { var r = __list1[__i1]; { SerializerUtility.WriteLongOpt(s, buffer, (long)r); } } } } if (desc.Data != null && desc.Data.Length != 0) { mask |= 128; SerializerUtility.WriteInt(buffer, desc.Data.Length); s.Write(buffer, 0, 4); s.Write(desc.Data, 0, desc.Data.Length); } buffer[0] = mask; var last = s.Position; s.Seek(pos, SeekOrigin.Begin); s.Write(buffer, 0, 1); s.Seek(last, SeekOrigin.Begin); }
public static ServiceDesc Deserialize(Stream s) { return(ServiceDesc.Deserialize(s)); }
public static void Serialize(Stream s, ServiceDesc desc) { ServiceDesc.Serialize(s, desc); }
private void MessageReceived(ServiceDesc desc) { try { if (desc.Type == (int)MessageType.SC) { //lock (mWaitingMessages) { mWaitingMessages.Enqueue(new PublishMessageRecievedEvent(null, desc, GetPublishData((ServiceType)desc.ServiceType, desc.FuncId, desc.Data))); } return; } else if (desc.Type == (int)MessageType.Sync) { //lock (mWaitingMessages) { using (var ms = new MemoryStream(desc.Data, false)) { mWaitingMessages.Enqueue(new SyncEvent(null, Serializer.Deserialize <SyncData>(ms), (ServiceType)desc.ServiceType)); } } return; } else if (desc.Type == (int)MessageType.CS) { //lock (mWaitingMessages) { mWaitingMessages.Enqueue(new MessageRecievedEvent(null, desc, null)); } return; } // // 服务器发给客户端重连用的key // else if (desc.Type == 100)//(int)MessageType.Authorize // { // if (desc.ServiceType == 0) // { // ClientId = desc.ClientId; // Key = desc.CharacterId; // } // else // { // if (desc.Error == 0) // { // ReconnectSuccess = true; // mConnectedNotified = true; // } // else // { // ReconnectSuccess = false; // mConnectedNotified = true; // } // } // } else if (desc.Type == (int)MessageType.Ping) { return; } else { Logger.Error("Unknow message type."); } } catch (Exception ex) { Logger.Error(ex.ToString()); } }
public SendMessageFailedEvent(object sender, ServiceDesc msg) { Type = EventType.SendMessageFailed; Sender = sender; Message = msg; }
public MessageSentEvent(object sender, ServiceDesc msg) { Type = EventType.MessageSent; Sender = sender; Message = msg; }