Exemplo n.º 1
0
        private void TryReadPackets()
        {
            if (bytesToSkip > 0)
            {
                int skipped = Math.Min(bytesToSkip, (int)memory.Length);
                memory.Position += skipped;
                bytesToSkip     -= skipped;
            }

            while (memory.Length - memory.Position > 2)
            {
                byte[] tmp = new byte[2];

                memory.Read(tmp, 0, 2);
                ushort cmd = BitConverter.ToUInt16(tmp, 0);

                if (!packetSize.ContainsKey(cmd))
                {
                    if (InvalidPacket != null)
                    {
                        InvalidPacket();
                    }

                    memory.Position -= 2;

                    break;
                }
                else
                {
                    int  size    = packetSize[cmd].Size;
                    bool isFixed = true;

                    if (size <= 0)
                    {
                        isFixed = false;

                        if (memory.Length - memory.Position > 2)
                        {
                            memory.Read(tmp, 0, 2);
                            size = BitConverter.ToUInt16(tmp, 0);
                        }
                        else
                        {
                            memory.Position -= 4;

                            break;
                        }
                    }

                    byte[] data = new byte[size];
                    memory.Read(data, 0, size - (isFixed ? 2 : 4));

                    ConstructorInfo ci = packetSize[cmd].Type.GetConstructor(new Type[] { });
                    InPacket        p  = (InPacket)ci.Invoke(null);
                    Debug.Print("Packet " + p.ToString());

                    if (!p.Read(data))
                    {
                        if (InvalidPacket != null)
                        {
                            InvalidPacket();
                        }

                        break;
                    }

                    if (packetHooks.ContainsKey(cmd))
                    {
                        packetHooks[cmd].DynamicInvoke(cmd, size, p);
                    }

                    if (PacketReceived != null)
                    {
                        PacketReceived(cmd, size, p);
                    }
                }
            }

            if (memory.Length - memory.Position > 0)
            {
                MemoryStream ms = new MemoryStream();

                ms.Write(memory.GetBuffer(), (int)memory.Position, (int)memory.Length - (int)memory.Position);
                memory.Dispose();

                memory = ms;
            }
        }
Exemplo n.º 2
0
        protected override void Dispatch(byte[] buffer)
        {
            using (InPacket iPacket = new InPacket(buffer))
            {
                PacketHandler handler = mProcessor[iPacket.OperationCode];

                if (handler != null)
                {
                    handler(this, iPacket);
                }
                else
                {
                    Logger.Write(LogLevel.Warning, "[{0}] Unhandled packet from {1}: {2}", mProcessor.Label, this.Host, iPacket.ToString());
                }
            }
        }