Beispiel #1
0
 internal Task SendAsync(string line)
 {
     try
     {
         if (connection == null)
         {
             Console.WriteLine($"[client] (no connection; cannot send)");
             return(done);
         }
         else if (string.IsNullOrEmpty(line))
         {
             return(done);
         }
         else
         {
             var buffer = connection.Output.Alloc();
             Console.WriteLine($"[client] sending {line.Length} bytes...");
             WritableBufferExtensions.WriteAsciiString(ref buffer, line);
             return(buffer.FlushAsync());
         }
     }
     catch (Exception ex)
     {
         Program.WriteError(ex);
         return(done);
     }
 }
        public void CopyTo(bool chunk, ref WritableBuffer buffer)
        {
            foreach (var header in _headers)
            {
                buffer.Write(_headersStartBytes.Slice());
                WritableBufferExtensions.WriteAsciiString(ref buffer, header.Key);
                buffer.Write(_headersSeperatorBytes.Slice());
                WritableBufferExtensions.WriteAsciiString(ref buffer, header.Value);
            }

            if (chunk)
            {
                buffer.Write(_chunkedHeaderBytes.Slice());
            }

            buffer.Write(_serverHeaderBytes.Slice());
            var date = _dateHeaderValueManager.GetDateHeaderValues().Bytes;

            buffer.Write(date.Slice());

            buffer.Write(_headersEndBytes.Slice());
        }
        internal static Task CompleteHandshakeAsync(ref HttpRequest request, WebSocketConnection socket)
        {
            var key = request.Headers.GetRaw("Sec-WebSocket-Key");

            var connection = socket.Connection;

            const int ResponseTokenLength = 28;

            var buffer = connection.Output.Alloc(StandardPrefixBytes.Length +
                                                 ResponseTokenLength + StandardPostfixBytes.Length);
            string hashBase64 = ComputeReply(key, buffer.Memory);

            if (hashBase64.Length != ResponseTokenLength)
            {
                throw new InvalidOperationException("Unexpected response key length");
            }
            WebSocketServer.WriteStatus($"Response token: {hashBase64}");

            buffer.Write(StandardPrefixBytes, 0, StandardPrefixBytes.Length);
            WritableBufferExtensions.WriteAsciiString(ref buffer, hashBase64);
            buffer.Write(StandardPostfixBytes, 0, StandardPostfixBytes.Length);

            return(buffer.FlushAsync());
        }