diff --git a/HxServer/HxServer/Peripheral/MonitorServer.cs b/HxServer/HxServer/Peripheral/MonitorServer.cs index 7ec6fc4..2d7aea7 100644 --- a/HxServer/HxServer/Peripheral/MonitorServer.cs +++ b/HxServer/HxServer/Peripheral/MonitorServer.cs @@ -45,6 +45,7 @@ public class AppInfo public class MonitorServer { private static UdpClient UdpClient; + private static HttpUtils HttpServer = new(); private static readonly ConcurrentDictionary AppInfos = new(); @@ -66,9 +67,33 @@ public class MonitorServer /// public static void Startup() { + HttpServer.CreateHttp("http://+:5002/", HttpListenerEvent); + TaskUtils.Run(AppMonitorProcess, 2000, Guid.NewGuid()); } + private static void HttpListenerEvent(object sender, HttpListenerContext e) + { + e.Response.AddHeader("Content-Type", "application/json"); + + if (e.Request.HttpMethod == "GET") + { + e.Response.OutputStream.Write("OK"); + } + else if (e.Request.HttpMethod == "POST") + { + try + { + string json = string.Empty; + using (StreamReader reader = new StreamReader(e.Request.InputStream, Encoding.UTF8)) + json = reader.ReadToEnd(); + + Debug.WriteLine("POST" + json); + } + catch { e.Response.OutputStream.Write(JsonUtils.ToJson(new { status = false, picdata = "" })); } + } + } + private static void RequestCallback(IAsyncResult ar) { var point = new IPEndPoint(IPAddress.Any, 0); @@ -157,7 +182,7 @@ public class MonitorServer { if (name == null) return; - if (AppInfos.TryGetValue(name, out AppInfo? app)) + if (AppInfos.TryGetValue(name, out AppInfo app)) { if (app == null) return; @@ -184,7 +209,8 @@ public class MonitorServer /* 若进程不存在, 启动进程 */ if (process.Length == 0) { - Process.Start(app.Path); + if (File.Exists(app.Path)) + Process.Start(app.Path); } //else //{ diff --git a/HxServer/HxServer/Utils/FileUtils.cs b/HxServer/HxServer/Utils/FileUtils.cs index ff4568a..d903610 100644 --- a/HxServer/HxServer/Utils/FileUtils.cs +++ b/HxServer/HxServer/Utils/FileUtils.cs @@ -11,7 +11,7 @@ public class FileUtils public static long Size(string fullpath) { try { return new FileInfo(fullpath).Length; } catch { return 0; } } public static string GetName(string fullpath) { try { return new FileInfo(fullpath).Name; } catch { return ""; } } public static string ReadString(string fullpath) { try { return File.ReadAllText(fullpath); } catch { return ""; } } - public static byte[]? ReadBytes(string fullpath) { try { return File.ReadAllBytes(fullpath); } catch { return null; } } + public static byte[] ReadBytes(string fullpath) { try { return File.ReadAllBytes(fullpath); } catch { return null; } } public static void Append(string fullpath, string contents) { diff --git a/HxServer/HxServer/Utils/HttpUtils.cs b/HxServer/HxServer/Utils/HttpUtils.cs new file mode 100644 index 0000000..b7ec044 --- /dev/null +++ b/HxServer/HxServer/Utils/HttpUtils.cs @@ -0,0 +1,111 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Text; + +namespace HxServer.Utils; + +/// +/// HTTP扩展库 +/// +public class HttpUtils +{ + /// + /// HTTP GET/POST数据接收回调函数 + /// + public EventHandler ListenCallBack; + + /// + /// 创建HTTP服务端 + /// + /// 监听的地址 + /// 回调函数 + /// true 创建成功 false 失败 + public bool CreateHttp(string url, EventHandler callback) + { + ServicePointManager.DefaultConnectionLimit = 1023; + + ListenCallBack = callback; + + HttpListener DataSever = new(); + + try + { + DataSever.Prefixes.Add(url); + DataSever.Start(); + DataSever.BeginGetContext(RequestCallBack, DataSever); + } + catch (Exception ex) { Console.WriteLine($"Http Create {ex}"); } + + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Http Create Success"); + Console.ForegroundColor = ConsoleColor.White; + + return DataSever.IsListening; + } + + private void RequestCallBack(IAsyncResult ar) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Http RequestCallBack Receive data"); + Console.ForegroundColor = ConsoleColor.White; + + HttpListener DataSever = ar.AsyncState as HttpListener; + HttpListenerContext Context = DataSever.EndGetContext(ar); + DataSever.BeginGetContext(RequestCallBack, DataSever); + + /* 设置返回数据为JSON类 */ + Context.Response.AddHeader("Content-Type", "application/json"); + + System.Threading.Tasks.Task.Factory.StartNew(() => ListenCallBack?.Invoke(null, Context)); + } + + /// + /// 发送Get请求 + /// + /// 地址 + /// 超时时间(秒) + /// 结果 + /// 发送状态 + public static bool Get(string url, int seconds, out string result) + { + result = string.Empty; + + using var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(seconds) }; + var response = client.GetAsync(url).Result; + if (response.IsSuccessStatusCode) + { + result = response.Content.ReadAsStringAsync().Result; + + return true; + } + + return false; + } + + /// + /// 发送Post请求 + /// + /// 地址 + /// 数据 + /// 超时时间(秒) + /// 结果 + /// + public static bool Post(string url, object data, int seconds, out string result) + { + result = string.Empty; + + HttpContent content = new StringContent(data.ToString()); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn"); + using HttpClient client = new(); + HttpResponseMessage res = client.PostAsync(url, content).Result; + if (res.IsSuccessStatusCode) + { + result = res.Content.ReadAsStringAsync().Result; + + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/HxServer/HxServer/Utils/JsonUtils.cs b/HxServer/HxServer/Utils/JsonUtils.cs index ce6f42e..8121f5d 100644 --- a/HxServer/HxServer/Utils/JsonUtils.cs +++ b/HxServer/HxServer/Utils/JsonUtils.cs @@ -9,7 +9,7 @@ namespace HxServer.Utils; public class JsonUtils { - public static string ToJson(object? value) + public static string ToJson(object value) { if (value == null) return ""; @@ -17,7 +17,7 @@ public class JsonUtils return JsonConvert.SerializeObject(value); } - public static Dictionary ToDictionary(object? message) + public static Dictionary ToDictionary(object message) { var json = message?.ToString(); diff --git a/HxServer/HxServer/Utils/StreamUtils.cs b/HxServer/HxServer/Utils/StreamUtils.cs new file mode 100644 index 0000000..0704c89 --- /dev/null +++ b/HxServer/HxServer/Utils/StreamUtils.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HxServer.Utils +{ + public static class StreamUtils + { + public static void Write(this Stream stream, object data) + { + try + { + using StreamWriter writer = new(stream); + writer.Write(data.ToString()); + } + catch { } + } + } +} \ No newline at end of file diff --git a/HxServer/HxServer/Utils/UdpClientUtils.cs b/HxServer/HxServer/Utils/UdpClientUtils.cs index ab07e9a..24bf5e7 100644 --- a/HxServer/HxServer/Utils/UdpClientUtils.cs +++ b/HxServer/HxServer/Utils/UdpClientUtils.cs @@ -21,7 +21,7 @@ namespace HxServer.Utils client.Send(buffer); } - public static void Send(this UdpClient client, IPEndPoint? point, object obj) + public static void Send(this UdpClient client, IPEndPoint point, object obj) { var data = obj.ToString();