コード例 #1
0
ファイル: WebServerManager.cs プロジェクト: garronej/Domotic
            /// <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
                {
                }
            }
コード例 #2
0
        /// <summary>
        /// Raises the <see cref="WebEventReceived"/> event.
        /// </summary>
        /// <param name="responder">Contains request data sent by the the client and functionality to respond to the request.</param>
        /// <param name="method">The incoming http method. </param>
        /// <param name="path">The path of the requested resource.</param>
        internal virtual void OnWebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            if (onWebEventReceived == null)
            {
                onWebEventReceived = new ReceivedWebEventHandler(OnWebEventReceived);
            }

            if (_WebEventReceived == null || noHandlerSet)
            {
                responder.SendResponse();
            }
            else if (Program.CheckAndInvoke(_WebEventReceived, onWebEventReceived, path, method, responder))
            {
                _WebEventReceived(path, method, responder);
            }
        }