Exemplo n.º 1
0
        async Task ProcessTcpClient(TcpClient tcpClient, CancellationToken token)
        {
            try
            {
                // this worker thread stays alive until either of the following happens:
                // Client sends a close conection request OR
                // An unhandled exception is thrown OR
                // The server is disposed

                // get a secure or insecure stream
                Stream stream = tcpClient.GetStream();
                if (_secure)
                {
                    SslStream sslStream = new SslStream(stream, false, CertVerificationCallback);
                    sslStream.AuthenticateAsServer(_sslConfig.Certificate, _sslConfig.ClientCertificateRequired, _sslConfig.EnabledSslProtocols, _sslConfig.CheckCertificateRevocation);
                    stream = sslStream;
                }
                WebSocketHttpContext context = await webSocketServerFactory.ReadHttpHeaderFromStreamAsync(tcpClient, stream, token);

                if (context.IsWebSocketRequest)
                {
                    WebSocketServerOptions options = new WebSocketServerOptions()
                    {
                        KeepAliveInterval = TimeSpan.FromSeconds(30), SubProtocol = "binary"
                    };

                    WebSocket webSocket = await webSocketServerFactory.AcceptWebSocketAsync(context, options);

                    await ReceiveLoopAsync(webSocket, token);
                }
                else
                {
                    Debug.Log("Http header contains no web socket upgrade request. Ignoring");
                }
            }
            catch (IOException)
            {
                // do nothing. This will be thrown if the transport is closed
            }
            catch (ObjectDisposedException)
            {
                // do nothing. This will be thrown if the Listener has been stopped
            }
            catch (Exception ex)
            {
                ReceivedError?.Invoke(0, ex);
            }
            finally
            {
                try
                {
                    tcpClient.Client.Close();
                    tcpClient.Close();
                }
                catch (Exception ex)
                {
                    ReceivedError?.Invoke(0, ex);
                }
            }
        }
Exemplo n.º 2
0
        public override async Task <IConnection> AcceptAsync()
        {
            try
            {
                TcpClient tcpClient = await listener.AcceptTcpClientAsync();

                var options = new WebSocketServerOptions {
                    KeepAliveInterval = TimeSpan.FromSeconds(30), SubProtocol = "binary"
                };

                Stream stream = tcpClient.GetStream();

                WebSocketHttpContext context = await webSocketServerFactory.ReadHttpHeaderFromStreamAsync(tcpClient, stream);

                WebSocket webSocket = await webSocketServerFactory.AcceptWebSocketAsync(context, options);

                return(new WebsocketConnection(webSocket));
            }
            catch (ObjectDisposedException)
            {
                // expected,  the connection was closed
                return(null);
            }
        }
Exemplo n.º 3
0
        private async Task ProcessTcpClientAsync(TcpClient tcpClient)
        {
            CancellationTokenSource source = new CancellationTokenSource();

            if (_isDisposed)
            {
                return;
            }

            // this worker thread stays alive until either of the following happens:
            // Client sends a close conection request OR
            // An unhandled exception is thrown OR
            // The server is disposed

            // get a secure or insecure stream
            Stream stream = tcpClient.GetStream();

            if (Secure)
            {
                var sslStream = new SslStream(stream, false, CertVerificationCallback);
                await sslStream.AuthenticateAsServerAsync(_sslConfig.Certificate, _sslConfig.ClientCertificateRequired, _sslConfig.EnabledSslProtocols, _sslConfig.CheckCertificateRevocation);

                stream = sslStream;
            }
            WebSocketHttpContext context = await _webSocketServerFactory.ReadHttpHeaderFromStreamAsync(stream);

            if (context.IsWebSocketRequest)
            {
                if (context.Path == _GamePath)
                {
                    string subProtocol = GetSubProtocol(context.WebSocketRequestedProtocols);
                    var    options     = new WebSocketServerOptions()
                    {
                        KeepAliveInterval = TimeSpan.FromSeconds(30), SubProtocol = subProtocol
                    };

                    WebSocket webSocket = await _webSocketServerFactory.AcceptWebSocketAsync(context, options);

                    var newClient = Server.AddClient(new ClientContainer(System.Guid.NewGuid().ToString(), webSocket));
                    await newClient.ClientProcess();
                }
                else
                {
                    Console.WriteLine("path not match");
                }
            }
            else
            {
            }

            //}
            //catch (ObjectDisposedException)
            //{
            //    // do nothing. This will be thrown if the Listener has been stopped
            //}
            //finally
            //{
            //    try
            //    {
            //        tcpClient.Client.Close();
            //        tcpClient.Close();
            //        source.Cancel();
            //    }
            //    catch (Exception ex)
            //    {
            //    }
            //}
        }
Exemplo n.º 4
0
        private async Task ProcessTcpClientAsync(TcpClient tcpClient)
        {
            CancellationTokenSource source = new CancellationTokenSource();

            try
            {
                if (_isDisposed)
                {
                    return;
                }

                // this worker thread stays alive until either of the following happens:
                // Client sends a close conection request OR
                // An unhandled exception is thrown OR
                // The server is disposed
                Console.WriteLine("Server: Connection opened. Reading Http header from stream");

                // get a secure or insecure stream
                Stream stream = tcpClient.GetStream();
                WebSocketHttpContext context = await _webSocketServerFactory.ReadHttpHeaderFromStreamAsync(stream);

                if (context.IsWebSocketRequest)
                {
                    // disable ping pong for now (it is causing multi-threaded issues)
                    var options = new WebSocketServerOptions()
                    {
                        KeepAliveInterval = TimeSpan.Zero
                    };
                    Console.WriteLine("Http header has requested an upgrade to Web Socket protocol. Negotiating Web Socket handshake");
                    WebSocket webSocket = await _webSocketServerFactory.AcceptWebSocketAsync(context, options);

                    Console.WriteLine("Web Socket handshake response sent. Stream ready.");
                    await RespondToWebSocketRequestAsync(webSocket, source.Token);
                }
                else
                {
                    Console.WriteLine("Http header contains no web socket upgrade request. Ignoring");
                }

                Console.WriteLine("Server: Connection closed");
            }
            catch (ObjectDisposedException)
            {
                // do nothing. This will be thrown if the Listener has been stopped
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                try
                {
                    tcpClient.Client.Close();
                    tcpClient.Close();
                    source.Cancel();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to close TCP connection: {ex}");
                }
            }
        }
Exemplo n.º 5
0
        private async Task ProcessTcpClientAsync(TcpClient tcpClient)
        {
            CancellationTokenSource source = new CancellationTokenSource();

            try
            {
                if (_isDisposed)
                {
                    return;
                }
                // this worker thread stays alive until either of the following happens:
                // Client sends a close conection request OR
                // An unhandled exception is thrown OR
                // The server is disposed
                WSLog.Info("<Server>: Connection opened. Reading Http header from stream");
                WSLog.Info("-----------------------------------------------------------");

                // get a secure or insecure stream
                Stream stream = tcpClient.GetStream();
                WebSocketHttpContext context = await _webSocketServerFactory.ReadHttpHeaderFromStreamAsync(stream);

                if (context.IsWebSocketRequest)
                {
                    string subProtocol = GetSubProtocol(context.WebSocketRequestedProtocols);
                    var    options     = new WebSocketServerOptions()
                    {
                        KeepAliveInterval = TimeSpan.FromSeconds(30), SubProtocol = subProtocol
                    };
                    WSLog.Info("<Message>: Http header has requested an upgrade to Web Socket protocol. Negotiating Web Socket handshake");

                    WebSocket webSocket = await _webSocketServerFactory.AcceptWebSocketAsync(context, options);

                    WSLog.Info("<Message>: Web Socket handshake response sent. Stream ready.");
                    await RespondToWebSocketRequestAsync(webSocket, source.Token);
                }
                else
                {
                    WSLog.Info("<Message>: Http header contains no web socket upgrade request. Ignoring");
                }

                // closed connection
                WSLog.Info("-----------------------------------------------------------");
                WSLog.Info("<Server>: Connection closed.");
            }
            catch (ObjectDisposedException)
            {
                // do nothing. This will be thrown if the Listener has been stopped
            }
            catch (Exception ex)
            {
                WSLog.Error(ex.ToString());
            }
            finally
            {
                try
                {
                    tcpClient.Client.Close();
                    tcpClient.Close();
                    source.Cancel();
                }
                catch (Exception ex)
                {
                    WSLog.Error($"<Error>: failed to close TCP connection: {ex}");
                }
            }
        }