Exemplo n.º 1
0
        /// <summary>
        /// Close and release socket.
        /// </summary>
        private void Close()
        {
            lock (this)
            {
                if (Socket == null)
                {
                    return;
                }

                if (_keepAlive != null)
                {
                    _keepAlive.Dispose();
                    _keepAlive = null;
                }

                try
                {
                    Socket.Disconnect(true);
                    Socket.Close();
                    Socket = null;
                    Stream.Close();
                    Stream.Dispose();
                    Stream = null;
                    MessageFactoryContext.RequestCompleted          -= OnRequest;
                    MessageFactoryContext.ContinueResponseRequested -= On100Continue;
                    MessageFactoryContext.Reset();
                }
                catch (Exception err)
                {
                    _logger.Warning("Failed to close context properly.", err);
                }
            }
            Disconnected(this, EventArgs.Empty);
        }
Exemplo n.º 2
0
        private int _keepAliveTimeout = 100000;         // 100 seconds.

        /// <summary>
        /// Initializes a new instance of the <see cref="HttpContext"/> class.
        /// </summary>
        /// <param name="socket">Socket received from HTTP listener.</param>
        /// <param name="context">Context used to parse incoming messages.</param>
        public HttpContext(Socket socket, MessageFactoryContext context)
        {
            Socket = socket;
            MessageFactoryContext = context;
            MessageFactoryContext.RequestCompleted          += OnRequest;
            MessageFactoryContext.ContinueResponseRequested += On100Continue;
        }
Exemplo n.º 3
0
        private object CreateHttpContext(Type type, object[] arguments)
        {
            MessageFactoryContext context = Get <MessageFactory>().CreateNewContext();
            var httpContext = new HttpContext((Socket)arguments[0], context);

            httpContext.Disconnected += OnContextDisconnected;
            return(httpContext);
        }
Exemplo n.º 4
0
 public ClientContext(Socket socket, byte[] buffer, MessageFactoryContext parser)
     : this()
 {
     Socket = socket;
     Parser = parser;
     Buffer = buffer;
     Offset = 0;
 }
Exemplo n.º 5
0
        private object CreateSecureHttpContext(Type type, object[] arguments)
        {
            MessageFactoryContext context = Get <MessageFactory>().CreateNewContext();
            var certificate = (X509Certificate)arguments[0];
            var protocols   = (SslProtocols)arguments[1];
            var httpContext = new SecureHttpContext(certificate, protocols, (Socket)arguments[2], context);

            httpContext.Disconnected += OnContextDisconnected;
            return(httpContext);
        }
Exemplo n.º 6
0
 private void Parse(MessageFactoryContext context, string message, int offset)
 {
     byte[] bytes = Encoding.ASCII.GetBytes(message);
     if (offset == 0)
     {
         context.Parse(bytes, 0, bytes.Length);
     }
     else
     {
         context.Parse(bytes, 0, offset);
         context.Parse(bytes, offset, bytes.Length - offset);
     }
 }
Exemplo n.º 7
0
        private void TestTortousInvite()
        {
            MessageFactoryContext context = _factory.CreateNewContext(null);

            Parse(context, Messages.AShortTortuousINVITE);
            Assert.NotNull(_request);
            Assert.Equal("chair-dnrc.example.com", _request.Uri.Domain);
            Assert.Equal("1918181833n", _request.To.Parameters["tag"]);

            Via via = _request.Via;

            Assert.Equal(3, via.Items.Count);
            Assert.Equal("390skdjuw", via.Items[0].Branch);
            Assert.Equal("SIP/2.0", via.Items[0].SipVersion);
            Assert.Equal("192.168.255.111", via.Items[2].Domain);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Parse all complete requests in buffer.
        /// </summary>
        /// <param name="bytesLeft"></param>
        /// <returns>offset in buffer where parsing stopped.</returns>
        /// <exception cref="InvalidOperationException">Parsing failed.</exception>
        private int ParseBuffer(int bytesLeft)
        {
            int offset = MessageFactoryContext.Parse(_buffer, 0, bytesLeft);

            bytesLeft -= offset;

            // try another pass if we got bytes left.
            if (bytesLeft <= 0)
            {
                return(offset);
            }

            // Continue until offset is not changed.
            int oldOffset = 0;

            while (offset != oldOffset)
            {
                oldOffset = offset;
                _logger.Trace("Parsing from index " + offset + ", " + bytesLeft + " bytes.");
                offset     = MessageFactoryContext.Parse(_buffer, offset, bytesLeft);
                bytesLeft -= offset;
            }
            return(offset);
        }
Exemplo n.º 9
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.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureHttpContext"/> class.
 /// </summary>
 /// <param name="protocols">SSL protocol to use.</param>
 /// <param name="socket">The socket.</param>
 /// <param name="context">The context.</param>
 /// <param name="certificate">Server certificate to use.</param>
 public SecureHttpContext(X509Certificate certificate, SslProtocols protocols, Socket socket,
                          MessageFactoryContext context) : base(socket, context)
 {
     _certificate = certificate;
     Protocol     = protocols;
 }
Exemplo n.º 11
0
 private void Parse(MessageFactoryContext context, string message)
 {
     Parse(context, message, 0);
 }