Ejemplo n.º 1
0
        /// <summary>
        /// Constructor for incoming connections from clients
        /// </summary>
        /// <param name="client"></param>
        internal Connection(ITcpClient client, SslSettings sslSettings) : this(sslSettings)
        {
            // set random ID and connection direction
            ID       = Guid.NewGuid().ToString().Substring(0, 8);
            IsClient = false;

            _tcpClient     = client;
            _networkStream = _tcpClient.GetStream();
            _networkStream.AuthenticateAsServer();

            // create session ID provider - IDs are ODD for server-side initiated sessions
            _sessionIdProvider = new IdProvider(1, 2);

            // create and start frame decoder
            _streamDecoderTask = _decodeStreamAsync(_networkStream, _cts.Token);

            // block until the connection is active or it timesout
            var index = WaitHandle.WaitAny(new[] { _connectionOpen, _connectionError }, 2000);

            switch (index)
            {
            case 0: return;

            case 1: throw new ConnectionException();

            case WaitHandle.WaitTimeout: throw new ConnectionTimeoutException();

            default: throw new Exception();
            }
        }