Esempio n. 1
0
 /// <summary>
 /// Singleton here
 /// </summary>
 /// <param name="parent">parent ref</param>
 /// <returns></returns>
 public static DotNetWebServer GetInstance(Program parent)
 {
     if (_instance == null)
     {
         _instance = new DotNetWebServer(parent);
     }
     return(_instance);
 }
Esempio n. 2
0
        // Start the server, Singleton here
        public void Start()
        {
            // Start server
            var builder = new ConfigurationBuilder();

            builder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("config.json");
            Configuration = builder.Build();
            DotNetWebServer ws = DotNetWebServer.GetInstance(this);

            ws.Start();
        }
Esempio n. 3
0
        // Start the server, Singleton here
        public void Start()
        {
            // Start server
            var builder = new ConfigurationBuilder();

            builder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("config.json");
            Configuration = builder.Build();
            threadMode    = Configuration["mode"];
            maxPool       = Convert.ToInt32(Configuration["maxPool"]);
            DotNetWebServer ws = DotNetWebServer.GetInstance(Convert.ToInt16(Configuration["Port"]), this);

            ws.Start();
        }
Esempio n. 4
0
        // Start the server, Singleton here
        public void Start()
        {
            // Start server
            var builder = new ConfigurationBuilder();

            builder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("config.json");
            Configuration = builder.Build();
            // FIXME: refactor this into separate plugin system
            WerewolfManager wm = new WerewolfManager();

            wm.Start();
            DotNetWebServer ws = DotNetWebServer.GetInstance(this);

            ws.Start();
        }
Esempio n. 5
0
        /// <summary>
        /// Get a request from client, process it, then return response to client
        /// </summary>
        public void Process()
        {
            NetworkStream ns         = new NetworkStream(_client);
            string        requestStr = "";
            HTTPRequest   request    = null;
            HTTPResponse  response   = null;

            byte[] bytes = new byte[1024];
            int    bytesRead;

            // Read all request
            do
            {
                bytesRead   = ns.Read(bytes, 0, bytes.Length);
                requestStr += Encoding.UTF8.GetString(bytes, 0, bytesRead);
            } while (ns.DataAvailable);

            request = new HTTPRequest(requestStr);
            request.addProperty("RemoteEndPoint", _client.RemoteEndPoint.ToString());

            // We can handle only GET now
            if (request.Status != 200)
            {
                response = new HTTPResponse(request.Status);
            }
            else
            {
                bool processed = false;
                // pre processing
                foreach (KeyValuePair <string, PluginInfo> plugininfo in plugins)
                {
                    if (plugininfo.Value.preprocessing)
                    {
                        plugininfo.Value.reference.PreProcessing(request);
                    }
                }
                // plugins
                foreach (KeyValuePair <string, PluginInfo> plugininfo in plugins)
                {
                    if (request.Filename.StartsWith(plugininfo.Key))
                    {
                        response  = plugininfo.Value.reference.GetResponse(request);
                        processed = true;
                    }
                }
                // local file
                if (!processed)
                {
                    if (request.Filename.Equals(""))
                    {
                        response = getFile(ROOT + "/index.html");
                    }
                    else
                    {
                        response = getFile(ROOT + "/" + request.Filename);
                    }
                }
                // post processing pipe
                foreach (KeyValuePair <string, PluginInfo> plugininfo in plugins)
                {
                    if (plugininfo.Value.postprocessing)
                    {
                        response = plugininfo.Value.reference.PostProcessing(response);
                    }
                }
            }
            // Generate response
            ns.Write(Encoding.UTF8.GetBytes(response.header), 0, response.header.Length);
            if (response.body != null)
            {
                ns.Write(response.body, 0, response.body.Length);
            }

            // Shuting down
            //ns.Close();
            _client.Shutdown(SocketShutdown.Both);
            //_client.Close();
            var threads = DotNetWebServer
                          .GetInstance(Convert.ToInt16(Program.Configuration["Port"]), _parent)
                          .GetThreads;

            threads.Remove(Thread.CurrentThread.ManagedThreadId);
            // .NET Core doesn't support Thread.Abort()
            // Thread.CurrentThread.Abort();
        }