Exemplo n.º 1
0
        public RemoteHost( INetworkFacade networkFacade )
        {
            Contract.Requires(networkFacade!=null);

            Connection = networkFacade;
            Parser = new HttpStreamParser();
            HasDisconnected = false;
            HasStoppedSendingData = false;
        }
Exemplo n.º 2
0
 private void HttpsServerConnect( bool success, INetworkFacade server )
 {
     try
     {
         if ( success )
         {
             _sslTunnel = new SslTunnel();
             _sslTunnel.TunnelClosed += HttpsTunnelClosed;
             _sslTunnel.EstablishTunnel( _clientConnection, server, _lastRequest.Version );
         }
         else
         {
             ServiceLog.Logger.Warning( "{0} Unable to connect to remote HTTPS host", Id );
         }
     }
     catch ( Exception ex )
     {
         ServiceLog.Logger.Exception( string.Format( "{0} Unhandled exception in HTTPS session.", Id ), ex );
         Reset();
     }
 }
Exemplo n.º 3
0
        private void HandleSendToClient( bool success, INetworkFacade facade )
        {
            //_sendingDataToClientLock.Set();
            ServiceLog.Logger.Verbose( "{0} ClientSession -- handle send data to client", Id );

            if ( !success )
            {
                ServiceLog.Logger.Warning( "{0} Unable to send to client", Id );
                Reset();
            }
        }
Exemplo n.º 4
0
        private void HandleClose( bool success, INetworkFacade client )
        {
            ServiceLog.Logger.Verbose( "{0} ClientSession -- client closing socket", Id );

            try
            {
                // Not much we can do here if unsuccessful...
                if ( !success )
                {
                    ServiceLog.Logger.Warning( "{0} Unable to shutdown client session", Id );
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception( string.Format( "{0} Unhandled exception closing client connection", Id ), ex );
            }
        }
Exemplo n.º 5
0
        public void Start( INetworkFacade connection )
        {
            Contract.Ensures( _clientConnection != null );
            Contract.Ensures( _parser != null );

            ServiceLog.Logger.Info( "{0} Starting new proxy client session with client ID {1}", Id, connection.Id );

            _parser = new HttpStreamParser();
            _parser.AdditionalDataRequested += HandleParserAdditionalDataRequested;
            _parser.ReadRequestHeaderComplete += HandleParserReadRequestHeaderComplete;
            _parser.PartialDataAvailable += HandleParserPartialDataAvailable;

            _clientConnection = connection;
            _clientConnection.ConnectionClosed += HandleClientConnectionClosed;

            _clientConnection.BeginReceive( ReceiveDataFromClient );

            _hasClientStoppedSendingData = false;
        }
Exemplo n.º 6
0
 public void Start( INetworkFacade connection )
 {
     Contract.Requires(connection != null);
     Contract.Requires(ClientConnection == null);
     Contract.Requires(ServerConnection == null);
 }
Exemplo n.º 7
0
 public void SetupServerConnection( INetworkFacade serverConnection )
 {
 }
Exemplo n.º 8
0
        private void HandleClientConnected( INetworkFacade clientConnection )
        {
            try
            {
                //ServiceLog.Logger.Info("ProxyService notified of new client connect. Pool size: {0}", _sessionPool.AvailablePoolSize);

                IProxySession session = _sessionPool.Get();

                lock (_activeSessions)
                {
                    _activeSessions.Add(session);
                }

                session.SessionEnded += HandleSessionEnded;

                session.Start(clientConnection);
            }
            catch ( InvalidOperationException ex )
            {
                ServiceLog.Logger.Exception(
                    "No more clients can be accepted; the pool of available sessions was exhausted. Increase the pool maximum number of clients in the proxy server settings.",
                    ex );
            }
        }
Exemplo n.º 9
0
        private void SetupServerConnection( INetworkFacade serverConnection )
        {
            ServiceLog.Logger.Verbose( "{0} SessionContext::SetupServerConnection", Id );

            try
            {
                lock ( _changeServerConnectionMutex )
                {
                    //UnwireServerParserEvents();

                    //if ( ServerConnection != null )
                    //{
                    //    ServerConnection.BeginClose(
                    //        ( s, f ) => ServiceLog.Logger.Info( "{0} Server connection closed", Id ) );
                    //}

                    ServerConnection = serverConnection;

                    if ( serverConnection != null )
                    {
                        _remoteHosts.Add( new RemoteHost(){ Connection = serverConnection, HasDisconnected = false, HasStoppedSendingData = false} );

                        ServerParser = new HttpStreamParser();
                        ServerParser.AdditionalDataRequested += HandleServerParserAdditionalDataRequested;
                        ServerParser.PartialDataAvailable += HandleServerParserPartialDataAvailable;
                        ServerParser.ReadResponseHeaderComplete += HandleServerParserReadResponseHeaderComplete;
                        ServerParser.MessageReadComplete += HandleServerParserMessageReadComplete;
                        ServerConnection.ConnectionClosed += HandleServerConnectionConnectionClosed;

                        HasServerBegunShutdown = false;

                        ServerConnection.BeginReceive( HandleServerReceive );
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception setting up server connection", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }
Exemplo n.º 10
0
        private void SetupClientConnection( INetworkFacade clientConnection )
        {
            ServiceLog.Logger.Info( "{0} SessionContext::SetupClientConnection", Id );

            try
            {
                lock ( _changeClientConnectionMutex )
                {
                    if (ClientParser != null)
                    {
                        ClientParser.AdditionalDataRequested -= HandleClientParserAdditionalDataRequested;
                        ClientParser.PartialDataAvailable -= HandleClientParserPartialDataAvailable;
                        ClientParser.ReadRequestHeaderComplete -= HandleClientParserReadRequestHeaderComplete;
                        ClientParser = null;
                    }

                    if (ClientConnection != null)
                    {
                        ClientConnection.ConnectionClosed -= HandleClientConnectionClosed;
                        ClientConnection.BeginClose(
                            ( s, f ) => ServiceLog.Logger.Info( "{0} Client connection closed", Id ) );
                    }

                    ClientConnection = clientConnection;

                    if ( ClientConnection != null )
                    {
                        ClientParser = new HttpStreamParser();
                        ClientParser.AdditionalDataRequested += HandleClientParserAdditionalDataRequested;
                        ClientParser.PartialDataAvailable += HandleClientParserPartialDataAvailable;
                        ClientParser.ReadRequestHeaderComplete += HandleClientParserReadRequestHeaderComplete;
                        ClientConnection.ConnectionClosed += HandleClientConnectionClosed;

                        HasClientBegunShutdown = false;

                        ClientConnection.BeginReceive( HandleClientReceive );
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception setting up client connection", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }
Exemplo n.º 11
0
        private void HandleServerReceive( bool success, byte[] data, INetworkFacade client )
        {
            Contract.Requires( client != null );

            ServiceLog.Logger.Verbose( "{0} SessionContext::HandleServerReceive", Id );

            try
            {
                if ( success && ServerParser != null )
                {
                    if ( data == null )
                    {
                        ServiceLog.Logger.Info(
                            "{0} Server has shutdown the socket and will not be sending more data.", Id );
                        HasServerBegunShutdown = true;
                        State.AcknowledgeServerShutdown( this );
                    }
                    else
                    {
                        LastNetworkActivity = DateTime.Now;
                        ServerParser.AppendData( data );
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception receiving data from server", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }
Exemplo n.º 12
0
        private void HandleClientReceive( bool success, byte[] data, INetworkFacade client )
        {
            Contract.Requires( client != null );

            ServiceLog.Logger.Verbose( "{0} SessionContext::HandleClientReceive", Id );

            try
            {
                if ( success && ClientParser != null )
                {
                    if ( data == null )
                    {
                        ServiceLog.Logger.Info( "{0} Client is still active but has stopped sending data.",
                                                Id );
                        HasClientBegunShutdown = true;
                        State.AcknowledgeClientShutdown( this );
                    }
                    else
                    {
                        LastNetworkActivity = DateTime.Now;
                        ClientParser.AppendData( data );
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception(
                    string.Format( "{0} Unhandled exception receiving data from client", Id ), ex );
                ChangeState( SessionStateType.Error );
            }
        }
Exemplo n.º 13
0
        private void DataSent( bool success, INetworkFacade facade )
        {
            Contract.Requires( facade != null );

            ServiceLog.Logger.Verbose( "{0} SessionContext::DataSent", Id );

            if ( !success )
            {
                Reset();
            }
        }
Exemplo n.º 14
0
        private void ConnectionEstablished( bool success, INetworkFacade server )
        {
            ServiceLog.Logger.Verbose( "{0} SessionContext::ConnectionEstablished", Id );

            if ( success )
            {
                //SetupServerConnection( server );

                State.ServerConnectionEstablished( this );

                // Free the HTTP parsers to send HTTP body data
                _serverConnectingEvent.Set();
            }
            else
            {
                ServiceLog.Logger.Warning( "{0} Unable to connect to remote host {1} {2}", Id, Host, Port );
                ChangeState( SessionStateType.Error );
            }
        }
Exemplo n.º 15
0
        public void Start( INetworkFacade connection )
        {
            ServiceLog.Logger.Info( "{0} Starting new client session", Id );

            ChangeState( SessionStateType.ClientConnecting );

            // Wire up events for the client
            SetupClientConnection( connection );
        }
Exemplo n.º 16
0
        private void ReceiveDataFromClient( bool success, byte[] rawData, INetworkFacade client )
        {
            ServiceLog.Logger.Verbose( "{0} ClientSession -- received data from client", Id );

            try
            {
                if ( success )
                {
                    if ( rawData == null )
                    {
                        ServiceLog.Logger.Info( "{0} Client socket has stopped sending data.", Id );
                        _hasClientStoppedSendingData = true;

                        ServiceLog.Logger.Verbose( "{0} Pipeline depth = {1}", Id, _dispatcher.PipeLineDepth );

                        if ( _dispatcher.PipeLineDepth == 0 )
                        {
                            ServiceLog.Logger.Info( "{0} No active servers remain and client shutdown socket. Resetting.", Id );
                            Reset();
                        }
                    }
                    else
                    {
                        _parser.AppendData( rawData );
                    }
                }
                else
                {
                    ServiceLog.Logger.Info( "{0} Client receive reported failure. Resetting client.", Id );
                    Reset();
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception( string.Format( "{0} Unhandled exception receiving data from client.", Id ), ex );
                Reset();
            }
        }
Exemplo n.º 17
0
        public void Reset()
        {
            Contract.Ensures( _clientConnection == null );

            ServiceLog.Logger.Info( "{0} Session reset", Id );

            try
            {
                lock ( _resetMutex )
                {
                    ResetParser();

                    if ( _clientConnection != null )
                    {
                        _clientConnection.ConnectionClosed -= HandleClientConnectionClosed;
                        _clientConnection.BeginClose( HandleClose );

                        _clientConnection = null;

                        _dispatcher.Reset();

                        EventHandler sessionEnded = SessionEnded;
                        if ( sessionEnded != null )
                        {
                            SessionEnded( this, new EventArgs() );
                        }

                        _hasClientStoppedSendingData = false;
                    }
                }
            }
            catch ( Exception ex )
            {
                ServiceLog.Logger.Exception( string.Format( "{0} Unhandled exception resetting client session.", Id ), ex );
                Reset();
            }
        }
Exemplo n.º 18
0
 public void SetupClientConnection( INetworkFacade clientConnection )
 {
 }