Exemplo n.º 1
0
        /// <summary>
        /// Can be used to create filtering of new connections.
        /// </summary>
        /// <param name="socket">Accepted socket</param>
        /// <returns>
        /// true if connection can be accepted; otherwise false.
        /// </returns>
        protected override bool OnAcceptingSocket(Socket socket)
        {
            ClientAcceptedEventArgs args = new ClientAcceptedEventArgs(socket);

            Accepted(this, args);
            return(!args.Revoked);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Can be used to create filtering of new connections.
 /// </summary>
 /// <param name="socket">Accepted socket</param>
 /// <returns>
 /// true if connection can be accepted; otherwise false.
 /// </returns>
 protected override bool OnAcceptingSocket(Socket socket)
 {
     ClientAcceptedEventArgs args = new ClientAcceptedEventArgs(socket);
     Accepted(this, args);
     return !args.Revoked;
 }
Exemplo n.º 3
0
            private void OnAccept(IAsyncResult ar)
            {
                try
                {
                    // i'm not trying to avoid ALL cases here. but just the most simple ones.
                    // doing a lock would defeat the purpose since only one socket could be accepted at once.

                    // the lock kills performance and that's why I temporarly disabled it.
                    // right now it's up to the exception block to handle Stop()

                    /*lock (_listenLock)
                     *  if (!_canListen)
                     *      return;
                     */
                    Socket socket = _listener.EndAcceptSocket(ar);
                    _listener.BeginAcceptSocket(OnAccept, null);

                    ClientAcceptedEventArgs args = new ClientAcceptedEventArgs(socket);
                    Accepted(this, args);
                    if (args.Revoked)
                    {
                        _logWriter.Write(this, LogPrio.Debug, "Socket was revoked by event handler.");
                        socket.Close();
                        return;
                    }

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

                    NetworkStream stream         = new NetworkStream(socket, true);
                    IPEndPoint    remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;

                    if (_certificate != null)
                    {
                        CreateSecureContext(stream, remoteEndPoint);
                    }
                    else
                    {
                        new HttpClientContextImp(false, remoteEndPoint, _requestHandler, _disconnectHandler, stream,
                                                 LogWriter);
                    }
                }
                catch (Exception err)
                {
                    if (err is ObjectDisposedException || err is NullReferenceException) // occurs when we shut down the listener.
                    {
                        if (UseTraceLogs)
                        {
                            _logWriter.Write(this, LogPrio.Trace, err.Message);
                        }
                        return;
                    }

                    _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);
                    }
                }
            }
 private void _listener_Accepted(object sender, ClientAcceptedEventArgs e)
 {
     LogInfo("_listener_Accepted " + e.Socket);
 }
Exemplo n.º 5
0
            private void OnAccept(IAsyncResult ar)
            {
                try
                {
                    // i'm not trying to avoid ALL cases here. but just the most simple ones.
                    // doing a lock would defeat the purpose since only one socket could be accepted at once.

                    // the lock kills performance and that's why I temporarly disabled it.
                    // right now it's up to the exception block to handle Stop()
                    /*lock (_listenLock)
                        if (!_canListen)
                            return;
                    */
                    Socket socket = _listener.EndAcceptSocket(ar);
                    _listener.BeginAcceptSocket(OnAccept, null);

                    ClientAcceptedEventArgs args = new ClientAcceptedEventArgs(socket);
                    Accepted(this, args);
                    if (args.Revoked)
                    {
                        _logWriter.Write(this, LogPrio.Debug, "Socket was revoked by event handler.");
                        socket.Close();
                        return;
                    }

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

                    NetworkStream stream = new NetworkStream(socket, true);
                    IPEndPoint remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;

                    if (_certificate != null)
                        CreateSecureContext(stream, remoteEndPoint);
                    else
                        new HttpClientContextImp(false, remoteEndPoint, _requestHandler, _disconnectHandler, stream,
                                                 LogWriter);
                }
                catch (Exception err)
                {
                    if (err is ObjectDisposedException || err is NullReferenceException) // occurs when we shut down the listener.
                    {
                        if (UseTraceLogs)
                            _logWriter.Write(this, LogPrio.Trace, err.Message);
                        return;
                    }

                    _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);
                }
            }