Exemple #1
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);
        }
Exemple #2
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();
            }
        }
Exemple #3
0
        //public ClientEtiquette(Uri remoteUri, string origin, HttpCookieCollection cookies, IDictionary<string, string> extraFields)
        //    : this(remoteUri, origin)
        //{
        //    this.cookies = cookies;
        //    this.extraFields = extraFields;
        //}

        public void GiveHandshake(IWebSocket webSocket, Action handShakenCallback)
        {
            if (webSocket == null || !webSocket.IsConnected || handShakenCallback == null)
            {
                return;
            }

            var resourceName = uri.PathAndQuery;
            var host         = uri.WebSocketAuthority();
            var handshake    = new ClientHandshake(resourceName, host, origin)
            {
                ExtraFields = extraFields,
                Cookies     = cookies
            };
            var state = new GiveHandshakeState
            {
                WebSocket = webSocket,
                Callback  = handShakenCallback,
                Handshake = handshake
            };
            var handshakeBuffer = handshake.ToByteArray();

            webSocket.BeginSend(handshakeBuffer, 0, handshakeBuffer.Length, OnGivingHandshake, state);
        }