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

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");}}
}

http://www.xdnf.cn/news/4728.html

相关文章:

  • 剖析 FFmpeg:从基本功能到过滤器,实现音视频处理的灵活性
  • python编译exe执行时报错:OSError:[WinError6]句柄无效
  • http协议理解
  • LLM 论文精读(三)Demystifying Long Chain-of-Thought Reasoning in LLMs
  • Qt 编译 sqldrivers之psql
  • Linux中的防火墙
  • 【RAG】indexing 中的 Hierarchical Indexing(分层索引)
  • Matlab 四分之一车体被动和模糊控制对比
  • 文件包含 任意文件读取
  • B站搜索关键词全攻略:掌握B站搜索关键词的运作机制
  • 深度学习-160-Dify工具之从模板创建应用示例
  • 评论系统表结构设计--简单模型
  • 每日学习Java之一万个为什么(待完善)
  • [原创](现代Delphi 12指南):[macOS 64bit App开发]: 如何获取自身程序的所在的目录?
  • Mac 平台获取地区标识符号
  • Windows (可永久)暂停更新用以解决兼容性、性能与稳定性问题
  • CH32V208GBU6沁恒绑定配对获取静态地址
  • 高能数造全固态电池干法电极高品质原纤化技术:驱动干法和全固态电池制造新进程
  • inotify事件驱动机制
  • 【5G通信】bwp和redcap 随手记 2
  • LabVIEW超声波液位计检定
  • 长事务:数据库中的“隐形炸弹“——金仓数据库运维避坑指南
  • CSS:字体和文本样式
  • Ubuntu上安装MySQL 8并配置Navicat远程连接
  • docker操作镜像-以mysql为例
  • 数据结构和算法
  • AI技术视角:美联储信号与黄金动态的量化研究——基于多模态数据分析框架
  • 滚珠导轨:电子制造领域精密运动的核心支撑
  • Spark缓存--persist方法
  • C++使用PoDoFo库处理PDF文件