public NetState(Socket socket, MessagePump messagePump) { m_Socket = socket; m_Buffer = new ByteQueue(); m_Seeded = false; m_Running = false; m_RecvBuffer = m_ReceiveBufferPool.AcquireBuffer(); m_MessagePump = messagePump; m_Gumps = new List <Gump>(); m_HuePickers = new List <HuePicker>(); m_Menus = new List <IMenu>(); m_Trades = new List <SecureTrade>(); m_SendQueue = new SendQueue(); m_NextCheckActivity = Core.TickCount + 30000; m_Instances.Add(this); try { m_Address = Utility.Intern(((IPEndPoint)m_Socket.RemoteEndPoint).Address); m_ToString = m_Address.ToString(); } catch (Exception ex) { TraceException(ex); m_Address = IPAddress.None; m_ToString = "(error)"; } m_ConnectedOn = DateTime.UtcNow; if (m_CreatedCallback != null) { m_CreatedCallback(this); } }
public virtual void Dispose(bool flush) { if (m_Socket == null || m_Disposing) { return; } m_Disposing = true; if (flush) { flush = Flush(); } try { m_Socket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { TraceException(ex); } try { m_Socket.Close(); } catch (SocketException ex) { TraceException(ex); } if (m_RecvBuffer != null) { lock (m_ReceiveBufferPool) m_ReceiveBufferPool.ReleaseBuffer(m_RecvBuffer); } m_Socket = null; m_Buffer = null; m_RecvBuffer = null; #if NewAsyncSockets m_ReceiveEventArgs = null; m_SendEventArgs = null; #else m_OnReceive = null; m_OnSend = null; #endif m_Running = false; lock (m_Disposed) m_Disposed.Enqueue(this); lock (m_SendQueue) if (/*!flush &&*/ !m_SendQueue.IsEmpty) { m_SendQueue.Clear(); } }
public void HandleReceive(NetState ns) { ByteQueue buffer = ns.Buffer; if (buffer == null || buffer.Length <= 0) { return; } lock (buffer) { if (!ns.Seeded) { if (!HandleSeed(ns, buffer)) { return; } } int length = buffer.Length; while (length > 0 && ns.Running) { int packetID = buffer.GetPacketID(); if (CheckEncrypted(ns, packetID)) { 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) { if (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 (ns.Mobile.Deleted) { ns.Dispose(); break; } } ThrottlePacketCallback throttler = handler.ThrottleCallback; if (throttler != null && !throttler(ns)) { m_Throttled.Enqueue(ns); return; } PacketReceiveProfile prof = null; if (Core.Profiling) { 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; } } } }