Ejemplo n.º 1
0
        private async Task <bool> OpenHandshake()
        {
            bool handshakeResult = false;

            try
            {
                int terminatorIndex = -1;
                while (!WebSocketHelpers.FindHeaderTerminator(_receiveBuffer, _receiveBufferOffset, out terminatorIndex))
                {
                    int receiveCount = await _stream.ReadAsync(_receiveBuffer, _receiveBufferOffset, _receiveBuffer.Length - _receiveBufferOffset);

                    if (receiveCount == 0)
                    {
                        throw new WebSocketHandshakeException(string.Format(
                                                                  "Handshake with remote [{0}] failed due to receive zero bytes.", RemoteEndPoint));
                    }

                    BufferDeflector.ReplaceBuffer(_bufferManager, ref _receiveBuffer, ref _receiveBufferOffset, receiveCount);

                    if (_receiveBufferOffset > 2048)
                    {
                        throw new WebSocketHandshakeException(string.Format(
                                                                  "Handshake with remote [{0}] failed due to receive weird stream.", RemoteEndPoint));
                    }
                }

                string secWebSocketKey = string.Empty;
                string path            = string.Empty;
                string query           = string.Empty;
                handshakeResult = WebSocketServerHandshaker.HandleOpenningHandshakeRequest(this,
                                                                                           _receiveBuffer, 0, terminatorIndex + Consts.HeaderTerminator.Length,
                                                                                           out secWebSocketKey, out path, out query);

                _module = _routeResolver.Resolve(path, query);
                if (_module == null)
                {
                    throw new WebSocketHandshakeException(string.Format(
                                                              "Handshake with remote [{0}] failed due to cannot identify the resource name [{1}{2}].", RemoteEndPoint, path, query));
                }

                if (handshakeResult)
                {
                    var responseBuffer = WebSocketServerHandshaker.CreateOpenningHandshakeResponse(this, secWebSocketKey);
                    await _stream.WriteAsync(responseBuffer, 0, responseBuffer.Length);
                }

                BufferDeflector.ShiftBuffer(_bufferManager, terminatorIndex + Consts.HeaderTerminator.Length, ref _receiveBuffer, ref _receiveBufferOffset);
            }
            catch (ArgumentOutOfRangeException)
            {
                handshakeResult = false;
            }
            catch (WebSocketHandshakeException ex)
            {
                _log.Error(string.Format("Session [{0}] exception occurred, [{1}].", this, ex.Message), ex);
                handshakeResult = false;
            }

            return(handshakeResult);
        }
Ejemplo n.º 2
0
        private bool OpenHandshake()
        {
            bool handshakeResult = false;

            try
            {
                var requestBuffer = WebSocketClientHandshaker.CreateOpenningHandshakeRequest(this, out _secWebSocketKey);
                var ar            = _stream.BeginWrite(requestBuffer, 0, requestBuffer.Length, null, _stream);
                if (!ar.AsyncWaitHandle.WaitOne(ConnectTimeout))
                {
                    Close(WebSocketCloseCode.ProtocolError, "Opening handshake timeout.");
                    throw new TimeoutException(string.Format(
                                                   "Handshake with remote [{0}] timeout [{1}].", RemoteEndPoint, ConnectTimeout));
                }
                _stream.EndWrite(ar);

                int terminatorIndex = -1;
                while (!WebSocketHelpers.FindHttpMessageTerminator(_receiveBuffer, _receiveBufferOffset, out terminatorIndex))
                {
                    ar = _stream.BeginRead(_receiveBuffer, _receiveBufferOffset, _receiveBuffer.Length - _receiveBufferOffset, null, _stream);
                    if (!ar.AsyncWaitHandle.WaitOne(ConnectTimeout))
                    {
                        Close(WebSocketCloseCode.ProtocolError, "Opening handshake timeout.");
                        throw new TimeoutException(string.Format(
                                                       "Handshake with remote [{0}] timeout [{1}].", RemoteEndPoint, ConnectTimeout));
                    }

                    int receiveCount = _stream.EndRead(ar);
                    if (receiveCount == 0)
                    {
                        throw new WebSocketHandshakeException(string.Format(
                                                                  "Handshake with remote [{0}] failed due to receive zero bytes.", RemoteEndPoint));
                    }

                    BufferDeflector.ReplaceBuffer(_bufferManager, ref _receiveBuffer, ref _receiveBufferOffset, receiveCount);

                    if (_receiveBufferOffset > 2048)
                    {
                        throw new WebSocketHandshakeException(string.Format(
                                                                  "Handshake with remote [{0}] failed due to receive weird stream.", RemoteEndPoint));
                    }
                }

                handshakeResult = WebSocketClientHandshaker.VerifyOpenningHandshakeResponse(this, _receiveBuffer, 0, terminatorIndex + Consts.HttpMessageTerminator.Length, _secWebSocketKey);

                BufferDeflector.ShiftBuffer(_bufferManager, terminatorIndex + Consts.HttpMessageTerminator.Length, ref _receiveBuffer, ref _receiveBufferOffset);
            }
            catch (ArgumentOutOfRangeException)
            {
                handshakeResult = false;
            }
            catch (WebSocketHandshakeException ex)
            {
                _log(ex.Message);
                handshakeResult = false;
            }

            return(handshakeResult);
        }