Exemplo n.º 1
0
        public static async Task Accept(SolarWindHubOptions options, Socket socket, Action <HubId, Connection> onAccepted)
        {
            ConfigureSocket(socket, options);
            var networkStream = new AsyncNetworkStream(socket);
            //SslStream sslStream = null;

            //if (certificate != null)
            //{
            //    sslStream = new SslStream(networkStream, false);
            //    await sslStream
            //        .AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls12, true)
            //        .ConfigureAwait(false);

            //    if (!sslStream.IsAuthenticated)
            //    {
            //        sslStream.Close();
            //        return null;
            //    }
            //}


            HandshakeMessage incoming = await networkStream
                                        .HandshakeAsServer(options.HubId)
                                        .ConfigureAwait(false);

            var connection = new Connection(networkStream, options.LoggerFactory.CreateLogger <Connection>());

            onAccepted(incoming.HubId, connection);
        }
Exemplo n.º 2
0
        private Connection(AsyncNetworkStream networkStream, ILogger logger)
        {
            _networkStream = networkStream ?? throw new ArgumentNullException(nameof(networkStream));
            _logger        = logger ?? throw new ArgumentNullException(nameof(logger));

            _mainStream = new DuplexBufferedStream(_networkStream);
        }
        public static async Task <HandshakeMessage> HandshakeAsClient(this AsyncNetworkStream stream, HubId hubId)
        {
            await stream
            .SendHandshake(hubId)
            .ConfigureAwait(false);

            HandshakeMessage handshakeResponse = await stream
                                                 .ReceiveHandshake()
                                                 .ConfigureAwait(false);

            return(handshakeResponse);
        }
Exemplo n.º 4
0
        public static async void ConnectTo(SolarWindHubOptions options, Uri remoteUri, ILogger logger, Action <Uri, HubId, Connection> onConnected)
        {
            Socket socket = Build.TcpIPv4();

            ConfigureSocket(socket, options);

            while (true)
            {
                try
                {
                    await socket
                    .ConnectAsync(remoteUri.ResolveRemoteEndpoint())
                    .ConfigureAwait(false);

                    break;
                }
                catch (SocketException e)
                {
                    if (logger.IsEnabled(LogLevel.Trace))
                    {
                        logger.LogTrace(e, $"Cannot connect to '{remoteUri}");
                    }

                    await Task.Delay(5000).ConfigureAwait(false);
                }
            }


            var networkStream = new AsyncNetworkStream(socket);
            //SslStream sslStream = null;
            //if (remoteUri.UseTls())
            //{
            //    sslStream = new SslStream(networkStream, false);
            //    await sslStream
            //        .AuthenticateAsClientAsync(remoteUri.Host)
            //        .ConfigureAwait(false);

            //    if (!sslStream.IsAuthenticated)
            //    {
            //        sslStream.Close();
            //        return null;
            //    }
            //}

            HandshakeMessage handshakeResponse = await networkStream
                                                 .HandshakeAsClient(options.HubId)
                                                 .ConfigureAwait(false);

            var connection = new Connection(networkStream, logger);

            onConnected(remoteUri, handshakeResponse.HubId, connection);
        }
        internal static async Task <HandshakeMessage> HandshakeAsServer(this AsyncNetworkStream stream, HubId hubId)
        {
            HandshakeMessage incoming = await stream
                                        .ReceiveHandshake()
                                        .ConfigureAwait(false);

            if (incoming == null)
            {
                return(null);
            }

            await stream
            .SendHandshake(hubId)
            .ConfigureAwait(false);

            return(incoming);
        }