当前位置: 首页 > news >正文

GrassRoot备份项目

Windows服务项目

Grass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;namespace Grass
{public class GrassRoot{private HttpListener listener;private string url = "http://+:8422/"; //"http://localhost:8422/" 监听的URL//listener.Prefixes.Add("http://+:8422/"); // 监听所有IP地址上的8422端口")private string proxy_url = string.Empty;volatile bool runing = false;private Thread listenThread;private List<Thread> ListThread = new List<Thread>();private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;//跨线程需要同步上下文public GrassRoot(){listener = new HttpListener();listener.Prefixes.Add(url);listener.Start();}public void Start(){if (listener == null){listener = new HttpListener();}if (listener.IsListening == false){listener.Start();}if (this.listenThread == null || this.listenThread.IsAlive == false){this.listenThread = new Thread(new ThreadStart(ListenForRequests));this.listenThread.IsBackground = false;//前台线程不会自动退出,thread类默认前台线程,前台线程会阻塞主线程}switch (listenThread.ThreadState){case System.Threading.ThreadState.Unstarted:this.listenThread.Start();runing = true;break;case System.Threading.ThreadState.StopRequested:case System.Threading.ThreadState.AbortRequested:case System.Threading.ThreadState.Aborted:case System.Threading.ThreadState.Stopped:case System.Threading.ThreadState.Suspended:case System.Threading.ThreadState.SuspendRequested:this.listenThread.Abort();this.listenThread = new Thread(new ThreadStart(ListenForRequests));this.listenThread.IsBackground = false;//前台线程不会自动退出,thread类默认前台线程,前台线程会阻塞主线程this.listenThread.Start();runing = true;break;case System.Threading.ThreadState.Running:runing = true;break;}}public void Stop(){if (listener != null && listener.IsListening == true){listener.Stop();listener.Close();}ListThread.Clear();GC.Collect();}private void ListenForRequests(){Task.Run(() =>{// 创建一个新的线程来处理客户端通信  Thread clientThread = new Thread(HandleRequests);clientThread.IsBackground = true;   //后台线程不阻塞主线程,后台线程用完后自动退出clientThread.Start();ListThread.Add(clientThread);//剔除睡眠线程for (int i = 0; i < ListThread.Count; i++){if (ListThread.ElementAt<Thread>(i).ThreadState == System.Threading.ThreadState.Stopped)ListThread.RemoveAt(i);}GC.Collect();});}// 处理请求private async void HandleRequests(){while (listener.IsListening){// 等待传入的请求HttpListenerContext context = await listener.GetContextAsync();// 处理请求HttpListenerRequest request = context.Request;HttpListenerResponse response = context.Response;Console.WriteLine($"Received request for {request.Url}");// 构造响应内容string responseString = "<html><body><h1>Hello, World!</h1></body></html>";byte[] buffer = Encoding.UTF8.GetBytes(responseString);// 设置响应头response.ContentType = "text/html";response.ContentLength64 = buffer.Length;response.ContentEncoding = Encoding.UTF8;// 获取当前UTC时间并转换为本地时间DateTime utcNow = DateTime.UtcNow;TimeZoneInfo localZone = TimeZoneInfo.Local;DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, localZone);// 设置响应头中的Date和Last-Modified为本地时间//response.Headers["Date"] = localTime.ToString("R"); // RFC1123格式response.Headers["Last-Modified"] = localTime.ToString("R"); // RFC1123格式//response.Headers["Date"] = DateTime.UtcNow.ToString("R"); // RFC1123格式//response.Headers["Last-Modified"] = DateTime.UtcNow.ToString("R"); // RFC1123格式// 发送响应await response.OutputStream.WriteAsync(buffer, 0, buffer.Length);response.Close();}}}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;namespace GrassRoot
{internal static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>static void Main(){ServiceBase[] ServicesToRun;ServicesToRun = new ServiceBase[]{new Service1()};ServiceBase.Run(ServicesToRun);}}
}

Service1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Grass;namespace GrassRoot
{public partial class Service1 : ServiceBase{Grass.GrassRoot grassRoot =new Grass.GrassRoot();public Service1(){InitializeComponent();}protected override void OnStart(string[] args){grassRoot.Start();EventLog.WriteEntry("GrassRoot started");}protected override void OnStop(){grassRoot.Stop();EventLog.WriteEntry("GrassRoot stopped");}}
}

相关文章:

  • uniapp项目打包的微信小程序,设置uni-popup type=“bottom“时,底部有空隙
  • Qt实现车载多媒体项目,包含天气、音乐、视频、地图、五子棋功能模块,免费下载源文件!
  • Amazon Redshift 使用场景解析与最佳实践
  • 全连接神经网络学习笔记
  • 学习黑客什么是 ARP
  • AutoGen 框架解析:微软开源的多人 Agent 协作新范式
  • 每日学习:DAY24
  • Spring AI 入门(持续更新)
  • 深入解析建造者模式(Builder Pattern)——以Java实现复杂对象构建的艺术
  • 支持鸿蒙next的uts插件
  • 计算机学习路线与编程语言选择(信息差)
  • LLaMA模型本地部署全攻略:从零搭建私有化AI助手
  • 突破网络限制:Windows平台离线搭建Linux环境+Docker化部署AI知识库RAGFlow实战
  • 平板收银系统、国产系统,鸿蒙系统,小键盘的封装与应用—仙盟创梦IDE
  • Matlab 数控车床进给系统的建模与仿真
  • Java执行linux服务器本地命令
  • HTTP Error 500.31 - Failed to load ASP.NET Core runtime
  • 第三节第一部分:Static修饰类变量、成员变量
  • xiaopiu原型设计工具笔记
  • 多环串级PID
  • 安徽亳州涡阳县司法局党组书记刘兴连落马
  • 毕赣新作《狂野时代》入围戛纳主竞赛单元,易烊千玺舒淇主演
  • 上海交大:关注到对教师邵某的网络举报,已成立专班开展调查
  • 纪录片《中国》原班人马打造,《船山先生》美学再升级
  • “三德子”赵亮直播间卖“德子土鸡”,外包装商标实为“德子土”
  • 泉州一家婚介机构广告牌越南新娘七天闪婚领证?市监部门介入