Пример #1
0
        public IService CreateInstance(ConnectionDetails connectionDetails, bool isLowLevel = false)
        {
            switch (connectionDetails.ConnectionType)
            {
            case ConnectionType.WebSocket:
                _logger.DebugFormat("creating WebSocketService {0}", connectionDetails.Path);
                if (!isLowLevel)
                {
                    return(new WebSocketService(connectionDetails.Stream, connectionDetails.TcpClient, connectionDetails.Header, true, _logger));
                }
                else
                {
                    return(new WebSocketHandlerService(connectionDetails.Stream, connectionDetails.TcpClient, connectionDetails.Header, true, _logger));
                }
                break;

            case ConnectionType.Http:
                // this path actually refers to the reletive location of some html file or image
                return(new HttpService(connectionDetails.Stream, connectionDetails.Path, _webRoot, _logger));
            }

            return(new BadRequestService(connectionDetails.Stream, connectionDetails.Header, _logger));
        }
Пример #2
0
        private void HandleAsyncConnection(IAsyncResult res)
        {
            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
                try
                {
                    //using (
                    TcpClient tcpClient = _mainSocket.EndAcceptTcpClient(res);
                    //)
                    {
                        if (ConnectionCount < MaxConnections)
                        {
                            StartAccept();
                        }
                        _logger.Info("WebSocketDaemonSB opened client connection");

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

                        // extract the connection details and use those details to build a connection
                        ConnectionDetails connectionDetails = GetConnectionDetails(stream, tcpClient);
                        //using (
                        IService service = _serviceFactory.CreateInstance(connectionDetails, true);
                        //  )
                        {
                            // respond to the http request.
                            // Take a look at the WebSocketConnection or HttpConnection classes

                            WebSocketHandlerService sh = service as WebSocketHandlerService;
                            if (sh != null)
                            {
                                sh.ChannelParameters       = _channelParameters;
                                sh.WebSocketHandShakeSent += sh_WebSocketHandShakeSent;
                            }

                            service.Respond();

                            _logger.DebugFormat("Service responded {0} ", connectionDetails.ConnectionType);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Error in HandleAsyncConnection", ex);
                }
            }
            catch (ObjectDisposedException)
            {
                // do nothing. This will be thrown if the Listener has been stopped
            }
            catch (Exception ex)
            {
                _logger.Error("Error in HandleAsyncConnection", ex);
            }
        }