/// <exception cref="Exception"><c>Exception</c>.</exception>
        private void OnAccept(IAsyncResult ar)
        {
            bool beginAcceptCalled = false;

            try
            {
                int count = Interlocked.Decrement(ref _pendingAccepts);
                if (_shutdown)
                {
                    if (count == 0)
                    {
                        _shutdownEvent.Set();
                    }
                    return;
                }

                Interlocked.Increment(ref _pendingAccepts);
                _listener.BeginAcceptSocket(OnAccept, null);
                beginAcceptCalled = true;
                Socket socket = _listener.EndAcceptSocket(ar);

                if (!OnAcceptingSocket(socket))
                {
                    socket.Disconnect(true);
                    return;
                }

                _logWriter.Write(this, LogPrio.Debug, "Accepted connection from: " + socket.RemoteEndPoint);

                if (_certificate != null)
                {
                    _factory.CreateSecureContext(socket, _certificate, _sslProtocol);
                }
                else
                {
                    _factory.CreateContext(socket);
                }
            }
            catch (Exception err)
            {
                _logWriter.Write(this, LogPrio.Debug, err.Message);
                if (ExceptionThrown == null)
#if DEBUG
                { throw; }
#else
                { _logWriter.Write(this, LogPrio.Fatal, err.Message); }
                // we can't really do anything but close the connection
#endif
                if (ExceptionThrown != null)
                {
                    ExceptionThrown(this, err);
                }

                if (!beginAcceptCalled)
                {
                    RetryBeginAccept();
                }
            }
        }
Пример #2
0
        /// <exception cref="Exception"><c>Exception</c>.</exception>
        private void OnAccept(IAsyncResult ar)
        {
            bool beginAcceptCalled = false;

            try
            {
                int count = Interlocked.Decrement(ref _pendingAccepts);
                if (_shutdown)
                {
                    if (count == 0)
                    {
                        _shutdownEvent.Set();
                    }
                    return;
                }

                Interlocked.Increment(ref _pendingAccepts);
                _listener.BeginAcceptSocket(OnAccept, null);
                beginAcceptCalled = true;
                Socket socket = _listener.EndAcceptSocket(ar);

                if (!OnAcceptingSocket(socket))
                {
                    socket.Disconnect(true);  // Done in finalizer
                    return;
                }

                _logWriter.Write(this, LogPrio.Debug, "Accepted connection from: " + socket.RemoteEndPoint);

                IHttpClientContext clientContext;

                if (_certificate != null)
                {
                    clientContext = _factory.CreateSecureContext(socket, _certificate, _sslProtocol, _requireClientCerts);
                }
                else
                {
                    clientContext = _factory.CreateContext(socket);
                }

                // Disconnect the socket if we failed to create a context.
                // Common situation: client connects to an SSL listener using the regular HTTP protocol
                if (clientContext == null)
                {
                    socket.Disconnect(true);
                }
            }
            catch (Exception err)
            {
                ThrowException(err);

                if (!beginAcceptCalled)
                {
                    RetryBeginAccept();
                }
            }
        }
Пример #3
0
        /// <exception cref="Exception"><c>Exception</c>.</exception>
        private void OnAccept(IAsyncResult ar)
        {
            bool beginAcceptCalled = false;

            try
            {
                int count = Interlocked.Decrement(ref m_pendingAccepts);
                if (m_shutdown)
                {
                    if (count == 0)
                    {
                        m_shutdownEvent.Set();
                    }
                    return;
                }

                Interlocked.Increment(ref m_pendingAccepts);
                m_listener.BeginAcceptSocket(OnAccept, null);
                beginAcceptCalled = true;
                Socket socket = m_listener.EndAcceptSocket(ar);

                if (!OnAcceptingSocket(socket))
                {
                    socket.Disconnect(true);
                    return;
                }

                m_logWriter.Write(this, LogPrio.Debug, "Accepted connection from: " + socket.RemoteEndPoint);

                if (m_certificate != null)
                {
                    m_contextFactory.CreateSecureContext(socket, m_certificate, m_sslProtocol, m_clientCertValCallback);
                }
                else
                {
                    m_contextFactory.CreateContext(socket);
                }
            }
            catch (Exception err)
            {
                m_logWriter.Write(this, LogPrio.Debug, err.Message);
                ExceptionThrown?.Invoke(this, err);

                if (!beginAcceptCalled)
                {
                    RetryBeginAccept();
                }
            }
        }
Пример #4
0
        /// <exception cref="Exception"><c>Exception</c>.</exception>
        private void OnAccept(IAsyncResult ar)
        {
            bool beginAcceptCalled = false;

            try
            {
                int count = Interlocked.Decrement(ref _pendingAccepts);
                if (_shutdown)
                {
                    if (count == 0)
                    {
                        _shutdownEvent.Set();
                    }
                    return;
                }

                Interlocked.Increment(ref _pendingAccepts);
                _listener.BeginAcceptSocket(OnAccept, null);
                beginAcceptCalled = true;
                Socket socket = _listener.EndAcceptSocket(ar);

                if (!OnAcceptingSocket(socket))
                {
                    socket.Disconnect(true);
                    return;
                }

                _logWriter.Write(this, LogPrio.Debug, "Accepted connection from: " + socket.RemoteEndPoint);

                if (_certificate != null)
                {
                    _factory.CreateSecureContext(socket, _certificate, _sslProtocol, _requireClientCerts);
                }
                else
                {
                    _factory.CreateContext(socket);
                }
            }
            catch (Exception err)
            {
                ThrowException(err);

                if (!beginAcceptCalled)
                {
                    RetryBeginAccept();
                }
            }
        }