public static ITcpConnection CreateConnectingConnection(Guid connectionId,
                                                                IPEndPoint remoteEndPoint,
                                                                string targetHost,
                                                                bool validateServer,
                                                                TcpClientConnector connector,
                                                                TimeSpan connectionTimeout,
                                                                Action <ITcpConnection> onConnectionEstablished,
                                                                Action <ITcpConnection, SocketError> onConnectionFailed,
                                                                bool verbose)
        {
            var connection = new TcpConnectionSsl(connectionId, remoteEndPoint, verbose);

            // ReSharper disable ImplicitlyCapturedClosure
            connector.InitConnect(remoteEndPoint,
                                  (_, socket) =>
            {
                connection.InitClientSocket(socket, targetHost, validateServer, verbose);
                if (onConnectionEstablished != null)
                {
                    onConnectionEstablished(connection);
                }
            },
                                  (_, socketError) =>
            {
                if (onConnectionFailed != null)
                {
                    onConnectionFailed(connection, socketError);
                }
            }, connection, connectionTimeout);
            // ReSharper restore ImplicitlyCapturedClosure
            return(connection);
        }
        public static ITcpConnection CreateServerFromSocket(Guid connectionId,
                                                            IPEndPoint remoteEndPoint,
                                                            Socket socket,
                                                            X509Certificate certificate,
                                                            bool verbose)
        {
            Ensure.NotNull(certificate, "certificate");
            var connection = new TcpConnectionSsl(connectionId, remoteEndPoint, verbose);

            connection.InitServerSocket(socket, certificate, verbose);
            return(connection);
        }
        public static ITcpConnection CreateClientFromSocket(Guid connectionId,
                                                            IPEndPoint remoteEndPoint,
                                                            Socket socket,
                                                            string targetHost,
                                                            bool validateServer,
                                                            bool verbose)
        {
            var connection = new TcpConnectionSsl(connectionId, remoteEndPoint, verbose);

            connection.InitClientSocket(socket, targetHost, validateServer, verbose);
            return(connection);
        }
 public ITcpConnection ConnectSslTo(Guid connectionId,
                                    IPEndPoint remoteEndPoint,
                                    TimeSpan connectionTimeout,
                                    string targetHost,
                                    bool validateServer,
                                    Action <ITcpConnection> onConnectionEstablished         = null,
                                    Action <ITcpConnection, SocketError> onConnectionFailed = null,
                                    bool verbose = true)
 {
     Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
     Ensure.NotNullOrEmpty(targetHost, "targetHost");
     return(TcpConnectionSsl.CreateConnectingConnection(connectionId, remoteEndPoint, targetHost, validateServer,
                                                        this, connectionTimeout, onConnectionEstablished, onConnectionFailed, verbose));
 }
Beispiel #5
0
        private void OnConnectionAccepted(IPEndPoint endPoint, Socket socket)
        {
            var conn = _securityType == TcpSecurityType.Secure
                ? TcpConnectionSsl.CreateServerFromSocket(Guid.NewGuid(), endPoint, socket, _certificate, verbose: true)
                : TcpConnection.CreateAcceptedTcpConnection(Guid.NewGuid(), endPoint, socket, verbose: true);

            Log.Info("TCP connection accepted: [{0}, {1}, L{2}, {3:B}].", _securityType, conn.RemoteEndPoint, conn.LocalEndPoint, conn.ConnectionId);

            var manager = new TcpConnectionManager(
                string.Format("{0}", _securityType.ToString().ToLower()),
                conn,
                new TFramer(),
                (m, d) => MessageArrived(this, new FramedMessageArrivedEventArgs(m, d)),
                (m, e) => ConnectionClosed(this, new ConnectionClosedEventArgs(m, e)));

            ConnectionEstablished(this, new ConnectionEstablishedEventArgs(manager));
            manager.StartReceiving();
        }