private void StartAccept(SocketAsyncEventArgs e) { bool willRaiseEvent = true; if (e == null) { e = new SocketAsyncEventArgs(); e.Completed += ProcessAccept; } e.AcceptSocket = null; try { lock (this) { if (!SocketExtension.CleanedUp(server)) { willRaiseEvent = server.AcceptAsync(e); } } } catch (Exception) { /*-A-*/ } if (!willRaiseEvent) { ProcessAccept(server, e); } }
private bool HandleUpgradeResponse() { Func <string> acceptkey = () => { using (SHA1 sha1 = SHA1.Create()) { StringBuilder ss = new StringBuilder(); string key = _secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(key)); return(Convert.ToBase64String(hash)); } }; StringBuilder headers = new StringBuilder(); headers.Append("HTTP/1.1 101 Switching Protocols\r\n"); headers.Append("Upgrade: websocket\r\n"); headers.Append("Connection: Upgrade\r\n"); headers.AppendFormat("Sec-WebSocket-Accept: {0}\r\n", acceptkey()); headers.AppendFormat("Sec-WebSocket-Origin: {0}\r\n", _origin); headers.AppendFormat("Sec-WebSocket-Location: ws://{0}{1}\r\n\r\n", _host, _rawUri); byte[] response = Encoding.UTF8.GetBytes(headers.ToString()); lock (_signal) { if (!_socket.Connected || SocketExtension.CleanedUp(_socket)) { return(false); } return(SocketExtension.BeginSend(_socket, response, 0, response.Length, null)); } }
private static void Connected(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); SOCKET socket = GetSocket(arguments.This); if (socket == null || SocketExtension.CleanedUp(socket)) { Throwable.ObjectDisposedException(arguments.VirtualMachine); } else { arguments.SetReturnValue(socket.Connected); } }
private static void AcceptAsync(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; do { SocketContext context = GetSocketContext(arguments.This); if (context == null) { Throwable.ObjectDisposedException(arguments.VirtualMachine); break; } SOCKET socket = context.Socket; if (socket == null || SocketExtension.CleanedUp(socket)) { Throwable.ObjectDisposedException(arguments.VirtualMachine); break; } NSJSFunction callback = arguments.Length > 0 ? arguments[0] as NSJSFunction : null; try { SocketAsyncEventArgs e = context.AcceptAsync; if (e != null) { break; } e = new SocketAsyncEventArgs(); e.Completed += ProcessAccept; e.UserToken = context; context.AcceptAsync = e; if (callback != null) { NSJSObject socketobject = arguments.This; callback.CrossThreading = true; context.AcceptAsyncCallback = callback; } if (!socket.AcceptAsync(e)) { ProcessAccept(socket, e); } success = true; } catch (Exception e) { Throwable.Exception(arguments.VirtualMachine, e); } } while (false); arguments.SetReturnValue(success); }
private int Receive(int ofs, int len) { try { lock (_signal) { if (!_socket.Connected || SocketExtension.CleanedUp(_socket)) { return(0); } } return(_socket.Receive(_buffer, ofs, len, SocketFlags.None)); } catch (Exception) { return(0); } }
private string ReadString(int len) { lock (_signal) { if (!_socket.Connected || SocketExtension.CleanedUp(_socket)) { return(null); } } byte[] buffer; if (!SocketExtension.Receive(_socket, 3, out buffer)) { return(null); } if (buffer == null) { return(null); } return(Encoding.UTF8.GetString(buffer)); }
private static void Accept(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); SOCKET socket = GetSocket(arguments.This); if (socket == null || SocketExtension.CleanedUp(socket)) { Throwable.ObjectDisposedException(arguments.VirtualMachine); } else { try { arguments.SetReturnValue(New(arguments.VirtualMachine, socket.Accept())); } catch (Exception e) { Throwable.Exception(arguments.VirtualMachine, e); } } }
public static WebSocketServerHandshake Handshake(object signal, Socket socket) { if (signal == null || socket == null) { return(null); } WebSocketServerHandshake handshake; lock (signal) { if (!socket.Connected || SocketExtension.CleanedUp(socket)) { return(null); } handshake = new WebSocketServerHandshake(signal, socket); } if (handshake == null || !handshake.Handle()) { return(null); } return(handshake); }
private bool SendUpgradeRequest() { lock (_signal) { if (!_socket.Connected || SocketExtension.CleanedUp(_socket)) { return(false); } } StringBuilder sb = new StringBuilder(); sb.AppendFormat("GET {0} HTTP/1.1\r\n", _uri.AbsolutePath); sb.AppendFormat("Host: {0}:{1}\r\n", _uri.Host, _uri.Port); sb.Append("Connection: Upgrade\r\n"); sb.Append("Pragma: no-cache\r\n"); sb.Append("Cache-Control: no-cache\r\n"); sb.Append("Upgrade: websocket\r\n"); sb.Append("Origin: null\r\n"); sb.Append("Accept-Encoding: gzip, deflate, br\r\n"); sb.Append("Accept-Language: zh-CN,zh;q=0.9\r\n"); sb.Append("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\r\n"); sb.AppendFormat("Sec-WebSocket-Key: {0}\r\n", _secWebSocketKey); sb.Append("Sec-WebSocket-Origin: null\r\n"); sb.Append("Sec-WebSocket-Version: 13\r\n"); sb.Append("Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n"); byte[] request = Encoding.UTF8.GetBytes(sb.ToString()); if (!SocketExtension.BeginSend(_socket, request, 0, request.Length, null)) { return(false); } return(true); }
private static void ConnectAsync(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; do { if (arguments.Length <= 0) { Throwable.ObjectDisposedException(arguments.VirtualMachine); break; } SocketContext context = GetSocketContext(arguments.This); if (context == null) { Throwable.ObjectDisposedException(arguments.VirtualMachine); break; } SOCKET socket = context.Socket; if (socket == null || SocketExtension.CleanedUp(socket)) { Throwable.ObjectDisposedException(arguments.VirtualMachine); break; } EndPoint remoteEP = ObjectAuxiliary.ToEndPoint(arguments[0]); int cbsolt = 1; if (remoteEP == null) { IPAddress address = ObjectAuxiliary.ToAddress(arguments[0]); if (address == null) { break; } int port = arguments.Length > 1 ? ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault() : 0; remoteEP = new IPEndPoint(address, port); cbsolt++; } if (remoteEP == null) { break; } NSJSFunction callback = arguments.Length > cbsolt ? arguments[cbsolt] as NSJSFunction : null; try { SocketAsyncEventArgs e = context.ConnectedAsync; if (e != null) { break; } else { e = new SocketAsyncEventArgs(); e.Completed += ProcessConnected; e.UserToken = context; context.ConnectedAsync = e; } e.RemoteEndPoint = remoteEP; if (callback != null) { callback.CrossThreading = true; context.ConnectedAsyncCallback = callback; } if (!socket.ConnectAsync(e)) { ProcessConnected(socket, e); } success = true; } catch (Exception e) { Throwable.Exception(arguments.VirtualMachine, e); } } while (false); arguments.SetReturnValue(success); }