HaoYueNet/Simple/ClientCore/Network/NetworkHelper.cs

129 lines
3.6 KiB
C#
Raw Normal View History

2023-05-25 13:46:54 +08:00
using AxibugProtobuf;
using Google.Protobuf;
2023-05-25 13:46:54 +08:00
using HaoYueNet.ClientNetwork;
2021-12-10 23:38:40 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-05-25 13:46:54 +08:00
namespace ClientCore.Network
2021-12-10 23:38:40 +08:00
{
/// <summary>
/// 继承网络库,以支持网络功能
/// </summary>
public class NetworkHelper : NetworkHelperCore
{
public NetworkHelper()
{
//指定接收服务器数据事件
2023-07-06 14:47:20 +08:00
OnReceiveData += GetDataCallBack;
2021-12-10 23:38:40 +08:00
//断开连接
2021-12-13 23:53:01 +08:00
OnClose += OnConnectClose;
2021-12-10 23:38:40 +08:00
OnConnected += NetworkConnected;
2023-07-06 14:47:20 +08:00
//网络库调试信息输出事件,用于打印网络内容
OnLogOut += NetworkDeBugLog;
2021-12-10 23:38:40 +08:00
}
2023-12-14 18:15:04 +08:00
public delegate void OnReConnectedHandler();
/// <summary>
/// 重连成功事件
/// </summary>
public event OnReConnectedHandler OnReConnected;
/// <summary>
/// 是否自动重连
/// </summary>
2023-12-18 14:37:58 +08:00
public bool bAutoReConnect = false;
2023-12-14 18:15:04 +08:00
/// <summary>
/// 重连尝试时间
/// </summary>
const int ReConnectTryTime = 1000;
2021-12-10 23:38:40 +08:00
public void NetworkConnected(bool IsConnect)
{
2023-12-14 18:15:04 +08:00
NetworkDeBugLog($"NetworkConnected:{IsConnect}");
2021-12-10 23:38:40 +08:00
if (IsConnect)
2023-12-14 18:15:04 +08:00
{
}
2021-12-10 23:38:40 +08:00
else
{
2023-12-14 18:15:04 +08:00
//连接失败
NetworkDeBugLog("连接失败!");
//自动重连开关
if (bAutoReConnect)
ReConnect();
2021-12-10 23:38:40 +08:00
}
}
public void NetworkDeBugLog(string str)
{
//用于Unity内的输出
//Debug.Log("NetCoreDebug >> "+str);
Console.WriteLine("NetCoreDebug >> " + str);
}
/// <summary>
/// 接受包回调
/// </summary>
/// <param name="CMDID">协议ID</param>
/// <param name="ERRCODE">错误编号</param>
/// <param name="data">业务数据</param>
public void GetDataCallBack(int CMDID, int ERRCODE, byte[] data)
{
2021-12-13 23:53:01 +08:00
NetworkDeBugLog("收到消息 CMDID =>" + CMDID + " ERRCODE =>" + ERRCODE + " 数据长度=>" + data.Length);
2021-12-10 23:38:40 +08:00
try
{
2023-05-25 18:04:01 +08:00
//抛出网络数据
NetMsg.Instance.PostNetMsgEvent(CMDID, data);
2021-12-10 23:38:40 +08:00
}
catch (Exception ex)
{
2021-12-13 23:53:01 +08:00
NetworkDeBugLog("逻辑处理错误:" + ex.ToString());
2021-12-10 23:38:40 +08:00
}
}
/// <summary>
/// 关闭连接
/// </summary>
2021-12-13 23:53:01 +08:00
public void OnConnectClose()
2021-12-10 23:38:40 +08:00
{
2021-12-13 23:53:01 +08:00
NetworkDeBugLog("OnConnectClose");
2023-12-14 18:15:04 +08:00
//自动重连开关
if (bAutoReConnect)
ReConnect();
}
bool bInReConnecting = false;
/// <summary>
/// 自动重连
/// </summary>
void ReConnect()
{
if (bInReConnecting)
return;
bInReConnecting = true;
bool bflagDone = false;
do
{
//等待时间
Thread.Sleep(ReConnectTryTime);
App.log.Debug($"尝试自动重连{LastConnectIP}:{LastConnectPort}……");
//第一步
if (Init(LastConnectIP, LastConnectPort))
{
App.log.Debug($"自动重连成功!");
bflagDone = true;
App.log.Debug($"触发重连后的自动逻辑!");
OnReConnected?.Invoke();
}
} while (!bflagDone);
bInReConnecting = false;
2021-12-10 23:38:40 +08:00
}
}
}