示例#1
0
        public TcpTypedConnection(TcpConnection connection)
        {
            _connection       = connection;
            _framer           = new LengthPrefixMessageFramer();
            EffectiveEndPoint = connection.EffectiveEndPoint;

            connection.ConnectionClosed += OnConnectionClosed;

            //Setup callback for incoming messages
            _framer.RegisterMessageArrivedCallback(IncomingMessageArrived);
        }
示例#2
0
        public TcpTypedConnection(TcpConnection connection)
        {
            _connection = connection;
            _framer = new LengthPrefixMessageFramer();
            EffectiveEndPoint = connection.EffectiveEndPoint;

            connection.ConnectionClosed += OnConnectionClosed;

            //Setup callback for incoming messages
            _framer.RegisterMessageArrivedCallback(IncomingMessageArrived);
        }
        public TcpPackageConnection(ILogger log,
                                    IPEndPoint remoteEndPoint,
                                    Guid connectionId,
                                    bool ssl,
                                    string targetHost,
                                    bool validateServer,
                                    TimeSpan timeout,
                                    Action<TcpPackageConnection, TcpPackage> handlePackage,
                                    Action<TcpPackageConnection, Exception> onError,
                                    Action<TcpPackageConnection> connectionEstablished,
                                    Action<TcpPackageConnection, SocketError> connectionClosed)
        {
            Ensure.NotNull(log, "log");
            Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
            Ensure.NotEmptyGuid(connectionId, "connectionId");
            Ensure.NotNull(handlePackage, "handlePackage");
            if (ssl)
                Ensure.NotNullOrEmpty(targetHost, "targetHost");

            ConnectionId = connectionId;
            _log = log;
            _handlePackage = handlePackage;
            _onError = onError;

            //Setup callback for incoming messages
            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(IncomingMessageArrived);

            var connectionCreated = new ManualResetEventSlim();
// ReSharper disable ImplicitlyCapturedClosure
            _connection = Connector.ConnectTo(
                log,
                connectionId,
                remoteEndPoint,
                ssl,
                targetHost,
                validateServer,
                timeout,
                tcpConnection =>
                {
                    connectionCreated.Wait();
                    log.Debug("Connected to [{0}, L{1}, {2:B}].", tcpConnection.RemoteEndPoint, tcpConnection.LocalEndPoint, connectionId);
                    if (connectionEstablished != null)
                        connectionEstablished(this);
                },
                (conn, error) =>
                {
                    connectionCreated.Wait();
                    log.Debug("Connection to [{0}, L{1}, {2:B}] failed. Error: {3}.", conn.RemoteEndPoint, conn.LocalEndPoint, connectionId, error);
                    if (connectionClosed != null)
                        connectionClosed(this, error);
                },
                (conn, error) =>
                {
                    connectionCreated.Wait();
                    log.Debug("Connection [{0}, L{1}, {2:B}] was closed {3}", conn.RemoteEndPoint, conn.LocalEndPoint,
                              ConnectionId, error == SocketError.Success ? "cleanly." : "with error: " + error + ".");

                    if (connectionClosed != null)
                        connectionClosed(this, error);
                });
// ReSharper restore ImplicitlyCapturedClosure

            connectionCreated.Set();
        }
示例#4
0
        public TcpPackageConnection(ILogger log,
                                    IPEndPoint remoteEndPoint,
                                    Guid connectionId,
                                    bool ssl,
                                    bool validateServer,
                                    TimeSpan timeout,
                                    Action <TcpPackageConnection, TcpPackage> handlePackage,
                                    Action <TcpPackageConnection, Exception> onError,
                                    Action <TcpPackageConnection> connectionEstablished,
                                    Action <TcpPackageConnection, SocketError> connectionClosed)
        {
            Ensure.NotNull(log, "log");
            Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
            Ensure.NotEmptyGuid(connectionId, "connectionId");
            Ensure.NotNull(handlePackage, "handlePackage");

            ConnectionId   = connectionId;
            _log           = log;
            _handlePackage = handlePackage;
            _onError       = onError;

            //Setup callback for incoming messages
            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(IncomingMessageArrived);

            var connectionCreated = new ManualResetEventSlim();

// ReSharper disable ImplicitlyCapturedClosure
            _connection = Connector.ConnectTo(
                log,
                connectionId,
                remoteEndPoint,
                ssl,
                validateServer,
                timeout,
                tcpConnection => {
                connectionCreated.Wait();
                log.Debug("TcpPackageConnection: connected to [{0}, L{1}, {2:B}].", tcpConnection.RemoteEndPoint,
                          tcpConnection.LocalEndPoint, connectionId);
                if (connectionEstablished != null)
                {
                    connectionEstablished(this);
                }
            },
                (conn, error) => {
                connectionCreated.Wait();
                log.Debug("TcpPackageConnection: connection to [{0}, L{1}, {2:B}] failed. Error: {3}.",
                          conn.RemoteEndPoint, conn.LocalEndPoint, connectionId, error);
                if (connectionClosed != null)
                {
                    connectionClosed(this, error);
                }
            },
                (conn, error) => {
                connectionCreated.Wait();
                log.Debug("TcpPackageConnection: connection [{0}, L{1}, {2:B}] was closed {3}", conn.RemoteEndPoint,
                          conn.LocalEndPoint,
                          ConnectionId, error == SocketError.Success ? "cleanly." : "with error: " + error + ".");

                if (connectionClosed != null)
                {
                    connectionClosed(this, error);
                }
            });
// ReSharper restore ImplicitlyCapturedClosure

            connectionCreated.Set();
        }