示例#1
0
        protected void Initialize(int packetID, int length)
        {
            m_PacketID = packetID;
            m_Length   = length;

            if (m_Length > 0)
            {
                m_Stream = PacketWriter.CreateInstance(length);
                m_Stream.Write((byte)packetID);
            }

            PacketProfile prof = PacketProfile.GetOutgoingProfile((byte)packetID);

            if (prof != null)
            {
                prof.RegConstruct();
            }
        }
示例#2
0
 static PacketProfile()
 {
     OutgoingProfiles = new PacketProfile[0x100];
     IncomingProfiles = new PacketProfile[0x100];
 }
示例#3
0
        public void Send(Packet p)
        {
            if (m_Socket == null || m_BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            PacketProfile prof  = PacketProfile.GetOutgoingProfile((byte)p.PacketID);
            DateTime      start = (prof == null ? DateTime.MinValue : DateTime.Now);

            int length;

            byte[] buffer = p.Compile(m_CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                SendEnqueueResult enqueueResult;

                lock (m_SendQueue)
                    enqueueResult = (m_SendQueue.Enqueue(buffer, length));

                if (enqueueResult == SendEnqueueResult.Begin)
                {
                    int    sendLength = 0;
                    byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                    try
                    {
                        IAsyncResult res = m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null);
                        //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength );
                    }
                    catch                     // ( Exception ex )
                    {
                        //Console.WriteLine(ex);
                        Dispose(false);
                    }
                }
                else if (enqueueResult == SendEnqueueResult.Overflow)
                {
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Record(length, DateTime.Now - start);
                }
            }
            else
            {
                Dispose();
            }
        }
示例#4
0
        public void Send(Packet p)
        {
            if (m_Socket == null)
            {
                return;
            }

            PacketProfile prof  = PacketProfile.GetOutgoingProfile((byte)p.PacketID);
            DateTime      start = (prof == null ? DateTime.MinValue : DateTime.Now);

            byte[] buffer = p.Compile(m_CompressionEnabled);

            if (buffer != null)
            {
                if (buffer.Length <= 0)
                {
                    return;
                }

                int length = buffer.Length;

                if (m_Encoder != null)
                {
                    m_Encoder.EncodeOutgoingPacket(this, ref buffer, ref length);
                }

                bool shouldBegin = false;

                lock (m_SendQueue)
                    shouldBegin = (m_SendQueue.Enqueue(buffer, length));

                if (m_Connecting)
                {
                    shouldBegin = false;
                }

                if (shouldBegin)
                {
                    int    sendLength = 0;
                    byte[] sendBuffer = m_SendQueue.Peek(ref sendLength);

                    try
                    {
                        m_Socket.BeginSend(sendBuffer, 0, sendLength, SocketFlags.None, m_OnSend, null);
                        m_Sending = true;
                        //Console.WriteLine( "Send: {0}: Begin send of {1} bytes", this, sendLength );
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                        Dispose(false);
                    }
                }

                if (prof != null)
                {
                    prof.Record(length, DateTime.Now - start);
                }
            }
            else
            {
                Dispose();
            }
        }
示例#5
0
        public bool HandleReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return(true);
            }

            lock ( buffer )
            {
                int length = buffer.Length;

                if (!ns.Seeded)
                {
                    if (buffer.Length >= 4)
                    {
                        buffer.Dequeue(m_Peek, 0, 4);

                        int seed = (m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3];

                        if (seed == 0)
                        {
                            Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns);
                            ns.Dispose();
                            return(false);
                        }

                        ns.m_Seed = seed;
                        ns.Seeded = true;

                        length = buffer.Length;
                    }
                    else
                    {
                        return(true);
                    }
                }

                while (length > 0 && ns.Running)
                {
                    int packetID = buffer.GetPacketID();

                    if (!ns.SentFirstPacket && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 && packetID != 0x91 && packetID != 0xA4)
                    {
                        Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", ns);
                        ns.Dispose();
                        break;
                    }

                    PacketHandler handler = PacketHandlers.GetHandler(packetID);

                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        new PacketReader(data, length, false).Trace(ns);

                        break;
                    }

                    int packetLength = handler.Length;

                    if (packetLength <= 0)
                    {
                        if (length >= 3)
                        {
                            packetLength = buffer.GetPacketLength();

                            if (packetLength < 3)
                            {
                                ns.Dispose();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (length >= packetLength)
                    {
                        if (handler.Ingame && ns.Mobile == null)
                        {
                            Console.WriteLine("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", ns, packetID);
                            ns.Dispose();
                            break;
                        }
                        else if (handler.Ingame && ns.Mobile.Deleted)
                        {
                            ns.Dispose();
                            break;
                        }
                        else
                        {
                            ThrottlePacketCallback throttler = handler.ThrottleCallback;

                            if (throttler != null && !throttler(ns))
                            {
                                m_Throttled.Enqueue(ns);
                                return(false);
                            }

                            PacketProfile profile = PacketProfile.GetIncomingProfile(packetID);
                            DateTime      start   = (profile == null ? DateTime.MinValue : DateTime.Now);

                            byte[] packetBuffer;

                            if (BufferSize >= packetLength)
                            {
                                packetBuffer = m_Buffers.AcquireBuffer();
                            }
                            else
                            {
                                packetBuffer = new byte[packetLength];
                            }

                            packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                            PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);

                            handler.OnReceive(ns, r);
                            length = buffer.Length;

                            if (BufferSize >= packetLength)
                            {
                                m_Buffers.ReleaseBuffer(packetBuffer);
                            }

                            if (profile != null)
                            {
                                profile.Record(packetLength, DateTime.Now - start);
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(true);
        }
示例#6
0
        private void OnReceived(NetState state, ByteQueue buffer, out bool throttle)
        {
            throttle = false;
            GameClient client;

            if (!m_Clients.TryGetValue(state, out client))
            {
                Logger.Error("Inconsistent game server state: game client for {0} not found", state);
                return;
            }

            if (!client.Seeded)
            {
                if (buffer.GetPacketID() == 0xEF)
                {
                    client.Seeded = true;
                }
                else if (buffer.Length >= 4)
                {
                    byte[] peek = new byte[4];
                    buffer.Dequeue(peek, 0, 4);

                    int seed = (peek[0] << 24) | (peek[1] << 16) | (peek[2] << 8) | peek[3];

                    if (seed == 0)
                    {
                        Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", client);
                        client.Dispose();

                        return;
                    }

                    client.m_Seed = seed;
                    client.Seeded = true;
                }
                else
                {
                    return;                     // Need at least 4 bytes for the seed
                }
            }

            int length = buffer.Length;

            while (length > 0 && buffer.Length > 0)
            {
                int packetID = buffer.GetPacketID();

                if (!client.SentFirstPacket && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 && packetID != 0x91 && packetID != 0xA4 && packetID != 0xEF)
                {
                    Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", client);
                    client.Dispose();
                    return;
                }

                PacketHandler handler = m_Handlers.GetHandler(packetID);

                if (handler == null)
                {
                    byte[] data = new byte[length];
                    length = buffer.Dequeue(data, 0, length);

                    if (Environment.Logging)
                    {
                        PacketReader reader = PacketReader.CreateInstance(data, length, false);
                        reader.Trace(client);
                        PacketReader.ReleaseInstance(reader);
                    }

                    return;
                }

                int packetLength = handler.Length;

                if (packetLength == 0)
                {
                    // Dynamic length packet. Need at leaset 3 bytes (1 packet cmd + 2 length)

                    if (length >= 3)
                    {
                        packetLength = buffer.GetPacketLength();

                        if (packetLength < 3)
                        {
                            client.Dispose();
                            return;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (length >= packetLength)
                {
                    if (handler.Ingame && client.Mobile == null)
                    {
                        Console.WriteLine("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", client, packetID);
                        client.Dispose();
                        return;
                    }
                    else if (handler.Ingame && client.Mobile.Deleted)
                    {
                        client.Dispose();
                        return;
                    }
                    else
                    {
                        ThrottlePacketCallback throttler = handler.ThrottleCallback;

                        if (throttler != null && !throttler(client))
                        {
                            throttle = true;
                            return;
                        }

                        PacketProfile profile = PacketProfile.GetIncomingProfile(packetID);
                        DateTime      start   = (profile == null ? DateTime.MinValue : DateTime.Now);

                        byte[] packetBuffer;

                        if (BufferSize >= packetLength)
                        {
                            packetBuffer = m_Buffers.AcquireBuffer();
                        }
                        else
                        {
                            packetBuffer = new byte[packetLength];
                        }

                        packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                        PacketReader reader = PacketReader.CreateInstance(packetBuffer, packetLength, handler.Length != 0);

                        try
                        {
                            handler.OnReceive(client, reader);
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Exception disarmed in HandleReceive from {0}: {1}", client.Address, e);
                        }

                        PacketReader.ReleaseInstance(reader);

                        length = buffer.Length;

                        if (BufferSize >= packetLength)
                        {
                            m_Buffers.ReleaseBuffer(packetBuffer);
                        }

                        if (profile != null)
                        {
                            profile.Record(packetLength, DateTime.Now - start);
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }
示例#7
0
        private bool HandleReceive(NetState ns)
        {
            lock ( ns )
            {
                ByteQueue buffer = ns.Buffer;

                if (buffer == null)
                {
                    return(true);
                }

                int length = buffer.Length;

                if (!ns.Seeded)
                {
                    if (length >= 4)
                    {
                        buffer.Dequeue(m_Peek, 0, 4);

                        int seed = (m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3];

                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat("Login: {0}: Seed is 0x{1:X8}", ns, seed);
                        }

                        if (seed == 0)
                        {
                            log.WarnFormat("Login: {0}: Invalid client detected, disconnecting", ns);
                            ns.Dispose();
                            return(false);
                        }

                        ns.m_Seed = seed;
                        ns.Seeded = true;
                    }

                    return(true);
                }

                while (length > 0 && ns.Running)
                {
                    int packetID = buffer.GetPacketID();

                    if (!ns.SentFirstPacket && packetID != 0xF0 && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 && packetID != 0x91 && packetID != 0xA4 && packetID != 0xBF)
                    {
                        log.WarnFormat("Client: {0}: Encrypted client detected, disconnecting", ns);
                        ns.Dispose();
                        break;
                    }

                    PacketHandler handler = ns.GetHandler(packetID);

                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        new PacketReader(data, length, false).Trace(ns);

                        break;
                    }

                    int packetLength = handler.Length;

                    if (packetLength <= 0)
                    {
                        if (length >= 3)
                        {
                            packetLength = buffer.GetPacketLength();

                            if (packetLength < 3)
                            {
                                ns.Dispose();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (length >= packetLength)
                    {
                        if (handler.Ingame && ns.Mobile == null)
                        {
                            log.WarnFormat("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile",
                                           ns, packetID);
                            ns.Dispose();
                            break;
                        }
                        else if (handler.Ingame && ns.Mobile.Deleted)
                        {
                            ns.Dispose();
                            break;
                        }
                        else
                        {
                            ThrottlePacketCallback throttler = handler.ThrottleCallback;

                            if (throttler != null && !throttler(ns))
                            {
                                m_Throttled.Enqueue(ns);
                                return(false);
                            }

                            PacketProfile profile = PacketProfile.GetIncomingProfile(packetID);
                            DateTime      start   = (profile == null ? DateTime.MinValue : DateTime.UtcNow);

                            byte[] packetBuffer;

                            if (BufferSize >= packetLength)
                            {
                                packetBuffer = m_Buffers.AquireBuffer();
                            }
                            else
                            {
                                packetBuffer = new byte[packetLength];
                            }

                            packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                            PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);

                            try {
                                handler.OnReceive(ns, r);
                            } catch (Exception e) {
                                log.Fatal(String.Format("Exception disarmed in HandleReceive from {0}",
                                                        ns.Address), e);
                            }

                            length = buffer.Length;

                            if (BufferSize >= packetLength)
                            {
                                m_Buffers.ReleaseBuffer(packetBuffer);
                            }

                            if (profile != null)
                            {
                                profile.Record(packetLength, DateTime.UtcNow - start);
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(true);
        }
示例#8
0
        public static PacketProfile GetOutgoingProfile( int packetID )
        {
            if ( !Environment.Profiling )
                return null;

            PacketProfile prof = m_OutgoingProfiles[packetID];

            if ( prof == null )
                m_OutgoingProfiles[packetID] = prof = new PacketProfile( true );

            return prof;
        }
示例#9
0
        public static PacketProfile GetIncomingProfile( int packetID )
        {
            if ( !Environment.Profiling )
                return null;

            PacketProfile prof = m_IncomingProfiles[packetID];

            if ( prof == null )
                m_IncomingProfiles[packetID] = prof = new PacketProfile( false );

            return prof;
        }
示例#10
0
        public bool HandleReceive(NetState ns)
        {
            ByteQueue     queue1;
            int           num1;
            int           num2;
            int           num3;
            PacketHandler handler1;

            byte[] buffer1;
            int    num4;
            ThrottlePacketCallback callback1;
            PacketProfile          profile1;
            DateTime     time1;
            PacketReader reader1;
            NetState     state1 = ns;

            lock (ns)
            {
                queue1 = ns.Buffer;
                if (queue1 == null)
                {
                    return(true);
                }
                num1 = queue1.Length;
                if (ns.Seeded)
                {
                    goto Label_0250;
                }
                if (num1 >= 4)
                {
                    queue1.Dequeue(this.m_Peek, 0, 4);
                    num2 = ((((this.m_Peek[0] << 24) | (this.m_Peek[1] << 16)) | (this.m_Peek[2] << 8)) | this.m_Peek[3]);
                    if (num2 == 0)
                    {
                        Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns);
                        ns.Dispose();
                        return(false);
                    }
                    ns.m_Seed = num2;
                    ns.Seeded = true;
                }
                return(true);

Label_009D:
                queue1.Peek(this.m_Peek, 0, 1);
                num3 = this.m_Peek[0];
                if (((!ns.SentFirstPacket && (num3 != 241)) && ((num3 != 207) && (num3 != 128))) && ((num3 != 145) && (num3 != 164)))
                {
                    Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", ns);
                    ns.Dispose();
                    goto Label_0269;
                }
                handler1 = PacketHandlers.GetHandler(num3);
                if (handler1 == null)
                {
                    buffer1 = queue1.Dequeue(num1);
                    new PacketReader(buffer1, 0).Trace(ns);
                    goto Label_0269;
                }
                num4 = handler1.Length;
                if (num4 <= 0)
                {
                    if (num1 < 3)
                    {
                        goto Label_0269;
                    }
                    queue1.Peek(this.m_Peek, 0, 3);
                    num4 = ((this.m_Peek[1] << 8) | this.m_Peek[2]);
                    if (num4 < 3)
                    {
                        ns.Dispose();
                        goto Label_0269;
                    }
                }
                if (num1 < num4)
                {
                    goto Label_0269;
                }
                if (handler1.Ingame && (ns.Mobile == null))
                {
                    Console.WriteLine("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", ns, num3);
                    ns.Dispose();
                    goto Label_0269;
                }
                if (handler1.Ingame && ns.Mobile.Deleted)
                {
                    ns.Dispose();
                    goto Label_0269;
                }
                callback1 = handler1.ThrottleCallback;
                if ((callback1 != null) && !callback1.Invoke(ns))
                {
                    this.m_Throttled.Enqueue(ns);
                    return(false);
                }
                profile1 = PacketProfile.GetIncomingProfile(num3);
                time1    = ((profile1 == null) ? DateTime.MinValue : DateTime.Now);
                reader1  = new PacketReader(queue1.Dequeue(num4), (handler1.Length != 0));
                handler1.OnReceive(ns, reader1);
                num1 = queue1.Length;
                if (profile1 != null)
                {
                    profile1.Record(num4, ((TimeSpan)(DateTime.Now - time1)));
                }
Label_0250:
                if (num1 > 0)
                {
                    if (ns.Running)
                    {
                        goto Label_009D;
                    }
                }
            }
Label_0269:
            return(true);
        }