From 22477f0eca904ec628ab59cd749f959af35c1439 Mon Sep 17 00:00:00 2001
From: Moli <linmolilin@gmail.com>
Date: 星期五, 15 四月 2022 10:15:52 +0800
Subject: [PATCH] 加入按配置使用的功能

---
 WOLAwaken/Helper/ReceiveUDPPack.cs |   29 ++++++++++++++++++++++++-----
 1 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/WOLAwaken/Helper/ReceiveUDPPack.cs b/WOLAwaken/Helper/ReceiveUDPPack.cs
index 956a3dd..1d3f301 100644
--- a/WOLAwaken/Helper/ReceiveUDPPack.cs
+++ b/WOLAwaken/Helper/ReceiveUDPPack.cs
@@ -25,15 +25,29 @@
         /// <summary>
         /// 绾跨▼锛氫笉鏂洃鍚琔DP鎶ユ枃
         /// </summary>
-        static Thread thrRecv;
+        static Task thrRecv;
 
         public static void StartReceive()
         {
+            var receiveIp = "127.0.0.1";
+            var receivePort = 53490;
+            var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
+            if (config != null) 
+            {
+                if (!string.IsNullOrWhiteSpace(config.receiveIp))
+                {
+                    receiveIp = config.receiveIp;
+                }
+                if (!string.IsNullOrWhiteSpace(config.receivePort))
+                {
+                    int.TryParse(config.toPort, out receivePort);
+                }
+            }
             if (!IsUdpcRecvStart) // 鏈洃鍚殑鎯呭喌锛屽紑濮嬬洃鍚�
             {
-                localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53490); // 鏈満IP鍜岀洃鍚鍙e彿
+                localIpep = new IPEndPoint(IPAddress.Parse(receiveIp), receivePort); // 鏈満IP鍜岀洃鍚鍙e彿
                 udpcRecv = new UdpClient(localIpep);
-                thrRecv = new Thread(ReceiveMessage);
+                thrRecv = new Task(() => { ReceiveMessage(); });
                 thrRecv.Start();
                 IsUdpcRecvStart = true;
                 Console.WriteLine("UDP鐩戝惉鍣ㄥ凡鎴愬姛鍚姩");
@@ -44,7 +58,7 @@
         {
             if (IsUdpcRecvStart)
             {
-                thrRecv.Abort(); // 蹇呴』鍏堝叧闂繖涓嚎绋嬶紝鍚﹀垯浼氬紓甯�
+                thrRecv.Dispose(); // 蹇呴』鍏堝叧闂繖涓嚎绋嬶紝鍚﹀垯浼氬紓甯�
                 udpcRecv.Close();
                 IsUdpcRecvStart = false;
                 Console.WriteLine("UDP鐩戝惉鍣ㄥ凡鎴愬姛鍏抽棴");
@@ -55,13 +69,18 @@
         /// 鎺ユ敹鏁版嵁
         /// </summary>
         /// <param name="obj"></param>
-        private static void ReceiveMessage(object obj)
+        private static void ReceiveMessage()
         {
             while (IsUdpcRecvStart)
             {
                 try
                 {
                     byte[] bytRecv = udpcRecv.Receive(ref localIpep);
+                    foreach (var a in bytRecv)
+                    {
+                        Console.Write(a);
+                        Console.Write("-");
+                    }
                     string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length);
                     Console.WriteLine(string.Format("{0}[{1}]", localIpep, message));
 

--
Gitblit v1.9.1