예제 #1
0
        private void AcceptTcpClientCallback(IAsyncResult asyncResult)
        {
            lock (_syncRoot)
            {
                // Bail out if we're closing.

                if (State != ProtoHostState.Listening)
                    return;

                try
                {
                    var tcpClient = _listener.EndAcceptTcpClient(asyncResult);
                    HostConnection connection = null;

                    try
                    {
                        connection = new HostConnection(this, tcpClient, _streamManager);

                        _connections.Add(connection, null);

                        connection.Connect();
                    }
                    catch
                    {
                        if (connection != null)
                        {
                            _connections.Remove(connection);

                            connection.Dispose();
                        }

                        tcpClient.Close();

                        throw;
                    }
                }
                catch (Exception ex)
                {
                    Log.Info("Failed to accept TCP client", ex);
                }

                try
                {
                    _listener.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
                }
                catch (Exception ex)
                {
                    Log.Warn("BeginAcceptTcpClient failed", ex);

                    Close();
                }
            }
        }
예제 #2
0
        internal void RemoveConnection(HostConnection connection)
        {
            Require.NotNull(connection, "connection");

            lock (_syncRoot)
            {
                Client hostClient;

                if (_connections.TryGetValue(connection, out hostClient))
                {
                    _connections.Remove(connection);

                    if (hostClient != null)
                    {
                        bool removed = _clients.Remove(hostClient.Instance);

                        Debug.Assert(removed);
                    }
                }

                // We progress to the closed state when all connections have
                // been closed.

                if (State == ProtoHostState.Closing && _connections.Count == 0)
                    State = ProtoHostState.Closed;
            }
        }
예제 #3
0
        internal Client RaiseClientConnected(HostConnection connection, int protocolNumber)
        {
            Require.NotNull(connection, "connection");

            lock (_syncRoot)
            {
                Debug.Assert(_connections.ContainsKey(connection) && _connections[connection] == null);

                object client = null;

                try
                {
                    client = CreateServiceCore(protocolNumber);
                }
                catch (Exception ex)
                {
                    Log.Error("Failed to create service", ex);
                }

                if (client != null)
                {
                    var hostClient = new Client(client, ServiceAssembly, Service);

                    _connections[connection] = hostClient;
                    _clients[client] = connection;

                    return hostClient;
                }
                else
                {
                    return null;
                }
            }
        }
예제 #4
0
        internal void RaiseUnhandledException(HostConnection connection, Exception exception)
        {
            Require.NotNull(connection, "connection");
            Require.NotNull(exception, "exception");

            try
            {
                OnUnhandledException(new UnhandledExceptionEventArgs(exception));
            }
            catch (Exception ex)
            {
                Log.Warn("Exception from UnhandledException event", ex);
            }

            try
            {
                connection.Dispose();
            }
            catch (Exception ex)
            {
                Log.Warn("Disposing connection failed", ex);
            }
        }
예제 #5
0
        internal void RemoveConnection(HostConnection connection)
        {
            Require.NotNull(connection, "connection");

            lock (_syncRoot)
            {
                _connections.Remove(connection);

                // We progress to the closed state when all connections have
                // been closed.

                if (State == ProtoHostState.Closing && _connections.Count == 0)
                    State = ProtoHostState.Closed;
            }
        }