Ejemplo n.º 1
0
        private void HandleReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return;
            }
            lock (buffer) {
                int length = buffer.Length;
                while (length > 0 && ns.IsConnected)
                {
                    int packetLength = buffer.Length; // guessing here
                    try {
                        byte[] packetBuffer = _Buffers.AcquireBuffer();
                        packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);
                        switch (ns.ServerPort)
                        {
                        case Config.PortHttp:
                            OnReceiveHttp?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortHttps:
                            OnReceiveHttps?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc27900:
                            OnReceiveWfc27900?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc27901:
                            OnReceiveWfc27901?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc29900:
                            OnReceiveWfc29900?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        case Config.PortWfc29901:
                            OnReceiveWfc29901?.Invoke(ns, packetBuffer, packetLength);
                            break;

                        default:
                            Kernel.WriteLine(TypeName, $"{ns} sent message to unhandled port {ns.ServerPort}.");
                            WritePacketToConsole(packetBuffer, packetLength);
                            break;
                        }
                        length = buffer.Length;
                        _Buffers.ReleaseBuffer(packetBuffer);
                    }
                    catch (Exception e) {
                        Kernel.WriteLine(TypeName, $"{ns} sent message that could not be decoded:{Environment.NewLine}{e}");
                        ns.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 2
0
 protected NetState(NetPump messagePump)
 {
     _MessagePump       = messagePump;
     _NextCheckActivity = Timing.Ticks + MsBeforeDisconnectFirst;
     _Buffer            = new ByteQueue();
     _RecvBuffer        = _ReceiveBufferPool.AcquireBuffer();
     _SendQueue         = new SendQueue();
     Instances.Add(this);
     ConnectedOn = DateTime.UtcNow;
 }
Ejemplo n.º 3
0
 protected void Cleanup()
 {
     if (_RecvBuffer != null)
     {
         lock (_ReceiveBufferPool) {
             _ReceiveBufferPool.ReleaseBuffer(_RecvBuffer);
         }
     }
     _Buffer     = null;
     _RecvBuffer = null;
     lock (_Disposed) {
         _Disposed.Enqueue(this);
     }
     lock (_SendQueue) {
         if (!_SendQueue.IsEmpty)
         {
             _SendQueue.Clear();
         }
     }
 }