예제 #1
0
        protected virtual async Task ListenForConnectionsSSLAsync()
        {
            while (_isRunning)
            {
                try
                {
                    var client = await _server.AcceptTcpClientAsync();

                    var sslStream = new SslStream(client.GetStream());
                    await sslStream.AuthenticateAsServerAsync(new X509Certificate2(_certificate, _certificatePassword));

                    if (sslStream.IsAuthenticated && sslStream.IsEncrypted)
                    {
                        var reader = new StreamReader(sslStream);
                        var writer = new StreamWriter(sslStream)
                        {
                            AutoFlush = true,
                            NewLine   = _parameters.EndOfLineCharacters
                        };

                        var connection = new ConnectionTcpServer
                        {
                            Client       = client,
                            Reader       = reader,
                            Writer       = writer,
                            ConnectionId = Guid.NewGuid().ToString()
                        };

                        await FireEventAsync(this, new TcpConnectionServerEventArgs
                        {
                            ConnectionEventType = ConnectionEventType.Connected,
                            Connection          = connection,
                        });

                        _numberOfConnections++;

                        _ = Task.Run(async() => { await StartListeningForMessagesAsync(connection); });
                    }
                    else
                    {
                        var certStatus = $"IsAuthenticated = {sslStream.IsAuthenticated} && IsEncripted == {sslStream.IsEncrypted}";
                        await FireEventAsync(this, new TcpErrorServerEventArgs
                        {
                            Exception = new Exception(certStatus),
                            Message   = certStatus
                        });

                        client.Close();
                    }
                }
                catch (Exception ex)
                {
                    await FireEventAsync(this, new TcpErrorServerEventArgs
                    {
                        Exception = ex,
                        Message   = ex.Message,
                    });
                }
            }
        }
예제 #2
0
        protected virtual async Task ListenForConnectionsAsync()
        {
            while (_isRunning)
            {
                try
                {
                    var client = await _server.AcceptTcpClientAsync();

                    var stream = client.GetStream();
                    var reader = new StreamReader(stream);
                    var writer = new StreamWriter(stream)
                    {
                        AutoFlush = true,
                        NewLine   = _parameters.EndOfLineCharacters
                    };

                    var connection = new ConnectionTcpServer
                    {
                        Client       = client,
                        Reader       = reader,
                        Writer       = writer,
                        ConnectionId = Guid.NewGuid().ToString()
                    };

                    await FireEventAsync(this, new TcpConnectionServerEventArgs
                    {
                        ConnectionEventType = ConnectionEventType.Connected,
                        Connection          = connection,
                    });

                    _ = Task.Run(async() => { await StartListeningForMessagesAsync(connection); });
#pragma warning restore CS4014 // Becaue this call is not awaited, execution of the current method continues before the call is completed

                    _numberOfConnections++;
                }
                catch (Exception ex)
                {
                    await FireEventAsync(this, new TcpErrorServerEventArgs
                    {
                        Exception = ex,
                        Message   = ex.Message,
                    });
                }
            }
        }