public 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]; //Console.WriteLine( "Login: {0}: Seed is 0x{1:X8}", ns, seed ); if (seed == 0) { Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns); ns.Dispose(); return(false); } ns.m_Seed = seed; ns.Seeded = true; } return(true); } //Console.WriteLine( "{" ); 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); //Console.WriteLine( "}" ); return(false); } //Console.WriteLine( handler.OnReceive.Method.Name ); PacketProfile profile = PacketProfile.GetIncomingProfile(packetID); DateTime start = (profile == null ? DateTime.MinValue : DateTime.Now); 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) { Console.WriteLine("Exception disarmed in HandleReceive from {0}: {1}", ns.Address, e); } length = buffer.Length; if (BufferSize >= packetLength) { m_Buffers.ReleaseBuffer(packetBuffer); } if (profile != null) { profile.Record(packetLength, DateTime.Now - start); } //Console.WriteLine( "Client: {0}: Unhandled packet 0x{1:X2}", ns, packetID ); //Utility.FormatBuffer( Console.Out, new System.IO.MemoryStream( r.Buffer ), r.Buffer.Length ); } } else { break; } } //Console.WriteLine( "}" ); } return(true); }
private bool HandleReceive(NetState ns) { lock ( ns ) { ByteQueue buffer = ns.Buffer; if (buffer == null) { return(true); } int length = buffer.Length; if (!ns.Seeded) { if (buffer.GetPacketID() == 0xEF) { // new packet in client 6.0.5.0 replaces the traditional seed method with a seed packet // 0xEF = 239 = multicast IP, so this should never appear in a normal seed. So this is backwards compatible with older clients. ns.Seeded = true; } else 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 && packetID != 0xEF) { 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); }
public static byte[] GetUnusedBuffer() { return(m_UnusedBuffers.AquireBuffer()); }