public void Tick() { while (Packets.Count > 0) { IPacket oPacket = Packets.Dequeue(); IProtocolProcess protocolProcess = null; if (ProtocolTable.TryGetValue(oPacket.PacketID, out protocolProcess)) { try { protocolProcess.ToProcess(oPacket); } catch (Exception e) { Console.WriteLine(e); } } } }
public bool Process(AsyncSocketUserToken oUserToken, byte[] bytes, int offset, int count) { if (count < sizeof(int)) { return(false); // 判断包长度是否小于一个命令的长度 } int oPacketID = BitConverter.ToInt32(bytes, offset); // 获取命令ID IProtocolProcess oProtocol = null; if (ProtocolTable.TryGetValue(oPacketID, out oProtocol)) { IPacket oPacket = null; if (PacketTable.TryGetValue(oPacketID, out oPacket)) { IPacketCreate oIPacket = oPacket as IPacketCreate; IPacket oNewPacket = oIPacket.New() as IPacket; try { DynamicBuffer buffer = new DynamicBuffer(bytes, offset + sizeof(int), bytes.Length - offset - sizeof(int)); oNewPacket.Deserialization(buffer); oProtocol.ToProcess(oUserToken, oNewPacket); } catch (Exception e) { App.Logger.Error(e.Message + "\n" + e.StackTrace); return(false); } return(true); } else { return(false); } } return(false); }