public static void Register(PacketType packetID, int length, int minimumLength, OnPacketReceive onReceive) { _handlers[(byte)packetID] = new ClientPacketHandler(packetID, length, minimumLength, onReceive); }
private void ProcessReadQueue() { while (_recv.WaitOne()) { ByteQueue temp; lock (_queueLock) { temp = _receiveBufferQueue; _receiveBufferQueue = _readingBufferQueue; } _readingBufferQueue = temp; int length = _fragPackets.Size + _readingBufferQueue.Size; while (length > 0) { byte packetType; if (_fragPackets.Size > 0) { packetType = _fragPackets.GetPacketID(); } else { packetType = _readingBufferQueue.GetPacketID(); } /*using(StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true)) * { * byte[] enums = (byte[])Enum.GetValues(typeof (PacketType)); * int i = Array.BinarySearch(enums, 0, enums.Length, packetType); * * if(i != -1) * sw.WriteLine("{0} - Received packet {1}", DateTime.Now, ((PacketType)packetType)); * else * sw.WriteLine("{0} - Received packet {1}", DateTime.Now, packetType); * }*/ //result = HandlePacket(packetType, length); ClientPacketHandler handler = PacketHandlers.GetHandler((PacketType)packetType); if (handler == null) { using (StreamWriter sw = new StreamWriter(String.Format("unhandled_packets_{0}.log", _userName), true)) { byte[] unhandled = GetBufferToBeRead(length); sw.WriteLine("{0} - PacketType: {1}", DateTime.Now, unhandled[0]); sw.WriteLine(BitConverter.ToString(unhandled)); } length = 0; } else if (handler.Length == 0) { byte[] data = GetBufferToBeRead(length); /*using (StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true)) * {*/ if (length >= handler.MinimumLength) { PacketReader reader = new PacketReader(data, length); handler.OnReceive(this, reader); // If we failed it's because the packet isn't complete if (reader.Failed) { //sw.WriteLine("Failed Fragmented: {0}", BitConverter.ToString(data)); EnqueueFragment(data); length = 0; } else { //sw.WriteLine("Variable length: {0}", BitConverter.ToString(data, 0, reader.Index)); _readingBufferQueue.Enqueue(data, reader.Index, data.Length - reader.Index); length = _readingBufferQueue.Length; } } else { //sw.WriteLine("Fragmented variable: {0}", BitConverter.ToString(data)); // Not enough data EnqueueFragment(data); length = 0; } //} } else if (length >= handler.Length) { byte[] data = GetBufferToBeRead(handler.Length); /*using (StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true)) * { * sw.WriteLine("Fixed length: {0}", BitConverter.ToString(data)); * }*/ PacketReader reader = new PacketReader(data, handler.Length); handler.OnReceive(this, reader); // If we failed it's because the packet is wrong if (reader.Failed) { Dispose(); length = 0; } else { if (_fragPackets.Length > 0) { throw new Exception("Fragpackets must be empy here!"); } length = _readingBufferQueue.Length; } } else { /*using (StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true)) * { */ byte[] data = GetBufferToBeRead(length); //sw.WriteLine("Fragmented fixed: {0}", BitConverter.ToString(data)); EnqueueFragment(data); length = 0; //} } } } }