From ced393767e0b0afadad02c5243b8eac601cb4daf Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Mon, 25 Dec 2023 17:37:33 +0800 Subject: [PATCH] fix --- .../NetworkHelperCore.cs | 30 +++++++++++++++---- .../NetworkHelperCore.cs | 30 +++++++++++++++---- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs index 5eea8bc..0d7a3b3 100644 --- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs @@ -38,11 +38,13 @@ namespace HaoYueNet.ClientNetwork public static string LastConnectIP; public static int LastConnectPort; + public bool bDetailedLog = false; - public bool Init(string IP, int port, bool bBindReuseAddress = false,int bBindport = 0) + public bool Init(string IP, int port,bool isHadDetailedLog = true, bool bBindReuseAddress = false,int bBindport = 0) { LogOut("==>初始化网络核心"); + bDetailedLog = isHadDetailedLog; RevIndex = MaxRevIndexNum; SendIndex = MaxSendIndexNum; @@ -63,13 +65,21 @@ namespace HaoYueNet.ClientNetwork //带回调的 try { - LogOut("连接到远程IP " + IP + ":" + port); + if(bDetailedLog) + LogOut("连接到远程IP " + IP + ":" + port); + else + LogOut("连接到远程服务"); + client.Connect(IP, port); Thread thread = new Thread(Recive); thread.IsBackground = true; thread.Start(client); int localport = ((IPEndPoint)client.LocalEndPoint).Port; - LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); + + if (bDetailedLog) + LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); + else + LogOut("连接成功!"); if (_heartTimer == null) { @@ -79,14 +89,20 @@ namespace HaoYueNet.ClientNetwork _heartTimer.Elapsed += CheckUpdatetimer_Elapsed; _heartTimer.AutoReset = true; _heartTimer.Enabled = true; - LogOut("开启心跳包检测"); + + if (bDetailedLog) + LogOut("开启心跳包检测"); OnConnected?.Invoke(true); return true; } catch (Exception ex) { - LogOut("连接失败:" + ex.ToString()); + if (bDetailedLog) + LogOut("连接失败:" + ex.ToString()); + else + LogOut("连接失败"); + OnConnected?.Invoke(false); return false; } @@ -216,7 +232,9 @@ namespace HaoYueNet.ClientNetwork /// private void OnCloseReady() { - LogOut("关闭心跳包计数"); + + if (bDetailedLog) + LogOut("关闭心跳包计数"); _heartTimer.Enabled = false; _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed; LogOut("关闭连接"); diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs index 5d7aa03..1831bf8 100644 --- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs @@ -40,11 +40,13 @@ namespace HaoYueNet.ClientNetworkNet4x public static string LastConnectIP; public static int LastConnectPort; + public bool bDetailedLog = false; - public bool Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0) + public bool Init(string IP, int port, bool isHadDetailedLog = true, bool bBindReuseAddress = false, int bBindport = 0) { LogOut("==>初始化网络核心"); + bDetailedLog = isHadDetailedLog; RevIndex = MaxRevIndexNum; SendIndex = MaxSendIndexNum; @@ -65,13 +67,21 @@ namespace HaoYueNet.ClientNetworkNet4x //带回调的 try { - LogOut("连接到远程IP " + IP + ":" + port); + if (bDetailedLog) + LogOut("连接到远程IP " + IP + ":" + port); + else + LogOut("连接到远程服务"); + client.Connect(IP, port); Thread thread = new Thread(Recive); thread.IsBackground = true; thread.Start(client); int localport = ((IPEndPoint)client.LocalEndPoint).Port; - LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); + + if (bDetailedLog) + LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); + else + LogOut("连接成功!"); if (_heartTimer == null) { @@ -81,14 +91,20 @@ namespace HaoYueNet.ClientNetworkNet4x _heartTimer.Elapsed += CheckUpdatetimer_Elapsed; _heartTimer.AutoReset = true; _heartTimer.Enabled = true; - LogOut("开启心跳包检测"); + + if (bDetailedLog) + LogOut("开启心跳包检测"); OnConnected?.Invoke(true); return true; } catch (Exception ex) { - LogOut("连接失败:" + ex.ToString()); + if (bDetailedLog) + LogOut("连接失败:" + ex.ToString()); + else + LogOut("连接失败"); + OnConnected?.Invoke(false); return false; } @@ -218,7 +234,9 @@ namespace HaoYueNet.ClientNetworkNet4x /// private void OnCloseReady() { - LogOut("关闭心跳包计数"); + + if (bDetailedLog) + LogOut("关闭心跳包计数"); _heartTimer.Enabled = false; _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed; LogOut("关闭连接");