Enqueue() public method

Enqueues the specified buffer.
Buffer is is less than the minimum requirement.
public Enqueue ( byte buffer ) : void
buffer byte Buffer to enqueue.
return void
Exemplo n.º 1
0
        private void OnSendComplete(IAsyncResult ar)
        {
            var context   = (SendContext)ar.AsyncState;
            int bytesSent = _socket.EndSendTo(ar);

            BufferPool.Enqueue(context.buffer);
            _logger.Trace("OnSendComplete, " + bytesSent + " sent ");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Received a message from remote end point.
        /// </summary>
        /// <param name="ar"></param>
        private void OnRead(IAsyncResult ar)
        {
            var      buffer    = (byte[])ar.AsyncState;
            EndPoint endPoint  = new IPEndPoint(IPAddress.Any, 3929);
            int      bytesRead = 0;

            bool isKeepAlive = true;

            try
            {
                _logger.Trace("_socket.EndReceiveFrom");
                bytesRead = _socket.EndReceiveFrom(ar, ref endPoint);
                if (bytesRead <= 4)
                {
                    for (int i = 0; i < bytesRead; ++i)
                    {
                        if (buffer[i] != '\r' && buffer[i] != '\n')
                        {
                            isKeepAlive = false;
                            break;
                        }
                    }
                }
                else
                {
                    isKeepAlive = false;
                }

                if (!isKeepAlive)
                {
                    _logger.Debug("Received " + bytesRead + " bytes from " + endPoint + ":\r\n" +
                                  Encoding.ASCII.GetString(buffer, 0, bytesRead));
                }
            }
            catch (Exception err)
            {
                _logger.Warning("EndReceiveFrom failed: " + err);
            }


            // begin receiving another packet before starting to process this one
            byte[] newBuffer = BufferPool.Dequeue();

            try
            {
                EndPoint localEndPoint = new IPEndPoint(_listeningPoint.Address, _listeningPoint.Port);
                _socket.BeginReceiveFrom(newBuffer, 0, newBuffer.Length, SocketFlags.None,
                                         ref localEndPoint, OnRead, newBuffer);
            }
            catch (Exception err)
            {
                _logger.Warning("BeginReceiveFrom failed, closing socket. Exception: " + err);
                BufferPool.Enqueue(newBuffer);
                BufferPool.Enqueue(buffer);
                _socket.Close();
                return;
            }

            if (bytesRead == 0 || isKeepAlive)
            {
                BufferPool.Enqueue(buffer);
                return;
            }

            // Parse buffer.
            MessageFactoryContext factoryContext = _parsers.CreateNewContext(endPoint);

            try
            {
                int offset = factoryContext.Parse(buffer, 0, bytesRead);
                if (offset != bytesRead)
                {
                    _logger.Error("Failed to parse complete message");
                }
            }
            finally
            {
                BufferPool.Enqueue(buffer);
                _parsers.Release(factoryContext);
            }
        }
Exemplo n.º 3
0
 private void HandleDisconnect(ClientContext context)
 {
     BufferPool.Enqueue(context.Buffer);
     _factory.Release(context.Parser);
     context.Socket.Disconnect(true);
 }