/// <summary> /// Adds a new web event to the server. /// </summary> /// <param name="webEvent">The web event and corresponding data that the server will handle.</param> /// <returns>A Boolean value that indicates whether the <see cref="WebEvent"/> could be added, <b>true</b> if added, /// <b>false</b>, if webEvent already exists. </returns> internal bool AddWebEvent(WebEvent webEvent) { if (!WebEvents.Contains(webEvent.GetWebEventId())) { WebEvents.Add(webEvent.GetWebEventId(), webEvent); return(true); } return(false); }
/// <summary> /// Creates a path on the server so that an http GET request can be processed. /// Specifies a path of the form: http://{IP}:{port}/{path}. /// </summary> /// <param name="path">The path used to identify a resource.</param> /// <returns>A <see cref="T:GTM.NetworkModule.WebEvent"/> object that is used to update data and respond to incoming requests. /// </returns> public static WebEvent SetupWebEvent(string path) { WebEvent webEvent = WebServerManager.Instance.GetWebEventById(path); if (webEvent == null) { webEvent = new WebEvent(path); } return(webEvent); }
/// <summary> /// Creates a path to a resource on the server so that a http GET requests can be processed. /// Specifies the path of the form: http://{IP}:{port}/{path}. /// </summary> /// <param name="path">The path used to identify a resource.</param> /// <param name="refreshAfter">Specifies the refresh interval in seconds.</param> /// <returns>A <see cref="WebEvent"/> object that is used to update data and respond to incoming requests. /// </returns> public static WebEvent SetupWebEvent(string path, uint refreshAfter) { WebEvent webEvent = WebServerManager.Instance.GetWebEventById(path); if (webEvent == null) { webEvent = new WebEvent(path); } webEvent.refreshAfter = refreshAfter; return(webEvent); }
/// <summary> /// Stops and removes the given <see cref="WebEvent"/>. /// </summary> /// <param name="webEvent">The <see cref="WebEvent"/> to stop.</param> /// <returns>Returns <b>true</b> if the <see cref="WebEvent"/> exists and can be removed, <b>false</b> if <see cref="WebEvent"/> /// does not exist or cannot be stopped.</returns> public bool Stop(WebEvent webEvent) { if (WebEvents.Contains(webEvent.GetWebEventId())) { WebEvents.Remove(webEvent.GetWebEventId()); return(true); } // Debug.Print("Service not found"); return(false); }
///<summary> /// Stops and removes a single resource. If the resource is requesterd again, the server will return the default page. ///</summary> ///<param name="webEvent">The <see cref="WebEvent"/> to be stopped</param> ///<returns>The result of the operation, <b>true</b> if the resource is found and removed, otherwise <b>false</b>.</returns> public static bool DisableWebEvent(WebEvent webEvent) { return(WebServerManager.Instance.Stop(webEvent)); }
/// <summary> /// Processes incoming requests /// </summary> protected void ProcessRequest() { // accept an incoming connection request and once we have one, spawn a new thread to accept more bool newThread = false; System.Net.Sockets.Socket clientSocket = null; Responder responder = null; while (IsRunning && !newThread) { try { // process incoming request clientSocket = LocalServer.Accept(); clientSocket.ReceiveTimeout = Timeout; clientSocket.SendTimeout = Timeout; // Parse message an create an object containing parsed data responder = new Responder(); responder.ClientEndpoint = clientSocket.RemoteEndPoint.ToString(); responder.ClientSocket = clientSocket; Thread t = new Thread(new ThreadStart(ProcessRequest)); t.Start(); newThread = true; } catch { if (clientSocket != null) { try { clientSocket.Close(); } catch { } } } } // now process the request try { bool finishedParsing = false; TimeSpan parseStart = Timer.GetMachineTime(); while (!finishedParsing) { if (Timer.GetMachineTime() - parseStart > MaxProcessingTime) { return; } // receiving data, add to an array to process later byte[] buffer = new byte[clientSocket.Available]; clientSocket.Receive(buffer); finishedParsing = responder.Parse(buffer); } // trigger event to get response WebEvent webevent = null; if (responder.Path != null) { webevent = Instance.GetWebEventById(responder.Path); } if (webevent == null) { webevent = Instance.DefaultEvent; } responder.webEvent = webevent; webevent.OnWebEventReceived(responder.Path, responder.HttpMethod, responder); } catch { } }