This commit is contained in:
sin365 2023-12-25 17:37:33 +08:00
parent f204fe1deb
commit ced393767e
2 changed files with 48 additions and 12 deletions

View File

@ -38,11 +38,13 @@ namespace HaoYueNet.ClientNetwork
public static string LastConnectIP; public static string LastConnectIP;
public static int LastConnectPort; 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("==>初始化网络核心"); LogOut("==>初始化网络核心");
bDetailedLog = isHadDetailedLog;
RevIndex = MaxRevIndexNum; RevIndex = MaxRevIndexNum;
SendIndex = MaxSendIndexNum; SendIndex = MaxSendIndexNum;
@ -63,13 +65,21 @@ namespace HaoYueNet.ClientNetwork
//带回调的 //带回调的
try try
{ {
if(bDetailedLog)
LogOut("连接到远程IP " + IP + ":" + port); LogOut("连接到远程IP " + IP + ":" + port);
else
LogOut("连接到远程服务");
client.Connect(IP, port); client.Connect(IP, port);
Thread thread = new Thread(Recive); Thread thread = new Thread(Recive);
thread.IsBackground = true; thread.IsBackground = true;
thread.Start(client); thread.Start(client);
int localport = ((IPEndPoint)client.LocalEndPoint).Port; int localport = ((IPEndPoint)client.LocalEndPoint).Port;
if (bDetailedLog)
LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}");
else
LogOut("连接成功!");
if (_heartTimer == null) if (_heartTimer == null)
{ {
@ -79,6 +89,8 @@ namespace HaoYueNet.ClientNetwork
_heartTimer.Elapsed += CheckUpdatetimer_Elapsed; _heartTimer.Elapsed += CheckUpdatetimer_Elapsed;
_heartTimer.AutoReset = true; _heartTimer.AutoReset = true;
_heartTimer.Enabled = true; _heartTimer.Enabled = true;
if (bDetailedLog)
LogOut("开启心跳包检测"); LogOut("开启心跳包检测");
OnConnected?.Invoke(true); OnConnected?.Invoke(true);
@ -86,7 +98,11 @@ namespace HaoYueNet.ClientNetwork
} }
catch (Exception ex) catch (Exception ex)
{ {
if (bDetailedLog)
LogOut("连接失败:" + ex.ToString()); LogOut("连接失败:" + ex.ToString());
else
LogOut("连接失败");
OnConnected?.Invoke(false); OnConnected?.Invoke(false);
return false; return false;
} }
@ -216,6 +232,8 @@ namespace HaoYueNet.ClientNetwork
/// </summary> /// </summary>
private void OnCloseReady() private void OnCloseReady()
{ {
if (bDetailedLog)
LogOut("关闭心跳包计数"); LogOut("关闭心跳包计数");
_heartTimer.Enabled = false; _heartTimer.Enabled = false;
_heartTimer.Elapsed -= CheckUpdatetimer_Elapsed; _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed;

View File

@ -40,11 +40,13 @@ namespace HaoYueNet.ClientNetworkNet4x
public static string LastConnectIP; public static string LastConnectIP;
public static int LastConnectPort; 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("==>初始化网络核心"); LogOut("==>初始化网络核心");
bDetailedLog = isHadDetailedLog;
RevIndex = MaxRevIndexNum; RevIndex = MaxRevIndexNum;
SendIndex = MaxSendIndexNum; SendIndex = MaxSendIndexNum;
@ -65,13 +67,21 @@ namespace HaoYueNet.ClientNetworkNet4x
//带回调的 //带回调的
try try
{ {
if (bDetailedLog)
LogOut("连接到远程IP " + IP + ":" + port); LogOut("连接到远程IP " + IP + ":" + port);
else
LogOut("连接到远程服务");
client.Connect(IP, port); client.Connect(IP, port);
Thread thread = new Thread(Recive); Thread thread = new Thread(Recive);
thread.IsBackground = true; thread.IsBackground = true;
thread.Start(client); thread.Start(client);
int localport = ((IPEndPoint)client.LocalEndPoint).Port; int localport = ((IPEndPoint)client.LocalEndPoint).Port;
if (bDetailedLog)
LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}"); LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}");
else
LogOut("连接成功!");
if (_heartTimer == null) if (_heartTimer == null)
{ {
@ -81,6 +91,8 @@ namespace HaoYueNet.ClientNetworkNet4x
_heartTimer.Elapsed += CheckUpdatetimer_Elapsed; _heartTimer.Elapsed += CheckUpdatetimer_Elapsed;
_heartTimer.AutoReset = true; _heartTimer.AutoReset = true;
_heartTimer.Enabled = true; _heartTimer.Enabled = true;
if (bDetailedLog)
LogOut("开启心跳包检测"); LogOut("开启心跳包检测");
OnConnected?.Invoke(true); OnConnected?.Invoke(true);
@ -88,7 +100,11 @@ namespace HaoYueNet.ClientNetworkNet4x
} }
catch (Exception ex) catch (Exception ex)
{ {
if (bDetailedLog)
LogOut("连接失败:" + ex.ToString()); LogOut("连接失败:" + ex.ToString());
else
LogOut("连接失败");
OnConnected?.Invoke(false); OnConnected?.Invoke(false);
return false; return false;
} }
@ -218,6 +234,8 @@ namespace HaoYueNet.ClientNetworkNet4x
/// </summary> /// </summary>
private void OnCloseReady() private void OnCloseReady()
{ {
if (bDetailedLog)
LogOut("关闭心跳包计数"); LogOut("关闭心跳包计数");
_heartTimer.Enabled = false; _heartTimer.Enabled = false;
_heartTimer.Elapsed -= CheckUpdatetimer_Elapsed; _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed;