Browse Source

添加压缩工具类,添加日志输出回调函数,添加获取可用端口工具

yuzhengyang 7 years ago
parent
commit
5028463f0f

+ 1 - 0
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -53,6 +53,7 @@
     <Compile Include="DataUtils\CollectionUtils\ArrayTool.cs" />
     <Compile Include="DataUtils\CollectionUtils\ListTool.cs" />
     <Compile Include="DataUtils\CollectionUtils\Ls.cs" />
+    <Compile Include="DataUtils\CompressionUtils\Compression.cs" />
     <Compile Include="DataUtils\CurrencyUtils\RMB.cs" />
     <Compile Include="DataUtils\DateTimeUtils\ChineseHourTool.cs" />
     <Compile Include="DataUtils\DateTimeUtils\DateTimeConvert.cs" />

+ 54 - 0
Azylee.Utils/Azylee.Core/DataUtils/CompressionUtils/Compression.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.IO.Compression;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.CompressionUtils
+{
+    public static class Compression
+    {
+        //压缩字节
+        //1.创建压缩的数据流 
+        //2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
+        //3.将需要压缩的字节写到被压缩的文件流
+        public static byte[] Compress(byte[] bytes)
+        {
+            try
+            {
+                using (MemoryStream compressStream = new MemoryStream())
+                {
+                    using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
+                        zipStream.Write(bytes, 0, bytes.Length);
+                    return compressStream.ToArray();
+                }
+            }
+            catch { return null; }
+        }
+        //解压缩字节
+        //1.创建被压缩的数据流
+        //2.创建zipStream对象,并传入解压的文件流
+        //3.创建目标流
+        //4.zipStream拷贝到目标流
+        //5.返回目标流输出字节
+        public static byte[] Decompress(byte[] bytes)
+        {
+            try
+            {
+                using (var compressStream = new MemoryStream(bytes))
+                {
+                    using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
+                    {
+                        using (var resultStream = new MemoryStream())
+                        {
+                            zipStream.CopyTo(resultStream);
+                            return resultStream.ToArray();
+                        }
+                    }
+                }
+            }
+            catch { return null; }
+        }
+    }
+}

+ 22 - 1
Azylee.Utils/Azylee.Core/LogUtils/SimpleLogUtils/Log.cs

@@ -54,6 +54,15 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
         private LogLevel FileLogLevel = LogLevel.All;//日志输出到"文件"等级
         #endregion
 
+        /// <summary>
+        /// 日志输出回调方法
+        /// </summary>
+        /// <param name="type">类型</param>
+        /// <param name="msg">内容</param>
+        public delegate void LogEventDelegate(LogType type, string msg);
+        public LogEventDelegate LogEvent;
+
+
         private Log() { }
         /// <summary>
         /// 初始化 Log 工具(不建议使用)
@@ -81,10 +90,12 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
         /// </summary>
         /// <param name="console">控制台输出级别</param>
         /// <param name="file">文件输出级别</param>
-        public Log(LogLevel console = LogLevel.All, LogLevel file = LogLevel.All)
+        /// <param name="action">日志输出回调</param>
+        public Log(LogLevel console = LogLevel.All, LogLevel file = LogLevel.All, LogEventDelegate logEvent = null)
         {
             ConsoleLogLevel = console;
             FileLogLevel = file;
+            if (logEvent != null) LogEvent += logEvent;
         }
 
         /// <summary>
@@ -288,6 +299,8 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
 
             if ((FileLogLevel & LogLevel.Verbose) == LogLevel.Verbose)
                 WriteFile(new LogModel() { Type = LogType.v, Message = msg?.ToString(), CreateTime = DateTime.Now });
+
+            try { LogEvent(LogType.v, msg?.ToString()); } catch { }
         }
         /// <summary>
         /// 输出 Debug (调试信息)
@@ -300,6 +313,8 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
 
             if ((FileLogLevel & LogLevel.Debug) == LogLevel.Debug)
                 WriteFile(new LogModel() { Type = LogType.d, Message = msg?.ToString(), CreateTime = DateTime.Now });
+
+            try { LogEvent(LogType.d, msg?.ToString()); } catch { }
         }
         /// <summary>
         /// 输出 Information (重要信息)
@@ -312,6 +327,8 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
 
             if ((FileLogLevel & LogLevel.Information) == LogLevel.Information)
                 WriteFile(new LogModel() { Type = LogType.i, Message = msg?.ToString(), CreateTime = DateTime.Now });
+
+            try { LogEvent(LogType.i, msg?.ToString()); } catch { }
         }
         /// <summary>
         /// 输出 Warning (警告信息)
@@ -324,6 +341,8 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
 
             if ((FileLogLevel & LogLevel.Warning) == LogLevel.Warning)
                 WriteFile(new LogModel() { Type = LogType.w, Message = msg?.ToString(), CreateTime = DateTime.Now });
+
+            try { LogEvent(LogType.w, msg?.ToString()); } catch { }
         }
         /// <summary>
         /// 输出 Error (错误信息)
@@ -336,6 +355,8 @@ namespace Azylee.Core.LogUtils.SimpleLogUtils
 
             if ((FileLogLevel & LogLevel.Error) == LogLevel.Error)
                 WriteFile(new LogModel() { Type = LogType.e, Message = msg?.ToString(), CreateTime = DateTime.Now });
+
+            try { LogEvent(LogType.e, msg?.ToString()); } catch { }
         }
         #endregion
     }

+ 28 - 0
Azylee.Utils/Azylee.Core/WindowsUtils/CMDUtils/CMDNetstatTool.cs

@@ -83,5 +83,33 @@ namespace Azylee.Core.WindowsUtils.CMDUtils
             }
             return result;
         }
+
+        /// <summary>
+        /// 随机获取可用的端口号
+        /// </summary>
+        /// <param name="count">需要的端口号个数</param>
+        /// <param name="start">起始端口</param>
+        /// <returns></returns>
+        public static List<int> GetAvailablePorts(byte count, int start = 52800)
+        {
+            if (count > 0)
+            {
+                List<int> ports = new List<int>();
+                int startPort = start;
+
+                List<Tuple<int, int>> list = Find(":");
+
+                for (var i = 0; i < count; i++)
+                {
+                    if (!Ls.Ok(list) || !list.Any(x => x.Item1 == startPort))
+                    {
+                        ports.Add(startPort);
+                        startPort++;
+                    }
+                    if (startPort > 65535 || ports.Count() >= count) return ports;
+                }
+            }
+            return null;
+        }
     }
 }

+ 2 - 2
Azylee.Utils/Azylee.Core/WindowsUtils/CMDUtils/CMDProcessTool.cs

@@ -20,7 +20,7 @@ namespace Azylee.Core.WindowsUtils.CMDUtils
         /// 创建cmd的进程
         /// </summary>
         /// <returns></returns>
-        public static Process GetProcess()
+        public static Process GetProcess(string verb = "RunAs")
         {
             ProcessStartInfo startInfo = new ProcessStartInfo();
             startInfo.FileName = "cmd.exe";
@@ -30,7 +30,7 @@ namespace Azylee.Core.WindowsUtils.CMDUtils
             startInfo.RedirectStandardError = true;
             startInfo.UseShellExecute = false;
             startInfo.CreateNoWindow = true;
-            startInfo.Verb = "RunAs";
+            startInfo.Verb = verb;
             Process process = new Process();
             process.StartInfo = startInfo;
             return process;

+ 6 - 1
Azylee.Utils/Tests/Test.TcpClientApp/Program.cs

@@ -1,4 +1,5 @@
-using Azylee.Jsons;
+using Azylee.Core.WindowsUtils.CMDUtils;
+using Azylee.Jsons;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -28,6 +29,10 @@ namespace Test.TcpClientApp
             byte[] d = Encoding.Unicode.GetBytes("ぬゆまほㅙㅚЪЖ");
             string __d = Encoding.Unicode.GetString(d);
 
+
+            List<int> ports = CMDNetstatTool.GetAvailablePorts(10);
+
+
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new MainForm());