public void InitParser() { Packet1 packet = new Packet1(); _packetOperate = packet; _protocolProcess = packet; }
private void AssemblyPacket() { List <Type> typeList = new List <Type>(); Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i < assemblies.Length; i++) { typeList.AddRange(assemblies[i].GetTypes()); } Type[] types = typeList.ToArray(); for (int i = 0; i < types.Length; i++) { Type type = types[i]; object[] objs = type.GetCustomAttributes(typeof(ProtocolAttribute), false); if (objs.Length > 0) { // 如果包含包属性,则创建对象 object obj = Activator.CreateInstance(type); ProtocolAttribute oAttr = objs[0] as ProtocolAttribute; if (oAttr.Type == ProcessType.Receive) { IPacket oPacket = obj as IPacket; PacketTable.Add(oAttr.ID, oPacket); } else if (oAttr.Type == ProcessType.Process) { IProtocolProcess oProtocol = obj as IProtocolProcess; ProtocolTable.Add(oAttr.ID, oProtocol); } } } }
private void AssemblyPacket() { Type[] types = ZCommUtil.GetTypes(); for (int i = 0; i < types.Length; i++) { Type type = types[i]; object[] objs = type.GetCustomAttributes(typeof(ProtocolAttribute), false); if (objs.Length > 0) { // 如果包含包属性,则创建对象 object obj = Activator.CreateInstance(type); ProtocolAttribute oAttr = objs[0] as ProtocolAttribute; if (oAttr.Type == ProcessType.Receive) { IPacket oPacket = obj as IPacket; PacketTable.Add(oAttr.ID, oPacket); } else if (oAttr.Type == ProcessType.Process) { IProtocolProcess oProtocol = obj as IProtocolProcess; ProtocolTable.Add(oAttr.ID, oProtocol); } } } }
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); }
public void SetParser(AbstractParsePacket packet) { _packetOperate = packet; _protocolProcess = packet; }