Browse Source

Merge branch 'master' of https://github.com/yuzhengyang/Fork

于正洋 5 years ago
parent
commit
b7cc34688a

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

@@ -32,6 +32,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="PresentationFramework" />
     <Reference Include="System" />
     <Reference Include="System.configuration" />
     <Reference Include="System.Core" />
@@ -157,6 +158,7 @@
     <Compile Include="WindowsUtils\APIUtils\ApplicationAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\ExplorerAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
+    <Compile Include="WindowsUtils\APIUtils\ScreenAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\SystemSleepAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\WallpaperUtils\WallpaperTool.cs" />
     <Compile Include="WindowsUtils\APIUtils\WindowsAPI.cs" />

+ 2 - 2
Azylee.Utils/Azylee.Core/DrawingUtils/ImageUtils/Img.cs

@@ -53,7 +53,7 @@ namespace Azylee.Core.DrawingUtils.ImageUtils
         /// <param name="quality">0-100</param>
         /// <param name="format"></param>
         /// <returns></returns>
-        public static byte[] Compression(Bitmap bitmap, int quality)
+        public static byte[] CompressionToByte(Image image, int quality)
         {
             try
             {
@@ -74,7 +74,7 @@ namespace Azylee.Core.DrawingUtils.ImageUtils
                     myEncoderParameters.Param[0] = myEncoderParameter;
                     using (MemoryStream ms = new MemoryStream())
                     {
-                        bitmap.Save(ms, CodecInfo, myEncoderParameters);
+                        image.Save(ms, CodecInfo, myEncoderParameters);
                         myEncoderParameters.Dispose();
                         myEncoderParameter.Dispose();
                         return ms.ToArray();

+ 7 - 6
Azylee.Utils/Azylee.Core/DrawingUtils/ImageUtils/ScreenCapture.cs

@@ -2,13 +2,16 @@
 //      https://github.com/yuzhengyang
 //      author:yuzhengyang
 //############################################################
+using Azylee.Core.WindowsUtils.APIUtils;
 using System;
 using System.Collections.Generic;
 using System.Drawing;
+using System.Drawing.Imaging;
 using System.Linq;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
 using System.Windows.Forms;
 
 namespace Azylee.Core.DrawingUtils.ImageUtils
@@ -26,16 +29,14 @@ namespace Azylee.Core.DrawingUtils.ImageUtils
         {
             try
             {
-                //屏幕宽
-                int iWidth = Screen.PrimaryScreen.Bounds.Width;
-                //屏幕高
-                int iHeight = Screen.PrimaryScreen.Bounds.Height;
+                //获取屏幕分辨率
+                Size size = ScreenAPI.DESKTOP;
                 //按照屏幕宽高创建位图
-                Bitmap bitmap = new Bitmap(iWidth, iHeight);
+                Bitmap bitmap = new Bitmap(size.Width, size.Height);
                 //从一个继承自Image类的对象中创建Graphics对象
                 Graphics gc = Graphics.FromImage(bitmap);
                 //抓屏并拷贝到myimage里
-                gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
+                gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), size);
                 gc.Dispose();
                 return bitmap;
             }

+ 1 - 1
Azylee.Utils/Azylee.Core/IOUtils/TxtUtils/ConfigTool.cs

@@ -97,7 +97,7 @@ namespace Azylee.Core.IOUtils.TxtUtils
                 ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
                 return true;
             }
-            catch { return false; }
+            catch (Exception ex) { return false; }
         }
         public static bool SetExe(string exePath, string key, string value)
         {

+ 123 - 0
Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/ScreenAPI.cs

@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+
+namespace Azylee.Core.WindowsUtils.APIUtils
+{
+    /// <summary>
+    /// 屏幕分辨率接口
+    /// </summary>
+    public class ScreenAPI
+    {
+        #region Win32 API
+        [DllImport("user32.dll")]
+        static extern IntPtr GetDC(IntPtr ptr);
+        [DllImport("gdi32.dll")]
+        static extern int GetDeviceCaps(
+        IntPtr hdc, // handle to DC
+        int nIndex // index of capability
+        );
+        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
+        static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
+        #endregion
+        #region DeviceCaps常量
+        const int HORZRES = 8;
+        const int VERTRES = 10;
+        const int LOGPIXELSX = 88;
+        const int LOGPIXELSY = 90;
+        const int DESKTOPVERTRES = 117;
+        const int DESKTOPHORZRES = 118;
+        #endregion
+
+        #region 属性
+        /// <summary>
+        /// 获取屏幕分辨率当前物理大小
+        /// </summary>
+        public static Size WorkingArea
+        {
+            get
+            {
+                IntPtr hdc = GetDC(IntPtr.Zero);
+                Size size = new Size();
+                size.Width = GetDeviceCaps(hdc, HORZRES);
+                size.Height = GetDeviceCaps(hdc, VERTRES);
+                ReleaseDC(IntPtr.Zero, hdc);
+                return size;
+            }
+        }
+        /// <summary>
+        /// 当前系统DPI_X 大小 一般为96
+        /// </summary>
+        public static int DpiX
+        {
+            get
+            {
+                IntPtr hdc = GetDC(IntPtr.Zero);
+                int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
+                ReleaseDC(IntPtr.Zero, hdc);
+                return DpiX;
+            }
+        }
+        /// <summary>
+        /// 当前系统DPI_Y 大小 一般为96
+        /// </summary>
+        public static int DpiY
+        {
+            get
+            {
+                IntPtr hdc = GetDC(IntPtr.Zero);
+                int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
+                ReleaseDC(IntPtr.Zero, hdc);
+                return DpiX;
+            }
+        }
+        /// <summary>
+        /// 获取真实设置的桌面分辨率大小
+        /// </summary>
+        public static Size DESKTOP
+        {
+            get
+            {
+                IntPtr hdc = GetDC(IntPtr.Zero);
+                Size size = new Size();
+                size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
+                size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
+                ReleaseDC(IntPtr.Zero, hdc);
+                return size;
+            }
+        }
+
+        /// <summary>
+        /// 获取宽度缩放百分比
+        /// </summary>
+        public static float ScaleX
+        {
+            get
+            {
+                IntPtr hdc = GetDC(IntPtr.Zero);
+                int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
+                int d = GetDeviceCaps(hdc, HORZRES);
+                float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
+                ReleaseDC(IntPtr.Zero, hdc);
+                return ScaleX;
+            }
+        }
+        /// <summary>
+        /// 获取高度缩放百分比
+        /// </summary>
+        public static float ScaleY
+        {
+            get
+            {
+                IntPtr hdc = GetDC(IntPtr.Zero);
+                float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
+                ReleaseDC(IntPtr.Zero, hdc);
+                return ScaleY;
+            }
+        }
+        #endregion
+    }
+}

+ 1 - 1
Azylee.Utils/Azylee.Jsons/Json.cs

@@ -108,7 +108,7 @@ namespace Azylee.Jsons
                 string s = Encoding.UTF8.GetString(b);
                 return JsonConvert.DeserializeObject<T>(s);
             }
-            catch { return default(T); }
+            catch(Exception ex) { return default(T); }
         }
     }
 }

+ 2 - 1
Azylee.Utils/Azylee.YeahWeb/Azylee.YeahWeb.csproj

@@ -55,6 +55,8 @@
     <Compile Include="EmailUtils\EmailTool.cs" />
     <Compile Include="ExtWebAPI\BingWebAPI\WallpaperUtils\WallpaperWebModel.cs" />
     <Compile Include="ExtWebAPI\BingWebAPI\WallpaperUtils\WallpaperTool.cs" />
+    <Compile Include="ExtWebAPI\IPAddressAPI\PublicIPAddressTool.cs" />
+    <Compile Include="ExtWebAPI\IPAddressAPI\PublicIPAddressModel.cs" />
     <Compile Include="FTPUtils\FTPTool.cs" />
     <Compile Include="HttpUtils\MethodUtils\ExtendUtils\HeaderTool.cs" />
     <Compile Include="HttpUtils\MethodUtils\GetUtils\GetToolPlus.cs" />
@@ -72,7 +74,6 @@
     <Compile Include="SocketUtils\TcpUtils\TcpDataConverter.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcpDataModel.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcppServer.cs" />
-    <Compile Include="SocketUtils\TcpUtils\TcpDelegate.cs" />
     <Compile Include="SocketUtils\TcpUtils\TcpStreamHelper.cs" />
     <Compile Include="ExtWebAPI\TencentWebAPI\PictureAI\PictureScener.cs" />
   </ItemGroup>

+ 24 - 0
Azylee.Utils/Azylee.YeahWeb/ExtWebAPI/IPAddressAPI/PublicIPAddressModel.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.YeahWeb.ExtWebAPI.IPAddressAPI
+{
+    public class PublicIPAddressModel
+    {
+        public string IP { get; set; }
+        public string ID { get; set; }
+        public string Name { get; set; }
+        public PublicIPAddressModel()
+        {
+
+        }
+        public PublicIPAddressModel(string ip, string id, string name)
+        {
+            this.IP = ip;
+            this.ID = id;
+            this.Name = name;
+        }
+    }
+}

+ 46 - 0
Azylee.Utils/Azylee.YeahWeb/ExtWebAPI/IPAddressAPI/PublicIPAddressTool.cs

@@ -0,0 +1,46 @@
+using Azylee.Core.DataUtils.StringUtils;
+using Azylee.Jsons;
+using Azylee.YeahWeb.HttpUtils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.YeahWeb.ExtWebAPI.IPAddressAPI
+{
+    /// <summary>
+    /// 公网IP地址API
+    /// </summary>
+    public class PublicIPAddressTool
+    {
+        const string URL = "http://pv.sohu.com/cityjson?ie=utf-8";
+
+        /// <summary>
+        /// 获取公网IP地址API
+        /// </summary>
+        /// <returns></returns>
+        public static PublicIPAddressModel GetPublicIP()
+        {
+            string rs = HttpTool.Get(URL);
+            if (Str.Ok(rs))
+            {
+                int flagbeg = rs.IndexOf("{");
+                int flagend = rs.LastIndexOf("}");
+                if (flagbeg < flagend)
+                {
+                    rs = rs.Substring(flagbeg, flagend - flagbeg + 1);
+                    Dictionary<string, string> model = Json.String2Object<Dictionary<string, string>>(rs);
+                    if (model != null)
+                    {
+                        model.TryGetValue("cip", out string cip);
+                        model.TryGetValue("cid", out string cid);
+                        model.TryGetValue("cname", out string cname);
+                        PublicIPAddressModel result = new PublicIPAddressModel(cip, cid, cname);
+                        return result;
+                    }
+                }
+            }
+            return null;
+        }
+    }
+}

+ 60 - 3
Azylee.Utils/Azylee.YeahWeb/SocketUtils/TcpUtils/TcpClientInfo.cs

@@ -6,13 +6,29 @@ using System.Text;
 
 namespace Azylee.YeahWeb.SocketUtils.TcpUtils
 {
+    /// <summary>
+    /// 客户端信息
+    /// </summary>
     public class TcpClientInfo
     {
+        #region 连接基础信息
         /// <summary>
-        /// 唯一编号
+        /// 唯一编号(每次登录都不一样)
         /// </summary>
         public int Number { get; set; }
         /// <summary>
+        /// 客户端应用程序编码(应用程序区分)
+        /// </summary>
+        public string AppCode { get; set; }
+        /// <summary>
+        /// 连接密钥
+        /// </summary>
+        public string ConnectKey { get; set; }
+        /// <summary>
+        /// 客户端远程终结点IP
+        /// </summary>
+        public string IP { get; set; }
+        /// <summary>
         /// 客户端远程终结点(IP:Port)
         /// </summary>
         public string Host { get; set; }
@@ -24,10 +40,43 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         /// 是否连接
         /// </summary>
         public bool IsConnect { get; set; }
+        #endregion
+
+        #region 用户信息及认证
         /// <summary>
-        /// 连接密钥
+        /// 权限编码(可扩展权限管理功能)
+        /// </summary>
+        public string AccessCode { get; set; }
+        /// <summary>
+        /// 用户邮箱
+        /// </summary>
+        public string UserEmail { get; set; }
+        /// <summary>
+        /// 用户名称
         /// </summary>
-        public string  ConnectKey { get; set; }
+        public string UserName { get; set; }
+        /// <summary>
+        /// 主机ID
+        /// </summary>
+        public string MachineID { get; set; }
+        /// <summary>
+        /// 主机名称
+        /// </summary>
+        public string MachineName { get; set; }
+        #endregion
+
+        #region 扩展信息
+        /// <summary>
+        /// 公网IP地址
+        /// </summary>
+        //public string  PublicIP { get; set; }
+        /// <summary>
+        /// 扩展数据
+        /// </summary>
+        //public Dictionary<string, string> ExtData { get; set; }
+        #endregion
+
+        #region 流量管理
         /// <summary>
         /// 上行流量总计
         /// </summary>
@@ -45,8 +94,16 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         /// </summary>
         public DateTime LastDownloadTime { get; set; }
         /// <summary>
+        /// 心跳通信时间
+        /// </summary>
+        public DateTime HeartbeatTime { get; set; }
+        #endregion
+
+        #region 连接对象
+        /// <summary>
         /// 客户端对象
         /// </summary>
         public TcpClient Client { get; set; }
+        #endregion
     }
 }

+ 172 - 0
Azylee.Utils/Azylee.YeahWeb/SocketUtils/TcpUtils/TcpClientManager.cs

@@ -7,6 +7,9 @@ using System.Text;
 
 namespace Azylee.YeahWeb.SocketUtils.TcpUtils
 {
+    /// <summary>
+    /// 客户端信息管理器
+    /// </summary>
     public class TcpClientManager
     {
         private int HostNumber { get; set; }
@@ -98,9 +101,14 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
                 var item = TcpClientList.FirstOrDefault(x => x.Host == host);
                 if (item == null)
                 {
+                    string ip = "";
+                    int ipFlagIndex = host.IndexOf(":");
+                    if (ipFlagIndex > 0) ip = host.Substring(0, ipFlagIndex);
+
                     var model = new TcpClientInfo()
                     {
                         Number = HostNumber,
+                        IP = ip,
                         Host = host,
                         Client = client,
                         IsConnect = true,
@@ -120,6 +128,12 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         #endregion
 
         #region 更新项
+        /// <summary>
+        /// 更新 ConnectKey 连接秘钥
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
         public bool UpdateConnectKey(string host, string s)
         {
             if (IsExistByHost(host))
@@ -135,6 +149,138 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
             }
             return false;
         }
+        /// <summary>
+        /// 更新 UserEmail 用户邮箱
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public bool UpdateUserEmail(string host, string s)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].UserEmail = s;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 更新 AccessCode 权限编码
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public bool UpdateAccessCode(string host, string s)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].AccessCode = s;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 更新 MachineID 主机ID
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public bool UpdateMachineID(string host, string s)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].MachineID = s;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 更新 MachineName 主机名称
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public bool UpdateMachineName(string host, string s)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].MachineName = s;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 更新 UserName 用户名
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public bool UpdateUserName(string host, string s)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].UserName = s;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 更新 AppCode 应用程序编码
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public bool UpdateAppCode(string host, string s)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].AppCode = s;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        /// <summary>
+        /// 更新上行流量
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="flow"></param>
+        /// <returns></returns>
         public bool UpdateUploadFlowCount(string host, long flow)
         {
             if (IsExistByHost(host))
@@ -151,6 +297,12 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
             }
             return false;
         }
+        /// <summary>
+        /// 更新下行流量
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="flow"></param>
+        /// <returns></returns>
         public bool UpdateDownloadFlowCount(string host, long flow)
         {
             if (IsExistByHost(host))
@@ -167,6 +319,26 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
             }
             return false;
         }
+        /// <summary>
+        /// 更新心跳时间
+        /// </summary>
+        /// <param name="host"></param>
+        /// <returns></returns>
+        public bool UpdateHeartbeatTime(string host)
+        {
+            if (IsExistByHost(host))
+            {
+                for (var i = 0; i < TcpClientList.Count; i++)
+                {
+                    if (TcpClientList[i].Host == host)
+                    {
+                        TcpClientList[i].HeartbeatTime = DateTime.Now;
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
         #endregion
 
         #region 删除项

+ 34 - 3
Azylee.Utils/Azylee.YeahWeb/SocketUtils/TcpUtils/TcpDataModel.cs

@@ -22,6 +22,39 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         /// </summary>
         public byte[] Data { get; set; }
         /// <summary>
+        /// 默认构造函数
+        /// </summary>
+        public TcpDataModel() { }
+        /// <summary>
+        /// 构造函数(仅指令)
+        /// </summary>
+        /// <param name="type"></param>
+        public TcpDataModel(int type)
+        {
+            this.Type = type;
+        }
+        /// <summary>
+        /// 构造函数(默认)
+        /// </summary>
+        /// <param name="type"></param>
+        /// <param name="data"></param>
+        public TcpDataModel(int type, byte[] data)
+        {
+            this.Type = type;
+            this.Data = data;
+        }
+        /// <summary>
+        /// 构造函数(自动转换)
+        /// </summary>
+        /// <param name="type"></param>
+        /// <param name="data"></param>
+        public TcpDataModel(int type, object data)
+        {
+            this.Type = type;
+            this.Data = Json.Object2Byte(data);
+        }
+
+        /// <summary>
         /// 将当前模型转换为 byte 数组
         /// </summary>
         /// <returns></returns>
@@ -56,9 +89,7 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
 
                 byte[] data_byte = bytes.Skip(8).Take(length).ToArray();
 
-                model = new TcpDataModel();
-                model.Type = type;
-                model.Data = data_byte;
+                model = new TcpDataModel(type, data_byte);
             }
             catch { }
             return model;

+ 0 - 31
Azylee.Utils/Azylee.YeahWeb/SocketUtils/TcpUtils/TcpDelegate.cs

@@ -1,31 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Azylee.YeahWeb.SocketUtils.TcpUtils
-{
-    /// <summary>
-    /// Tcp 工具 委托声明
-    /// </summary>
-    [Obsolete]
-    public class TcpDelegate
-    {
-        /// <summary>
-        /// 接受消息委托
-        /// </summary>
-        /// <param name="host"></param>
-        /// <param name="model"></param>
-        public delegate void ReceiveMessage(string host, TcpDataModel model);
-        /// <summary>
-        /// Tcp 连接消息委托
-        /// </summary>
-        /// <param name="host"></param>
-        public delegate void OnConnect(string host);
-        /// <summary>
-        /// Tcp 断开连接消息委托
-        /// </summary>
-        /// <param name="host"></param>
-        public delegate void OnDisconnect(string host);
-    }
-}

+ 33 - 24
Azylee.Utils/Azylee.YeahWeb/SocketUtils/TcpUtils/TcppClient.cs

@@ -1,7 +1,9 @@
-using Azylee.Core.ThreadUtils.SleepUtils;
+using Azylee.Core.DataUtils.CollectionUtils;
+using Azylee.Core.ThreadUtils.SleepUtils;
 using Azylee.Core.WindowsUtils.ConsoleUtils;
 using Azylee.Jsons;
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net.Sockets;
@@ -21,6 +23,7 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         Action OnConnectAction = null;
         Action OnDisconnectAction = null;
         Action<TcpDataModel> OnReceiveAction = null;
+        ConcurrentQueue<Tuple<int, Action<TcpDataModel>>> SyncFunction = new ConcurrentQueue<Tuple<int, Action<TcpDataModel>>>();
 
         /// <summary>
         /// 构造函数
@@ -72,37 +75,29 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
 
         #region 连接后的读写操作
         /// <summary>
-        /// 发送数据
+        /// 发送数据(action禁止使用阻塞操作,必须新建task线程操作)
         /// </summary>
         /// <param name="model">数据模型</param>
-        public bool Write(TcpDataModel model)
+        /// <param name="actionType">事件驱动处理类型</param>
+        /// <param name="action">事件驱动处理方法</param>
+        public bool Write(TcpDataModel model, int? actionType = 0, Action<TcpDataModel> action = null)
         {
             bool flag = false;
-            if (this.Client != null && this.Client.Connected)
+            if (Client != null && Client.Connected)
             {
                 flag = TcpStreamHelper.Write(Client, model);
             }
+            if (flag)
+            {
+                if (actionType != null && action != null)
+                {
+                    int type = actionType.GetValueOrDefault();
+                    SyncFunction.Enqueue(new Tuple<int, Action<TcpDataModel>>(type, action));
+                }
+            }
             return flag;
         }
         /// <summary>
-        /// 发送数据
-        /// </summary>
-        /// <param name="type">类型</param>
-        /// <param name="data">数据</param>
-        public bool Write(int type, byte[] data)
-        {
-            return Write(new TcpDataModel() { Type = type, Data = data });
-        }
-        /// <summary>
-        /// 发送数据
-        /// </summary>
-        /// <param name="type">类型</param>
-        /// <param name="s">字符串</param>
-        public bool Write(int type, string s)
-        {
-            return Write(new TcpDataModel() { Type = type, Data = Json.Object2Byte(s) });
-        }
-        /// <summary>
         /// 接受数据
         /// </summary>
         private void acceptCallback(IAsyncResult state)
@@ -134,11 +129,25 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
                             if (model.Type == int.MaxValue)
                             {
                                 //返回心跳
-                                Write(new TcpDataModel() { Type = int.MaxValue });
+                                Write(new TcpDataModel(int.MaxValue));
                             }
                             else
                             {
-                                OnReceiveAction?.Invoke(model);//委托:接收消息
+                                //优先调用默认接收消息方法Action
+                                OnReceiveAction?.Invoke(model);
+
+                                //调用同步处理委托方法
+                                if (Ls.Ok(SyncFunction))
+                                {
+                                    for (var i = 0; i < SyncFunction.Count; i++)
+                                    {
+                                        bool flag = SyncFunction.TryDequeue(out Tuple<int, Action<TcpDataModel>> fun);
+                                        if (flag)
+                                        {
+                                            Task.Factory.StartNew(() => { fun.Item2?.Invoke(model); });
+                                        }
+                                    }
+                                }
                             }
                         }
                     }

+ 6 - 25
Azylee.Utils/Azylee.YeahWeb/SocketUtils/TcpUtils/TcppServer.cs

@@ -70,7 +70,7 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         /// </summary>
         /// <param name="host">主机地址</param>
         /// <param name="model">数据模型</param>
-        public void Write(string host, TcpDataModel model)
+        public bool Write(string host, TcpDataModel model)
         {
             var dictionary = TcpClientManager.GetInfoByHost(host);
             if (dictionary != null && dictionary.Client != null)
@@ -79,28 +79,10 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
                 {
                     TcpClientManager.UpdateUploadFlowCount(host, model.Data.Length);
                     bool flag = TcpStreamHelper.Write(dictionary.Client, model);
+                    return flag;
                 }
             }
-        }
-        /// <summary>
-        /// 发送数据
-        /// </summary>
-        /// <param name="host">主机地址</param>
-        /// <param name="type">类型</param>
-        /// <param name="data">数据</param>
-        public void Write(string host, int type, byte[] data)
-        {
-            Write(host, new TcpDataModel() { Type = type, Data = data });
-        }
-        /// <summary>
-        /// 发送数据
-        /// </summary>
-        /// <param name="host">主机地址</param>
-        /// <param name="type">类型</param>
-        /// <param name="s">字符串</param>
-        public void Write(string host, int type, string s)
-        {
-            Write(host, new TcpDataModel() { Type = type, Data = Json.Object2Byte(s) });
+            return false;
         }
 
         private void acceptCallback(IAsyncResult state)
@@ -121,14 +103,13 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
         private void ConnectTask(string host, TcpClient client)
         {
             TcpClientInfo clientInfo = TcpClientManager.GetInfoByHost(host);
-            DateTime HeartbeatTime = DateTime.Now;
 
             //发送心跳
             Task.Factory.StartNew(() =>
             {
                 while (client.Connected)
                 {
-                    TcpDataModel model = new TcpDataModel() { Type = int.MaxValue };
+                    TcpDataModel model = new TcpDataModel(int.MaxValue);
                     TcpStreamHelper.Write(client, model);
 
                     Sleep.S(5);
@@ -150,8 +131,8 @@ namespace Azylee.YeahWeb.SocketUtils.TcpUtils
                         {
                             if (model.Type == int.MaxValue)
                             {
-                                //过滤心跳
-                                HeartbeatTime = DateTime.Now;
+                                //过滤心跳,并记录心跳时间
+                                TcpClientManager.UpdateHeartbeatTime(host);
                             }
                             else
                             {