public bool HandleReceive(NetState ns) { ByteQueue buffer = ns.Buffer; if (buffer == null || buffer.Length <= 0) return true; lock (buffer) { int length = buffer.Length; 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 != 0xEF) { Console.WriteLine("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) { ThrottlePacketCallback throttler = handler.ThrottleCallback; if (throttler != null && !throttler(ns)) { m_Throttled.Enqueue(ns); return false; } PacketReceiveProfile prof = PacketReceiveProfile.Acquire(packetID); if (prof != null) { prof.Start(); } 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 (prof != null) { prof.Finish(packetLength); } } else { break; } } } return true; }
public void OnReceive(NetState ns) { lock (this) m_Queue.Enqueue(ns); Hamaxe.Set(); }
private void CheckListener() { for (int j = 0; j < m_Listeners.Length; ++j) { Socket[] accepted = m_Listeners[j].Slice(); for (int i = 0; i < accepted.Length; ++i) { NetState ns = new NetState(accepted[i], this); ns.Start(); if (ns.Running) Console.WriteLine("Client: {0}: Connected. [{1} Online]", ns, NetState.Instances.Count); } } }
public void Trace(NetState ClientState) { try { using (StreamWriter sw = new StreamWriter("Packets.log", true)) { byte[] buffer = m_Data; if (buffer.Length > 0) sw.WriteLine("Client: {0}: Unhandled packet 0x{1:X2}", ClientState, buffer[0]); using (MemoryStream ms = new MemoryStream(buffer)) Utility.FormatBuffer(sw, ms, buffer.Length); sw.WriteLine(); sw.WriteLine(); } } catch { } }