/// <summary> /// We'll get an endpoint invokcation from the web server /// so we can execute the endpoint action and response based on its supplied arguments /// in a seperate thread, hence the event. we'll set the event return string /// so the web server can know how to respond back to the ui in a seperate thread /// </summary> /// <param name="source"></param> /// <param name="e"></param> private static void EndPointHandler(object source, EndPoinEventArgs e) { var misc = new EndPointActionArguments { Connection = e.Connection }; e.ReturnString = e.Command.Execute(misc); // we can override the manual use of the socket if we returned a value other than null if (e.ReturnString != null && e.Command.UsesManualSocket) { e.ManualSent = false; } else { e.ManualSent = e.Command.UsesManualSocket; } }
/// <summary> /// Starts the server. /// </summary> private void StartServer() { using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { server.Bind(new IPEndPoint(IPAddress.Any, Port)); server.Listen(1); while (!_cancel) { var connection = server.Accept(); if (connection.Poll(-1, SelectMode.SelectRead)) { // Create buffer and receive raw bytes. var bytes = new byte[connection.Available]; int count = connection.Receive(bytes); // Convert to string, will include HTTP headers. var rawData = new string(Encoding.UTF8.GetChars(bytes)); var endPoint = InterpretRequest(rawData); if (endPoint != null) { if (_enableLedStatus) { PingLed(); } // dispatch the endpoint var e = new EndPoinEventArgs(endPoint, connection); if (EndPointReceived != null) { ThreadUtil.SafeQueueWorkItem(() => { try { EndPointReceived(null, e); if (!e.ManualSent) { var response = e.ReturnString; SendResponse(response, connection); } else { connection.Close(); } } catch(Exception ex) { SendResponse("error: " + ex, connection); } }); } } else { SendResponse(GetApiList(), connection); } } } } }
/// <summary> /// Starts the server. /// </summary> private void StartServer() { using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { server.Bind(new IPEndPoint(IPAddress.Any, Port)); server.Listen(1); while (!_cancel) { var connection = server.Accept(); if (connection.Poll(-1, SelectMode.SelectRead)) { // Create buffer and receive raw bytes. var bytes = new byte[connection.Available]; int count = connection.Receive(bytes); // Convert to string, will include HTTP headers. var rawData = new string(Encoding.UTF8.GetChars(bytes)); var endPoint = InterpretRequest(rawData); if (endPoint != null) { if (_enableLedStatus) { PingLed(); } // dispatch the endpoint var e = new EndPoinEventArgs(endPoint, connection); if (EndPointReceived != null) { ThreadUtil.SafeQueueWorkItem(() => { try { EndPointReceived(null, e); if (!e.ManualSent) { var response = e.ReturnString; SendResponse(response, connection); } else { connection.Close(); } } catch (Exception ex) { SendResponse("error: " + ex, connection); } }); } } else { SendResponse(GetApiList(), connection); } } } } }