2024-06-28 18:08:25 +08:00
|
|
|
|
using AxibugEmuOnline.Client.ClientCore;
|
2024-09-13 10:07:24 +08:00
|
|
|
|
using AxibugProtobuf;
|
2024-06-28 18:08:25 +08:00
|
|
|
|
using HaoYueNet.ClientNetworkNet.Standard2;
|
|
|
|
|
using System;
|
2024-09-12 13:41:19 +08:00
|
|
|
|
using System.Net.Sockets;
|
2024-06-28 18:08:25 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace AxibugEmuOnline.Client.Network
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 继承网络库,以支持网络功能
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class NetworkHelper : NetworkHelperCore
|
|
|
|
|
{
|
|
|
|
|
public NetworkHelper()
|
|
|
|
|
{
|
|
|
|
|
//指定接收服务器数据事件
|
|
|
|
|
OnReceiveData += GetDataCallBack;
|
|
|
|
|
//断开连接
|
|
|
|
|
OnClose += OnConnectClose;
|
|
|
|
|
OnConnected += NetworkConnected;
|
|
|
|
|
//网络库调试信息输出事件,用于打印网络内容
|
|
|
|
|
OnLogOut += NetworkDeBugLog;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delegate void OnReConnectedHandler();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 重连成功事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event OnReConnectedHandler OnReConnected;
|
2024-09-12 13:41:19 +08:00
|
|
|
|
public bool isConnected => CheckIsConnectd();
|
2024-06-28 18:08:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否自动重连
|
|
|
|
|
/// </summary>
|
2024-09-13 10:07:24 +08:00
|
|
|
|
public bool bAutoReConnect = true;
|
2024-06-28 18:08:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 重连尝试时间
|
|
|
|
|
/// </summary>
|
|
|
|
|
const int ReConnectTryTime = 1000;
|
|
|
|
|
|
|
|
|
|
public void NetworkConnected(bool IsConnect)
|
|
|
|
|
{
|
|
|
|
|
NetworkDeBugLog($"NetworkConnected:{IsConnect}");
|
|
|
|
|
if (IsConnect)
|
|
|
|
|
{
|
|
|
|
|
//从未登录过
|
2024-09-11 18:31:25 +08:00
|
|
|
|
if (!App.user.IsLoggedIn)
|
2024-06-28 18:08:25 +08:00
|
|
|
|
{
|
|
|
|
|
//首次登录
|
2024-09-11 18:31:25 +08:00
|
|
|
|
App.login.Login();
|
2024-06-28 18:08:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//连接失败
|
|
|
|
|
NetworkDeBugLog("连接失败!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//自动重连开关
|
|
|
|
|
if (bAutoReConnect)
|
|
|
|
|
ReConnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void NetworkDeBugLog(string str)
|
|
|
|
|
{
|
|
|
|
|
//用于Unity内的输出
|
|
|
|
|
//Debug.Log("NetCoreDebug >> "+str);
|
2024-09-11 18:31:25 +08:00
|
|
|
|
App.log.Info("NetCoreDebug >> " + str);
|
2024-06-28 18:08:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 接受包回调
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="CMDID">协议ID</param>
|
|
|
|
|
/// <param name="ERRCODE">错误编号</param>
|
|
|
|
|
/// <param name="data">业务数据</param>
|
|
|
|
|
public void GetDataCallBack(int CMDID, int ERRCODE, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
//NetworkDeBugLog("收到消息 CMDID =>" + CMDID + " ERRCODE =>" + ERRCODE + " 数据长度=>" + data.Length);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//抛出网络数据
|
2024-09-13 10:07:24 +08:00
|
|
|
|
//网络线程直接抛
|
2024-09-13 13:28:33 +08:00
|
|
|
|
if (CMDID == (int)CommandID.CmdPing || CMDID == (int)CommandID.CmdPong)
|
|
|
|
|
NetMsg.Instance.PostNetMsgEvent(CMDID, ERRCODE, data);
|
2024-09-13 10:07:24 +08:00
|
|
|
|
else//加入队列,主线程来取
|
|
|
|
|
NetMsg.Instance.EnqueueNesMsg(CMDID, ERRCODE, data);
|
2024-06-28 18:08:25 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
NetworkDeBugLog("逻辑处理错误:" + ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void OnConnectClose()
|
|
|
|
|
{
|
|
|
|
|
NetworkDeBugLog("OnConnectClose");
|
|
|
|
|
|
2024-09-12 13:41:19 +08:00
|
|
|
|
App.user.LoginOutData();
|
2024-06-28 18:08:25 +08:00
|
|
|
|
|
|
|
|
|
//自动重连开关
|
|
|
|
|
if (bAutoReConnect)
|
|
|
|
|
ReConnect();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 13:28:33 +08:00
|
|
|
|
Thread ReConnectTask = null;
|
2024-06-28 18:08:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自动重连
|
|
|
|
|
/// </summary>
|
|
|
|
|
void ReConnect()
|
|
|
|
|
{
|
2024-09-13 10:07:24 +08:00
|
|
|
|
if (ReConnectTask != null)
|
2024-06-28 18:08:25 +08:00
|
|
|
|
return;
|
2024-09-13 13:28:33 +08:00
|
|
|
|
ReConnectTask = new Thread(() =>
|
2024-06-28 18:08:25 +08:00
|
|
|
|
{
|
2024-09-13 10:07:24 +08:00
|
|
|
|
bool bflagDone = false;
|
|
|
|
|
do
|
2024-06-28 18:08:25 +08:00
|
|
|
|
{
|
2024-09-13 10:07:24 +08:00
|
|
|
|
//等待时间
|
|
|
|
|
Thread.Sleep(ReConnectTryTime);
|
|
|
|
|
App.log.Info($"尝试自动重连{LastConnectIP}:{LastConnectPort}……");
|
|
|
|
|
//第一步
|
|
|
|
|
if (Init(LastConnectIP, LastConnectPort))
|
|
|
|
|
{
|
|
|
|
|
App.log.Info($"自动重连成功!");
|
|
|
|
|
bflagDone = true;
|
|
|
|
|
App.log.Info($"触发重连后的自动逻辑!");
|
|
|
|
|
OnReConnected?.Invoke();
|
|
|
|
|
}
|
2024-09-13 13:28:33 +08:00
|
|
|
|
} while (!bflagDone && App.network.bAutoReConnect);
|
|
|
|
|
|
|
|
|
|
if (!App.network.bAutoReConnect)
|
|
|
|
|
{
|
|
|
|
|
if (App.network.isConnected)
|
|
|
|
|
{
|
|
|
|
|
App.network.CloseConntect();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-13 10:07:24 +08:00
|
|
|
|
ReConnectTask = null;
|
2024-09-13 13:28:33 +08:00
|
|
|
|
});
|
2024-09-13 10:07:24 +08:00
|
|
|
|
ReConnectTask.Start();
|
2024-06-28 18:08:25 +08:00
|
|
|
|
}
|
2024-09-12 13:41:19 +08:00
|
|
|
|
|
|
|
|
|
bool CheckIsConnectd()
|
|
|
|
|
{
|
|
|
|
|
Socket socket = GetClientSocket();
|
2024-09-13 13:28:33 +08:00
|
|
|
|
if (socket == null)
|
2024-09-12 13:41:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
return socket.Connected;
|
|
|
|
|
}
|
2024-06-28 18:08:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|