163 lines
6.1 KiB
C#
163 lines
6.1 KiB
C#
//////////////////////////////////////////////////////////////////////////
|
||
//
|
||
// 文件:Assets/Code/Scripts/Gameplay/Net/Client/NetManager.cs
|
||
// 作者:Xoen Xie
|
||
// 时间:2023/07/18
|
||
// 描述:网络管理
|
||
// 说明:
|
||
//
|
||
//////////////////////////////////////////////////////////////////////////
|
||
|
||
using System;
|
||
using Framework;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
|
||
namespace Gameplay.Net
|
||
{
|
||
public sealed class NetManager : Singlenton<NetManager>, IUpdatable, IInitable
|
||
{
|
||
public NetClient mClient { get; private set; }
|
||
|
||
private uint m_nClientID;
|
||
|
||
private IMessage []m_MessagePool;
|
||
|
||
public void Init()
|
||
{
|
||
DebugUtil.Log("NetManager Init start");
|
||
InitPools();
|
||
|
||
m_nClientID = 1;
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
DebugUtil.Log("NetManager Release start");
|
||
CloseClient();
|
||
}
|
||
|
||
public void CloseClient()
|
||
{
|
||
if (mClient != null)
|
||
{
|
||
mClient.Release();
|
||
mClient = null;
|
||
}
|
||
}
|
||
|
||
public NetClient CreateNetClient(string sIP, int nPort)
|
||
{
|
||
if (mClient != null)
|
||
{
|
||
DebugUtil.Log("CreateNetClient mClient.Release");
|
||
mClient.Release();
|
||
}
|
||
|
||
mClient = new NetClient();
|
||
mClient.mID = m_nClientID++;
|
||
if (!mClient.Create(sIP, nPort))
|
||
{
|
||
mClient.Release();
|
||
mClient = null;
|
||
}
|
||
|
||
mClient.Connect();
|
||
|
||
return mClient;
|
||
}
|
||
|
||
public void SendMessage(IMessage message)
|
||
{
|
||
if (mClient == null)
|
||
{
|
||
DebugUtil.LogWarning("NetManager.SendMessage mClient == null, message={0}", message.ID);
|
||
return;
|
||
}
|
||
|
||
|
||
mClient.SendMessage(message);
|
||
}
|
||
|
||
public void Update(float deltaTime)
|
||
{
|
||
if (mClient != null)
|
||
mClient.Update();
|
||
}
|
||
|
||
private void InitPools()
|
||
{
|
||
m_MessagePool = new IMessage[(int)NLDMID.MSGID.Max];
|
||
// MESSAGE_START
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGtHandshake] = new MSG_Clt_Gt_Handshake();
|
||
m_MessagePool[(int)NLDMID.MSGID.GtCltHandshake] = new MSG_Gt_Clt_Handshake();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGtHeartbeat] = new MSG_Clt_Gt_Heartbeat();
|
||
m_MessagePool[(int)NLDMID.MSGID.GtCltHeartbeat] = new MSG_Gt_Clt_Heartbeat();
|
||
m_MessagePool[(int)NLDMID.MSGID.GtCltDisconnect] = new MSG_Gt_Clt_Disconnect();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltLLogin] = new MSG_Clt_L_Login();
|
||
m_MessagePool[(int)NLDMID.MSGID.LCltLoginResult] = new MSG_L_Clt_LoginResult();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltLoginResult] = new MSG_G_Clt_LoginResult();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltOpResult] = new MSG_G_Clt_OpResult();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorBase] = new MSG_G_Clt_ActorBase();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorItem] = new MSG_G_Clt_ActorItem();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorCharacter] = new MSG_G_Clt_ActorCharacter();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGCharacterUpgrade] = new MSG_Clt_G_CharacterUpgrade();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorLogic] = new MSG_G_Clt_ActorLogic();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorTask] = new MSG_G_Clt_ActorTask();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGSetNickname] = new MSG_Clt_G_SetNickname();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorLevel] = new MSG_G_Clt_ActorLevel();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGEnterLevel] = new MSG_Clt_G_EnterLevel();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGExitLevel] = new MSG_Clt_G_ExitLevel();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorRaffle] = new MSG_G_Clt_ActorRaffle();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGRaffle] = new MSG_Clt_G_Raffle();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltItemList] = new MSG_G_Clt_ItemList();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGChapterReward] = new MSG_Clt_G_ChapterReward();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGVehicleUpgrade] = new MSG_Clt_G_VehicleUpgrade();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltActorTeam] = new MSG_G_Clt_ActorTeam();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGCreateTeam] = new MSG_Clt_G_CreateTeam();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGUseItem] = new MSG_Clt_G_UseItem();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGTestInit] = new MSG_Clt_G_TestInit();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGGiftCode] = new MSG_Clt_G_GiftCode();
|
||
m_MessagePool[(int)NLDMID.MSGID.MlCltMailInfo] = new MSG_Ml_Clt_MailInfo();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltMlMailRequest] = new MSG_Clt_Ml_MailRequest();
|
||
m_MessagePool[(int)NLDMID.MSGID.MlCltMailData] = new MSG_Ml_Clt_MailData();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltMlOpenMail] = new MSG_Clt_Ml_OpenMail();
|
||
m_MessagePool[(int)NLDMID.MSGID.MlCltNewGlobalMail] = new MSG_Ml_Clt_NewGlobalMail();
|
||
m_MessagePool[(int)NLDMID.MSGID.GCltLoginKey] = new MSG_G_Clt_LoginKey();
|
||
m_MessagePool[(int)NLDMID.MSGID.CltGRelogin] = new MSG_Clt_G_Relogin();
|
||
// MESSAGE_END
|
||
}
|
||
|
||
public IMessage GetMessage(NLDMID.MSGID id)
|
||
{
|
||
return m_MessagePool[(int)id];
|
||
}
|
||
|
||
// 服务器当前时间戳:毫秒
|
||
public ulong ServerTimeMS { get { return m_ServerTimeStart + (TimeUtils.GetTimeStampMilliseconds() - m_ClientTimeStart); } }
|
||
|
||
// 服务器当前时间戳:秒
|
||
public uint ServerTime { get { return (uint)(ServerTimeMS / 1000); } }
|
||
|
||
// 服务器发送心跳的时间
|
||
private ulong m_ServerTimeStart;
|
||
|
||
// 服务器发送心跳时,客户端的时间
|
||
private ulong m_ClientTimeStart;
|
||
|
||
// 消息从客户端 ——> 服务器 -->客户端的总时间的一半
|
||
public uint RTT { get; private set; }
|
||
|
||
public void SetTime(ulong clientTime, ulong serverTime)
|
||
{
|
||
m_ServerTimeStart = serverTime; // 服务器回复心跳的时间
|
||
|
||
ulong current = TimeUtils.GetTimeStampMilliseconds();
|
||
ulong timePass = current - clientTime;
|
||
RTT = (uint)(timePass / 2);
|
||
|
||
m_ClientTimeStart = current - RTT;
|
||
}
|
||
}
|
||
}
|