Пример #1
0
        private void HandleRequest(object state)
        {
            try
            {
                var context = (HttpListenerContext)state;

                context.Response.StatusCode  = 200;
                context.Response.SendChunked = true;

                int totalTime = 0;


                if (context.Request.RawUrl == "/ver")
                {
                    UpgradeHelper helper = new UpgradeHelper();
                    string        baseVersion;
                    string        latestVersion;
                    string[]      vers;
                    string        currentVersion;
                    helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);



                    VersionJson json = new VersionJson();
                    json.baseVersion    = baseVersion;
                    json.latestVersion  = latestVersion;
                    json.vers           = vers;
                    json.currentVersion = currentVersion;

                    string jsonString;
                    jsonString = JsonMapper.ToJson(json);

                    var data = Encoding.UTF8.GetBytes(jsonString);
                    context.Response.OutputStream.Write(data, 0, data.Length);
                }
                else if (context.Request.RawUrl == "/upgrade")
                {
                    UpgradeHelper helper = new UpgradeHelper();
                    helper.Upgrade("");

                    var data = Encoding.UTF8.GetBytes("Launched");
                    context.Response.OutputStream.Write(data, 0, data.Length);
                }
                else if (context.Request.RawUrl == "/progress")
                {
                    string jsonString;
                    jsonString = JsonMapper.ToJson(currentProgress);
                    var data = Encoding.UTF8.GetBytes(jsonString);
                    context.Response.OutputStream.Write(data, 0, data.Length);
                }

                context.Response.OutputStream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                // Client disconnected or some other error - ignored for this example
            }
        }
Пример #2
0
        static void ShowVersion()
        {
            UpgradeHelper helper = new UpgradeHelper();

            string baseVersion;
            string latestVersion;

            string[] vers;
            string   currentVersion;

            helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);
        }
Пример #3
0
        void webServer_ProcessRequest(object sender, ProcessRequestEventArgs args)
        {
            try
            {
                string cmd = args.Request.Path;
                if (cmd == "/ver")
                {
                    UpgradeHelper helper = new UpgradeHelper();
                    string        baseVersion;
                    string        latestVersion;
                    string[]      vers;
                    string        currentVersion;
                    helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);



                    VersionJson json = new VersionJson();
                    json.baseVersion    = baseVersion;
                    json.latestVersion  = latestVersion;
                    json.vers           = vers;
                    json.currentVersion = currentVersion;

                    string jsonString;
                    jsonString = JsonMapper.ToJson(json);

                    var data = Encoding.UTF8.GetBytes(jsonString);
                    args.Response.BinaryWrite(data);
                }
                else if (cmd == "/upgrade")
                {
                    Thread thread1 = new Thread(() =>
                    {
                        UpgradeHelper helper = new UpgradeHelper();
                        helper.Upgrade("");
                    });
                    thread1.Start();

                    var data = Encoding.UTF8.GetBytes("Launched");
                    args.Response.BinaryWrite(data);
                }
                else if (cmd == "/progress")
                {
                    string jsonString;
                    jsonString = JsonMapper.ToJson(currentProgress);
                    var data = Encoding.UTF8.GetBytes(jsonString);
                    args.Response.BinaryWrite(data);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Http Request Exception:" + e.Message);
            }
        }
Пример #4
0
        void webServer_ProcessRequest(object sender, ProcessRequestEventArgs args)
        {
            string cmd = args.Request.Path;

            if (cmd == "/ver")
            {
                UpgradeHelper helper = new UpgradeHelper();
                string        baseVersion;
                string        latestVersion;
                string[]      vers;
                string        currentVersion;
                helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);



                VersionJson json = new VersionJson();
                json.baseVersion    = baseVersion;
                json.latestVersion  = latestVersion;
                json.vers           = vers;
                json.currentVersion = currentVersion;

                string jsonString;
                jsonString = JsonMapper.ToJson(json);

                var data = Encoding.UTF8.GetBytes(jsonString);
                args.Response.BinaryWrite(data);
            }
            else if (cmd == "/upgrade")
            {
                UpgradeHelper helper = new UpgradeHelper();
                helper.Upgrade("");

                var data = Encoding.UTF8.GetBytes("Launched");
                args.Response.BinaryWrite(data);
            }
            else if (cmd == "/progress")
            {
                string jsonString;
                jsonString = JsonMapper.ToJson(currentProgress);
                var data = Encoding.UTF8.GetBytes(jsonString);
                args.Response.BinaryWrite(data);
            }
        }
Пример #5
0
        void ThreadFunc()
        {
            //listener = new HttpListener();

            ////listener.Prefixes.Add("http://+:8081/");
            //listener.Prefixes.Add("http://localhost:8081/");
            //listener.Prefixes.Add("http://127.0.0.1:8081/");

            //listener.Start();
            //while (working)
            //{
            //    try
            //    {
            //        var context = listener.GetContext();
            //        ThreadPool.QueueUserWorkItem(o => HandleRequest(context));
            //    }
            //    catch (Exception)
            //    {
            //        // Ignored for this example
            //    }
            //}

            //listener.Close();



            //TinyWeb.WebServer webServer = new TinyWeb.WebServer();

            //webServer.EndPoint = new IPEndPoint(0, 8081);
            //webServer.ProcessRequest += new TinyWeb.ProcessRequestEventHandler(this.webServer_ProcessRequest);
            //webServer.IsStarted = true;


            using (var server = new NHttp.HttpServer())
            {
                server.RequestReceived += (s, e) =>
                {
                    // The response must be written to e.Response.OutputStream.
                    // When writing text, a StreamWriter can be used.
                    string cmd = e.Request.RawUrl;


                    using (System.IO.BinaryWriter writer = new BinaryWriter(e.Response.OutputStream))
                    {
                        if (cmd == "/ver")
                        {
                            UpgradeHelper helper = new UpgradeHelper();
                            string        baseVersion;
                            string        latestVersion;
                            string[]      vers;
                            string        currentVersion;
                            helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);


                            VersionJson json = new VersionJson();
                            json.baseVersion    = baseVersion;
                            json.latestVersion  = latestVersion;
                            json.vers           = vers;
                            json.currentVersion = currentVersion;

                            string jsonString;
                            jsonString = JsonMapper.ToJson(json);

                            var data = Encoding.UTF8.GetBytes(jsonString);
                            writer.Write(data, 0, data.Length);
                        }
                        else if (cmd == "/upgrade")
                        {
                            Thread thread1 = new Thread(() =>
                            {
                                UpgradeHelper helper = new UpgradeHelper();
                                helper.Upgrade("");
                            });
                            thread1.Start();

                            var data = Encoding.UTF8.GetBytes("Launched");
                            writer.Write(data, 0, data.Length);
                        }
                        else if (cmd == "/progress")
                        {
                            string jsonString;
                            jsonString = JsonMapper.ToJson(currentProgress);
                            var data = Encoding.UTF8.GetBytes(jsonString);
                            writer.Write(data, 0, data.Length);
                        }
                    }
                };

                server.EndPoint = new IPEndPoint(IPAddress.Any, 8081);

                server.Start();
                Console.ReadKey();
            }
        }
Пример #6
0
        static void Upgrade(string version)
        {
            UpgradeHelper helper = new UpgradeHelper();

            helper.Upgrade(version);
        }
Пример #7
0
        void ThreadFunc()
        {
            //listener = new HttpListener();

            ////listener.Prefixes.Add("http://+:8081/");
            //listener.Prefixes.Add("http://localhost:8081/");
            //listener.Prefixes.Add("http://127.0.0.1:8081/");

            //listener.Start();
            //while (working)
            //{
            //    try
            //    {
            //        var context = listener.GetContext();
            //        ThreadPool.QueueUserWorkItem(o => HandleRequest(context));
            //    }
            //    catch (Exception)
            //    {
            //        // Ignored for this example
            //    }
            //}

            //listener.Close();



            //TinyWeb.WebServer webServer = new TinyWeb.WebServer();

            //webServer.EndPoint = new IPEndPoint(0, 8081);
            //webServer.ProcessRequest += new TinyWeb.ProcessRequestEventHandler(this.webServer_ProcessRequest);
            //webServer.IsStarted = true;

            using (var server = new NHttp.HttpServer())
            {
                server.RequestReceived += (s, e) =>
                {
                    using (System.IO.BinaryWriter writer = new BinaryWriter(e.Response.OutputStream))
                    {
                        string cmd = e.Request.RawUrl;


                        try
                        {
                            // The response must be written to e.Response.OutputStream.
                            // When writing text, a StreamWriter can be used.



                            if (cmd == "/ver")
                            {
                                UpgradeHelper helper = new UpgradeHelper();
                                string        baseVersion;
                                string        latestVersion;
                                string[]      vers;
                                string        currentVersion;
                                helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);


                                VersionJson json = new VersionJson();
                                json.baseVersion    = baseVersion;
                                json.latestVersion  = latestVersion;
                                json.vers           = vers;
                                json.currentVersion = currentVersion;
                                json.state          = "success";

                                string jsonString;
                                jsonString = JsonMapper.ToJson(json);

                                var data = Encoding.UTF8.GetBytes(jsonString);
                                writer.Write(data, 0, data.Length);
                            }
                            else if (cmd == "/upgrade")
                            {
                                string progressName = "\nu准备升级";
                                AsyncServer.Instance.SetProgress(progressName, 0);

                                StateJson json = new StateJson();
                                if (upgrading)
                                {
                                    json.state = "already launched";
                                }
                                else
                                {
                                    Thread thread1 = new Thread(() =>
                                    {
                                        upgrading            = true;
                                        UpgradeHelper helper = new UpgradeHelper();
                                        helper.Upgrade("");
                                        upgrading = false;
                                    });
                                    thread1.Start();

                                    json.state = "success";
                                }

                                string jsonString;
                                jsonString = JsonMapper.ToJson(json);
                                var data = Encoding.UTF8.GetBytes(jsonString);
                                writer.Write(data, 0, data.Length);
                            }
                            else if (cmd == "/progress")
                            {
                                string jsonString;
                                currentProgress.state = "success";
                                jsonString            = JsonMapper.ToJson(currentProgress);
                                var data = Encoding.UTF8.GetBytes(jsonString);
                                writer.Write(data, 0, data.Length);
                            }
                        }
                        catch (Exception ex)
                        {
                            AsyncServer.Instance.SetProgress("Idle", 0);

                            string jsonString = "";
                            if (cmd == "/ver")
                            {
                                VersionJson json = new VersionJson();
                                json.state = "net error";
                                jsonString = JsonMapper.ToJson(json);
                            }
                            else if (cmd == "/upgrade")
                            {
                                StateJson json = new StateJson();
                                json.state = "net error";
                                jsonString = JsonMapper.ToJson(json);
                            }
                            else if (cmd == "/progress")
                            {
                                ProgressJson json = new ProgressJson();
                                json.state = "net error";
                                jsonString = JsonMapper.ToJson(json);
                            }

                            var data = Encoding.UTF8.GetBytes(jsonString);
                            writer.Write(data, 0, data.Length);

                            Console.WriteLine("Http Request Exception:" + ex.Message);
                        }
                    }
                };

                server.EndPoint = new IPEndPoint(IPAddress.Any, 8081);

                server.Start();

                while (true)
                {
                    Thread.Sleep(1000);
                }
            }
        }