Exemplo n.º 1
0
        private void IncomingMessageArrived(ArraySegment <byte> data)
        {
            var package = new TcpPackage();
            var valid   = false;

            try
            {
                package = TcpPackage.FromArraySegment(data);
                valid   = true;
                _handlePackage(this, package);
            }
            catch (Exception e)
            {
                _connection.Close(string.Format("Error when processing TcpPackage {0}: {1}",
                                                valid ? package.Command.ToString() : "<invalid package>", e.Message));

                var message = string.Format("TcpPackageConnection: [{0}, L{1}, {2}] ERROR for {3}. Connection will be closed.",
                                            RemoteEndPoint, LocalEndPoint, ConnectionId,
                                            valid ? package.Command.ToString() : "<invalid package>");
                if (_onError != null)
                {
                    _onError(this, e);
                }
                _log.Debug(e, message);
            }
        }
Exemplo n.º 2
0
 protected virtual void CleanupConnection(ITcpConnection connection)
 {
     lock (m_connections.SyncRoot)
     {
         Debug.WriteLine("Cleaning up connection");
         if (connection != null)
         {
             connection.IdleTimeout -= conn_IdleTimeout;
             m_connections.Remove(connection);
             connection.Close();
         }
     }
 }
Exemplo n.º 3
0
        public void when_connection_closed_quickly_socket_should_be_properly_disposed()
        {
            for (int i = 0; i < 1000; i++)
            {
                var            listeningSocket     = CreateListeningSocket();
                ITcpConnection clientTcpConnection = null;
                ITcpConnection serverTcpConnection = null;
                Socket         serverSocket        = null;
                try {
                    ManualResetEventSlim mre = new ManualResetEventSlim(false);

                    clientTcpConnection = TcpConnectionSsl.CreateConnectingConnection(
                        Guid.NewGuid(),
                        (IPEndPoint)listeningSocket.LocalEndPoint,
                        "localhost",
                        false,
                        new TcpClientConnector(),
                        TimeSpan.FromSeconds(5),
                        (conn) => {},
                        (conn, error) => {},
                        false);

                    clientTcpConnection.ConnectionClosed += (conn, error) => {
                        mre.Set();
                    };

                    serverSocket = listeningSocket.Accept();
                    clientTcpConnection.Close("Intentional close");
                    serverTcpConnection = TcpConnectionSsl.CreateServerFromSocket(Guid.NewGuid(),
                                                                                  (IPEndPoint)serverSocket.RemoteEndPoint, serverSocket, GetCertificate(), false);

                    mre.Wait(TimeSpan.FromSeconds(10));
                    SpinWait.SpinUntil(() => serverTcpConnection.IsClosed, TimeSpan.FromSeconds(10));

                    var disposed = false;
                    try {
                        int x = serverSocket.Available;
                    } catch (ObjectDisposedException) {
                        disposed = true;
                    }

                    Assert.True(disposed);
                } finally {
                    clientTcpConnection?.Close("Shut down");
                    serverTcpConnection?.Close("Shut down");
                    listeningSocket.Dispose();
                    serverSocket?.Dispose();
                }
            }
        }
Exemplo n.º 4
0
 protected void RemoveConnection(ITcpConnection connection)
 {
     try
     {
         if (!TcpConnections.Contains(connection))
         {
             return;
         }
         if (!connection.IsClosed)
         {
             connection.Close("Connection removed.");
         }
         _tcpConnections.Remove(connection);
     }
     catch (Exception e)
     {
         Log.ErrorException(e, "Error while removing a TCP connection.");
     }
 }
 public void Close(string reason = null)
 {
     _connection.Close(reason);
 }
Exemplo n.º 6
0
 void CloseConnectionOnError(string message)
 {
     Ensure.NotNull(message, "message");
     Log.Error("Closing connection '{0}#{1:d}' [R{2}, L{3}] due to error. Reason: {4}", ConnectionName, ConnectionId, RemoteEndPoint, LocalEndPoint, message);
     _tcpConnection.Close(message);
 }
Exemplo n.º 7
0
 public void Close()
 {
     _connection.Close(null);
 }
Exemplo n.º 8
0
 public void Stop()
 {
     _connection.Close(null);
     _connection.ConnectionClosed -= OnConnectionClosed;
     IsStopped = true;
 }
Exemplo n.º 9
0
 private void HandleTimeout(ITcpConnection connection)
 {
     RemoveFromConnecting(connection);
     Helper.EatException(() => connection.Close("Timeout on occured connect."));
 }
Exemplo n.º 10
0
 public void Stop()
 {
     IsStopped = true;
     _connection.Close();
     _connection.ConnectionClosed -= OnConnectionClosed;
 }