private static void HandleHttpRequest(WebSocketsClient client, Dictionary <string, string> headers) { //var uri = headers["GET"]; //var origin = headers["Origin"]; var key = client.ResolveKey(headers["Sec-WebSocket-Key"]); var answer = Convert.ToBase64String(Encoding.ASCII.GetBytes(key.Value)); var sendHeaders = new List <string> { "HTTP/1.1 101 Switching Protocols", // "Connection: Upgrade", // "Sec-WebSocket-Accept: " + answer, // "Upgrade: websocket" // }; Send(client, String.Join("\r\n", sendHeaders) + "\r\n\r\n", false, false, (c, d) => client.Start()); if (!CMOptions.ModuleDebug) { return; } CMOptions.ToConsole("HEADERS>>>\n"); CMOptions.ToConsole(sendHeaders.ToArray()); }
private static void Send( WebSocketsClient client, string data, bool encode, bool compress, Action <WebSocketsClient, byte[]> callback) { VitaNexCore.TryCatch( () => { int len; byte[] buffer; if (encode) { Encode(data, out buffer, out len); } else { buffer = data.Select(c => (byte)c).ToArray(); len = buffer.Length; } Send(client, buffer, len, compress, callback); }, CMOptions.ToConsole); }
private static void Send( WebSocketsClient client, byte[] buffer, int len, bool compress, Action <WebSocketsClient, byte[]> callback) { var stream = client.TcpClient.GetStream(); if (compress) { Compress(ref buffer, ref len); } var count = 0; while (count < len) { var block = buffer.Skip(count).Take(client.TcpClient.SendBufferSize).ToArray(); stream.Write(block, 0, block.Length); count += block.Length; } if (callback != null) { callback(client, buffer); } }
private static void Connected(WebSocketsClient client) { lock (Clients) { if (!Clients.Contains(client)) { Clients.Add(client); } } CMOptions.ToConsole("[{0}] Client connected: {1}", Clients.Count, client.Address); if (OnConnected != null) { VitaNexCore.TryCatch( () => OnConnected(client), e => { CMOptions.ToConsole(e); client.Dispose(); Disconnected(client); }); } }
private static void Disconnected(WebSocketsClient client) { if (OnDisconnected != null) { VitaNexCore.TryCatch(() => OnDisconnected(client), CMOptions.ToConsole); } lock (Clients) { Clients.Remove(client); } CMOptions.ToConsole("[{0}] Client disconnected: {1}", Clients.Count, client.Address); client.Dispose(); }
private static void Receive( WebSocketsClient client, bool decompress, bool decode, Action <WebSocketsClient, string, byte[]> callback) { VitaNexCore.TryCatch( () => { var stream = client.TcpClient.GetStream(); var buffer = new byte[client.TcpClient.ReceiveBufferSize]; var len = buffer.Length; stream.Read(buffer, 0, buffer.Length); if (decompress) { Decompress(ref buffer, ref len); } string data; if (decode) { Decode(buffer, out data); } else { data = new String(buffer.Select(b => (char)b).ToArray()); } if (callback != null) { callback(client, data, buffer); } }, CMOptions.ToConsole); }
private static void HandleConnection(WebSocketsClient client) { VitaNexCore.TryCatch( () => { if (client.Seeded) { return; } var headers = new Dictionary <string, string>(); Receive( client, false, true, (c, d, b) => { if (d.Length == 0) { return; } var lines = d.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); lines = lines.Take(lines.Length - 1).ToArray(); if (CMOptions.ModuleDebug) { CMOptions.ToConsole(lines.Not(String.IsNullOrWhiteSpace).ToArray()); } lines.ForEach( line => { line = line.Trim(); var header = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (header.Length == 0) { return; } var hk = header[0].Replace(":", String.Empty); if (String.IsNullOrWhiteSpace(hk)) { return; } var hv = header.Length > 1 ? String.Join(" ", header.Skip(1)) : String.Empty; if (!headers.ContainsKey(hk)) { headers.Add(hk, hv); } else { headers[hk] = hv; } }); }); if (headers.Count > 0) { HandleHttpRequest(client, headers); } else { throw new Exception("No headers defined for WebSockets client handshake.", new SocketException()); } }, CMOptions.ToConsole); }
private static void HandleConnection(WebSocketsClient client) { VitaNexCore.TryCatch( () => { if (client.Seeded) { return; } var headers = new Dictionary<string, string>(); Receive( client, false, true, (c, d, b) => { if (d.Length == 0) { return; } var lines = d.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); lines = lines.Take(lines.Length - 1).ToArray(); if (CMOptions.ModuleDebug) { CMOptions.ToConsole(lines.Not(String.IsNullOrWhiteSpace).ToArray()); } lines.ForEach( line => { line = line.Trim(); var header = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); if (header.Length == 0) { return; } var hk = header[0].Replace(":", String.Empty); if (String.IsNullOrWhiteSpace(hk)) { return; } var hv = header.Length > 1 ? String.Join(" ", header.Skip(1)) : String.Empty; if (!headers.ContainsKey(hk)) { headers.Add(hk, hv); } else { headers[hk] = hv; } }); }); if (headers.Count > 0) { HandleHttpRequest(client, headers); } else { throw new Exception("No headers defined for WebSockets client handshake.", new SocketException()); } }, CMOptions.ToConsole); }
private static void HandleHttpRequest(WebSocketsClient client, Dictionary<string, string> headers) { //var uri = headers["GET"]; //var origin = headers["Origin"]; var key = client.ResolveKey(headers["Sec-WebSocket-Key"]); var answer = Convert.ToBase64String(Encoding.ASCII.GetBytes(key.Value)); var sendHeaders = new List<string> { "HTTP/1.1 101 Switching Protocols", // "Connection: Upgrade", // "Sec-WebSocket-Accept: " + answer, // "Upgrade: websocket" // }; Send(client, String.Join("\r\n", sendHeaders) + "\r\n\r\n", false, false, (c, d) => client.Start()); if (!CMOptions.ModuleDebug) { return; } CMOptions.ToConsole("HEADERS>>>\n"); CMOptions.ToConsole(sendHeaders.ToArray()); }
private static void Receive( WebSocketsClient client, bool decompress, bool decode, Action<WebSocketsClient, string, byte[]> callback) { VitaNexCore.TryCatch( () => { var stream = client.TcpClient.GetStream(); var buffer = new byte[client.TcpClient.ReceiveBufferSize]; var len = buffer.Length; stream.Read(buffer, 0, buffer.Length); if (decompress) { Decompress(ref buffer, ref len); } string data; if (decode) { Decode(buffer, out data); } else { data = new String(buffer.Select(b => (char)b).ToArray()); } if (callback != null) { callback(client, data, buffer); } }, CMOptions.ToConsole); }
private static void Send( WebSocketsClient client, byte[] buffer, int len, bool compress, Action<WebSocketsClient, byte[]> callback) { var stream = client.TcpClient.GetStream(); if (compress) { Compress(ref buffer, ref len); } var count = 0; while (count < len) { var block = buffer.Skip(count).Take(client.TcpClient.SendBufferSize).ToArray(); stream.Write(block, 0, block.Length); count += block.Length; } if (callback != null) { callback(client, buffer); } }
private static void Send( WebSocketsClient client, string data, bool encode, bool compress, Action<WebSocketsClient, byte[]> callback) { VitaNexCore.TryCatch( () => { int len; byte[] buffer; if (encode) { Encode(data, out buffer, out len); } else { buffer = data.Select(c => (byte)c).ToArray(); len = buffer.Length; } Send(client, buffer, len, compress, callback); }, CMOptions.ToConsole); }