Exemplo n.º 1
0
        private void OnGivenHandshake(IAsyncResult asyncResult)
        {
            var state = (GivingHandshakeState)asyncResult.AsyncState;
            var size  = state.WebSocket.EndReceive(asyncResult);

            if (size < 1)
            {
                if (Log.IsDebugEnabled)
                {
                    Log.Debug("No server handshake data received from " + state.WebSocket.LocalEndPoint);
                }
                state.WebSocket.Dispose();
                return;
            }

            var clientHandshake = state.Handshake;
            var serverHandshake = new ServerHandshake();

            serverHandshake.Parse(state.Buffer, 0, size);
            var expected = serverHandshake.GenerateResponse(clientHandshake.Key1, clientHandshake.Key2, clientHandshake.Key3);
            var location = string.Concat(uri.Scheme, Uri.SchemeDelimiter, clientHandshake.Host, clientHandshake.ResourceName);

            if (serverHandshake.IsValid(location, clientHandshake.Origin, clientHandshake.Subprotocol, expected))
            {
                state.Callback();
            }
            else
            {
                state.WebSocket.Dispose();
            }
        }
Exemplo n.º 2
0
        private void ReturnHandshake(ServerHandshake serverHandshake, ClientHandshake clientHandshake, ReceiveHandshakeState handshakeState)
        {
            var webSocket = handshakeState.WebSocket;
            var state     = new ReturnHandshakeState {
                WebSocket = webSocket, Callback = handshakeState.Callback, ClientHandshake = clientHandshake
            };
            var handshakeBuffer = serverHandshake.ToByteArray();

            webSocket.BeginSend(handshakeBuffer, 0, handshakeBuffer.Length, OnHandshakeReturned, state);
        }
Exemplo n.º 3
0
        private void OnReceivingHandshake(IAsyncResult asyncResult)
        {
            var state = (ReceiveHandshakeState)asyncResult.AsyncState;
            var size  = state.WebSocket.EndReceive(asyncResult);

            if (size < 1)
            {
                if (Log.IsDebugEnabled)
                {
                    Log.Debug("No client handshake data received from " + state.WebSocket.LocalEndPoint);
                }
                state.WebSocket.Dispose();
                return;
            }

            var clientHandshake = new ClientHandshake();

            clientHandshake.Parse(state.Buffer, 0, size);
            if (clientHandshake.IsValid(host, origin))
            {
                var serverHandshake = new ServerHandshake(clientHandshake.Origin,
                                                          string.Concat(scheme, Uri.SchemeDelimiter, clientHandshake.Host, clientHandshake.ResourceName),
                                                          clientHandshake.Subprotocol,
                                                          clientHandshake.Key1, clientHandshake.Key2, clientHandshake.Key3);

                ReturnHandshake(serverHandshake, clientHandshake, state);
            }
            else
            {
                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Invalid client handshake from " + state.WebSocket.LocalEndPoint);
                }
                state.WebSocket.Dispose();
            }
        }