예제 #1
0
        public ServerHandshake GenerateResponseHandshake()
        {
            var responseHandshake = new ServerHandshake
            {
                Location = "ws://" + ClientHandshake.Host + ClientHandshake.ResourcePath,
                Origin = ClientHandshake.Origin,
                SubProtocol = ClientHandshake.SubProtocol
            };

            var challenge = new byte[8];
            Array.Copy(ClientHandshake.ChallengeBytes.Array, ClientHandshake.ChallengeBytes.Offset, challenge, 0, 8);

            responseHandshake.AnswerBytes =
                CalculateAnswerBytes(ClientHandshake.Key1, ClientHandshake.Key2, ClientHandshake.ChallengeBytes);

            return responseHandshake;
        }
예제 #2
0
        public void BeginSendServerHandshake(ServerHandshake handshake, ISocket socket)
        {
            string stringShake = handshake.ToResponseString();

            byte[] byteResponse = Encoding.UTF8.GetBytes(stringShake);
            int byteResponseLength = byteResponse.Length;
            Array.Resize(ref byteResponse, byteResponseLength + handshake.AnswerBytes.Length);
            Array.Copy(handshake.AnswerBytes, 0, byteResponse, byteResponseLength, handshake.AnswerBytes.Length);

            var segment = new ArraySegment<byte>(byteResponse);

            Task<int>.Factory.FromAsync(socket.BeginSend, socket.EndSend, new[] {segment}, SocketFlags.None, null)
                .ContinueWith(t => EndSendServerHandshake())
                .ContinueWith(t => FleckLog.Error("Send handshake failed", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
        }