コード例 #1
0
ファイル: WebServer.cs プロジェクト: thomdabeast/CptS422
        public static bool Start(int port, int number_threads)
        {
            // Create thread to listen for incoming clients
            new Thread(() =>
            {
                // Init and start server
                TcpListener listener = new TcpListener(IPAddress.Any, port);
                listener.Start();

                threadPool.Start((ushort)number_threads);

                while (true)
                {
                    TcpClient client = listener.AcceptTcpClient();

                    // Make thread task function and add it to the ThreadPool
                    threadPool.AddTask(() =>
                    {
                        WebRequest req = BuildRequest(client);

                        // Invalid request object, close connection.
                        if (null != req)
                        {
                            // Handle that request
                            // Find services that will handle the request URI
                            int longestPrefix       = 0;
                            WebService serviceToUse = null;

                            foreach (var service in services)
                            {
                                if (req.URI.StartsWith(service.ServiceURI))
                                {
                                    // We'll select the service that has a longer prefix of the request URI and use it
                                    if (service.ServiceURI.Length > longestPrefix)
                                    {
                                        longestPrefix = service.ServiceURI.Length;
                                        serviceToUse  = service;
                                    }
                                }
                            }

                            if (null == serviceToUse)
                            {
                                // send 404 reponse
                                req.WriteNotFoundResponse("");
                            }
                            else
                            {
                                // "Handle" request
                                serviceToUse.Handler(req);
                            }
                        }

                        client.Close();
                    });
                }
            }).Start();

            return(true);
        }
コード例 #2
0
ファイル: WebServer.cs プロジェクト: z-mohamed/CS422
        //Accept TCP client and Call BulildRequest
        static void ThreadWork(Object clnt)
        {
            TcpClient client = clnt as TcpClient;

            WebRequest req = BuildRequest(client);

            WebService handler = null;;

            bool processRequest;


            if (req == null)
            {
                //close client connection
                client.GetStream().Close();
                client.Close();
            }
            else
            {
                //if the request-target/URI starts with the string specified by the WebService object’s
                //ServiceURI parameter, then it can process that request.

                processRequest = false;

                foreach (var x in servicesList)
                {
                    if (req.URI.StartsWith(x.Key))
                    {
                        processRequest = true;
                        handler        = x.Value;
                    }
                }
                if (processRequest)
                {
                    //find appropriate handler  in the list of
                    //handlers/services that the webserver stores

                    //WebService handler;
                    //servicesList.TryGetValue (req.URI, out handler);

                    //call the handle
                    handler.Handler(req);
                }
                //write a 404 – Not Found response
                else
                {
                    string pageHTML = "<html> ERROR! </html>";
                    req.WriteNotFoundResponse(pageHTML);
                }

                //client.
                client.GetStream().Close();
                client.Close();
            }
        }
コード例 #3
0
        /*
         * ThreadWork function to serve as the request-handling function for a thread from the thread pool
         */
        private static void ThreadWork()
        {
            while (true)
            {
                TcpClient client = _collection.Take();
                if (client == null)
                {
                    break;
                }

                /*
                 * Processing a client involves calling the BuildRequest function first.
                 * Should the return value be null, the client connection is closed immediately
                 * and the thread goes back into a waiting/sleeping state.
                 * Should it be non-null,
                 * then an appropriate handler is found in the list of handlers/services that the web
                 * server stores, and the request is sent to that handler by calling its “Handle” function
                 */
                WebRequest request = BuildRequest(client);

                if (request == null)
                {
                    client.Close();
                }
                else
                {
                    WebService handler = Handler(request);

                    if (handler != null)
                    {
                        handler.Handler(request);
                        client.GetStream().Dispose();
                        client.Close();
                    }
                    else
                    {
                        request.WriteNotFoundResponse(request.RequestURI);
                        client.GetStream().Dispose();
                        client.Close();
                    }
                }
            }
        }