Moli
2022-04-15 22477f0eca904ec628ab59cd749f959af35c1439
加入按配置使用的功能
7个文件已修改
172 ■■■■■ 已修改文件
Helpers/Tool.cs 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
PublicCode/SystemManage/SysConfig.cs 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WOLAwaken/Helper/ReceiveUDPPack.cs 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WOLAwaken/Helper/SendUdpPack.cs 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WOLAwaken/Helper/SendWOLPack.cs 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WOLAwaken/Program.cs 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WOLAwaken/appsettings.json 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Helpers/Tool.cs
@@ -677,11 +677,18 @@
        /// <param name="ConfigFileName"></param>
        /// <returns></returns>
        public static SysConfig GetSystemConfig(string ConfigFileName= "appsettings.json") {
            string value = string.Empty;
            string Root = AppContext.BaseDirectory;
            string json = GetTxt(Root + "\\" + ConfigFileName);
            SysConfig config = json.ToModel<SysConfig>();
            return config;
            try
            {
                string value = string.Empty;
                string Root = AppContext.BaseDirectory;
                string json = GetTxt(Root + "\\" + ConfigFileName);
                SysConfig config = json.ToModel<SysConfig>();
                return config;
            }
            catch
            {
                throw new Exception("配置读取错误,请检测项目目录下的appsettings.json文件");
            }
        }
        /// <summary>
PublicCode/SystemManage/SysConfig.cs
@@ -115,6 +115,18 @@
        /// </summary>
        public string toIp { get; set; }
        /// <summary>
        /// 本机网络ip
        /// </summary>
        public string receiveIp { get; set; }
        /// <summary>
        /// 本机接收端口
        /// </summary>
        public string receivePort { get; set; }
        /// <summary>
        /// 运行的功能,1监听,2发送,3监听+发送
        /// </summary>
        public string startProject { get; set; }
        /// <summary>
        /// 转换后的mac地址
        /// </summary>
        public int[] mac
@@ -126,7 +138,8 @@
                int index = 0;
                foreach (var a in strs)
                {
                    macArr[index++] = Convert.ToInt32("0x" + a);
                    macArr[index++] = Convert.ToInt32(a.ToString(), 16);
                }
                return macArr;
            }
WOLAwaken/Helper/ReceiveUDPPack.cs
@@ -25,15 +25,29 @@
        /// <summary>
        /// 线程:不断监听UDP报文
        /// </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和监听端口号
                localIpep = new IPEndPoint(IPAddress.Parse(receiveIp), receivePort); // 本机IP和监听端口号
                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));
WOLAwaken/Helper/SendUdpPack.cs
@@ -45,11 +45,40 @@
        {
            try
            {
                localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53470); // 本机IP和监听端口号
                var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
                var ipConfig = "127.0.0.1";
                var portConfig = 53470;
                var toIp = "255.255.255.255";
                var toPort = 53490;
                if (config != null)
                {
                    if (!string.IsNullOrWhiteSpace(config.sendIp))
                    {
                        ipConfig = config.sendIp;
                    }
                    if (!string.IsNullOrWhiteSpace(config.sendPort))
                    {
                        int.TryParse(config.sendPort, out portConfig);
                    }
                    if (!string.IsNullOrWhiteSpace(config.toIp))
                    {
                        toIp = config.toIp;
                    }
                    if (!string.IsNullOrWhiteSpace(config.toPort))
                    {
                        int.TryParse(config.toPort, out toPort);
                    }
                }
                else
                {
                    ipConfig = "127.0.0.1";
                }
                localIpep = new IPEndPoint(IPAddress.Parse(ipConfig), portConfig); // 本机IP和监听端口号
                udpcSend = new UdpClient(localIpep);
                byte[] sendbytes = data;
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 53490); // 发送到的IP地址和端口号
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(toIp), toPort); // 发送到的IP地址和端口号
                string str = "";
                foreach (var a in sendbytes)
                {
WOLAwaken/Helper/SendWOLPack.cs
@@ -9,10 +9,17 @@
    public class SendWOLPack
    {
        public byte[] SendData = new byte[102];
        public int[] macAddress = new int[] { 0x04, 0x92, 0x26, 0xBE, 0x92, 0x98 };
        public int[] macAddress = new int[] { 0x50, 0xEB, 0xF6, 0x43, 0x50, 0x08 }; //50:EB:F6:43:50:08
        public SendWOLPack()
        {
            var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
            if (config != null) {
                if (config.mac != null && config.mac.Length > 0)
                {
                    macAddress = config.mac;
                }
            }
            int index = 0;
            for (int i = 0; i < 6; i++)
            {
WOLAwaken/Program.cs
@@ -1,21 +1,51 @@
// See https://aka.ms/new-console-template for more information
using Helpers;
using WOLAwaken.Helper;
Console.WriteLine("Hello, World!");
var config = Helpers.Tool.GetSystemConfig();
Console.WriteLine(config.baseSysConfg);
#region 发送方法
var SendFun = () => {
    while (true)
    {
        try
        {
            new SendWOLPack();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
        }
        Console.WriteLine("END;");
        Console.WriteLine("按下回车可以进行下一次发送;");
        Console.ReadLine();
    }
};
#endregion
var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
if (config != null && (!string.IsNullOrWhiteSpace(config.startProject)))
{
    if (config.startProject == "1")
    {
        //只监听
        ReceiveUDPPack.StartReceive();
    }
    else if (config.startProject == "2")
    {
        //只发送
        SendFun();
    }
    else if (config.startProject == "3")
    {
        //监听加发送
        ReceiveUDPPack.StartReceive();
        Task.Delay(1500).Wait();
        SendFun();
    }
    else
    {
        Console.WriteLine("无选项");
    }
}
Console.ReadLine();
//ReceiveUDPPack.StartReceive();
//while (true)
//{
//    try
//    {
//        new SendWOLPack();
//    }
//    catch (Exception ex)
//    {
//        Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
//    }
//    Console.WriteLine("END;");
//    Console.ReadLine();
//}
WOLAwaken/appsettings.json
@@ -1,9 +1,12 @@
{
  "baseSysConfg": {
    "macStr": "04-92-26-BE-92-98",
    "macStr": "50-EB-F6-BE-50-08",
    "sendPort": "53470",
    "toPort": "53490",
    "sendIp": "192.168.2.111",
    "toIp": "255.255.255.255"
    "toIp": "255.255.255.255",
    "receiveIp": "192.168.2.101",
    "receivePort": "53490",
    "startProject": "1"
  }
}