NetworkMgr.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using LuaInterface;
  8. public delegate void NetConnectedEvent(object sender);
  9. public delegate void NetDisconnectEvent(object sender);
  10. public delegate void NetMsgDelegate(NetPkg msg);
  11. public delegate void NetConnectorEvent(string log);
  12. public class NetworkMgr : Singleton<NetworkMgr>
  13. {
  14. public const UInt16 c_RSA_Encrypt = 1;
  15. public const UInt16 c_AES_Encrypt = 2;
  16. public const UInt16 c_pingCmdId = 1000;
  17. public const UInt16 c_pingCmdAck = 1001;
  18. public const UInt16 c_reconnectCmdAck = 1005;
  19. public GameServerConnector gameSrv = new GameServerConnector();
  20. public int m_GameReconnetCount = 0;
  21. // 是否是联网模式
  22. private bool bOnlineMode = true;
  23. public bool isOnlineMode
  24. {
  25. get { return bOnlineMode; }
  26. set { bOnlineMode = value; }
  27. }
  28. // 上一次发送心跳的时间
  29. private float mLastHeartTime;
  30. private float mLastHeartTimeWait;
  31. private int mHeartAckWaitNum = 0;
  32. private int mHeartDelayTime = 9999;
  33. // 毫秒值
  34. public int heartDelayTime
  35. {
  36. get { return mHeartDelayTime; }
  37. }
  38. private int mReconnetCount; //重连次数
  39. LuaFunction mLuaGameServerConnectedCb = null;
  40. LuaFunction receiveMessageFunc = null;
  41. public LuaFunction ReceiveMessageFunc
  42. {
  43. set { receiveMessageFunc = value; }
  44. get { return receiveMessageFunc; }
  45. }
  46. LuaFunction openReconnectionWndFunc = null;
  47. public LuaFunction OpenReconnectionWndFunc
  48. {
  49. set { openReconnectionWndFunc = value; }
  50. get { return openReconnectionWndFunc; }
  51. }
  52. LuaFunction luaNoitceFunc = null;
  53. public LuaFunction LuaNoitceFunc
  54. {
  55. set { luaNoitceFunc = value; }
  56. get { return luaNoitceFunc; }
  57. }
  58. //是否是登录状态
  59. private bool mIsLogin = false;
  60. public bool IsLogin
  61. {
  62. get { return mIsLogin; }
  63. set { mIsLogin = value; }
  64. }
  65. /// <summary>
  66. /// 网络模块初始化
  67. /// </summary>
  68. public override void Init()
  69. {
  70. isOnlineMode = true;
  71. EventMgr.AddEventListener<bool, bool>(ECoreEventType.EID_GameServer_ConnectSuccess, OnConnectRet);
  72. }
  73. void OnConnectRet(CoreEvent<bool, bool> ce)
  74. {
  75. bool ret = ce.Data;
  76. bool reconnect = ce.Data1;
  77. if(!ret)
  78. {
  79. mIsLogin = false;
  80. gameSrv.curCltPkgSeq = 1;
  81. }
  82. if (mLuaGameServerConnectedCb != null)
  83. mLuaGameServerConnectedCb.Call(this, ret, reconnect);
  84. }
  85. public void SetGameServerConnectedLuaFunc(LuaFunction func)
  86. {
  87. mLuaGameServerConnectedCb = func;
  88. }
  89. public bool ConnectServer(string host, int port)
  90. {
  91. string ip = GetIp(host);
  92. if(string.IsNullOrEmpty(ip))
  93. {
  94. DebugHelper.LogError(host + " 没有配置IP");
  95. return false;
  96. }
  97. //mLuaGameServerConnectedCb = luaFunc_;
  98. ConnectorParam param = new ConnectorParam(ip, port);
  99. return ConnectServer(param);
  100. }
  101. private string GetIp(string m_Host)
  102. {
  103. if (StringUtil.isIpString(m_Host))
  104. {
  105. return m_Host;
  106. }
  107. IPAddress[] ips = null;
  108. int retryTime = 0;
  109. while (ips == null && retryTime < 3)
  110. {
  111. try
  112. {
  113. retryTime++;
  114. IPHostEntry hostEntry = Dns.GetHostEntry(m_Host);
  115. ips = hostEntry.AddressList;
  116. }
  117. catch (System.Net.Sockets.SocketException)
  118. {
  119. DebugHelper.LogError("GetHostEntry Failed");
  120. }
  121. }
  122. if (ips != null && ips.Length > 0)
  123. return ips[0].ToString();
  124. return "";
  125. }
  126. /// <summary>
  127. /// 连接服务器
  128. /// </summary>
  129. /// <returns>是否成功</returns>
  130. [NoToLua]
  131. public bool ConnectServer(ConnectorParam para)
  132. {
  133. mReconnetCount = 0;
  134. isOnlineMode = true;
  135. return gameSrv.Init(para);
  136. }
  137. /// <summary>
  138. /// 重置大厅连接的发送队列
  139. /// </summary>
  140. public void ResetSending()
  141. {
  142. gameSrv.ResetSending(true);
  143. }
  144. public bool GetConnectStatus()
  145. {
  146. return gameSrv.Connected;
  147. }
  148. public void Disconnect()
  149. {
  150. if (gameSrv != null) {
  151. gameSrv.DisconnectConnector();
  152. gameSrv.DealConnectFail();
  153. }
  154. }
  155. public void CloseGameServerConnect()
  156. {
  157. gameSrv.CleanUp();
  158. gameSrv.Disconnect();
  159. mReconnetCount = 0;
  160. mIsLogin = false;
  161. }
  162. public void SetAesKey(string aesKey)
  163. {
  164. SecurityLayer.Instance.SetAesKey(aesKey);
  165. }
  166. public bool SendMsg(byte[] msgBody, UInt16 cmdId, bool isShowAlert = false,bool bConfirm = false)
  167. {
  168. NetPkg msg = NetPkg.CreateNetReqPkg(cmdId, msgBody);
  169. return SendMsg(ref msg, bConfirm, isShowAlert);
  170. }
  171. //使用RSA加密发送消息
  172. public bool SendMsgWithRSA(byte[] msgBody, UInt16 cmdId, bool isShowAlert = false, bool bConfirm = false)
  173. {
  174. byte[] encodedMsgBody = SecurityLayer.Instance.RSAEncrypt(msgBody);
  175. NetPkg msg = NetPkg.CreateNetReqPkg(cmdId, encodedMsgBody);
  176. msg.Head.EncryptFlag = c_RSA_Encrypt;
  177. return SendMsg(ref msg, bConfirm, isShowAlert);
  178. }
  179. //使用AES加密发送消息
  180. public bool SendMsgWithAES(byte[] msgBody, UInt16 cmdId, bool isShowAlert = false, bool bConfirm = false)
  181. {
  182. byte[] encodedMsgBody = SecurityLayer.Instance.AESEncrypt(msgBody);
  183. NetPkg msg = NetPkg.CreateNetReqPkg(cmdId, encodedMsgBody);
  184. msg.Head.EncryptFlag = c_AES_Encrypt;
  185. return SendMsg(ref msg, bConfirm, isShowAlert);
  186. }
  187. [NoToLua]
  188. public bool SendMsg(ref NetPkg msg, bool bConfirm,bool isShowAlert = false)
  189. {
  190. if (isOnlineMode)
  191. {
  192. msg.Confirm = bConfirm;
  193. gameSrv.PushSendMsg(msg);
  194. return true;
  195. }
  196. return false;
  197. }
  198. /// <summary>
  199. /// 高频率调用网络收发
  200. /// </summary>
  201. public void Update()
  202. {
  203. if (isOnlineMode && gameSrv!=null)
  204. {
  205. try
  206. {
  207. gameSrv.CustomUpdate();
  208. // 发送心跳包
  209. if (gameSrv.CanSendPing() && (Time.realtimeSinceStartup - mLastHeartTime >= 1.0f))
  210. {
  211. mLastHeartTime = Time.realtimeSinceStartup;
  212. if (mHeartAckWaitNum == 0)
  213. {
  214. mLastHeartTimeWait = Time.realtimeSinceStartup;
  215. }
  216. mHeartAckWaitNum++;
  217. //todo 处理心跳消息的发送
  218. SendPing();
  219. }
  220. gameSrv.HandleSending();
  221. HandleLobbyMsgRecv();
  222. }
  223. catch(Exception e)
  224. {
  225. DebugHelper.LogException(e);
  226. DebugHelper.LogError("NetworkMgr Update Error:" + e.Message);
  227. }
  228. if (mHeartAckWaitNum > 3)
  229. {
  230. mHeartAckWaitNum = 0;
  231. // 累计多次心跳未收到回包,说明链接到达不了服务器,断开链接, 并进入重连机制
  232. if (gameSrv != null) {
  233. gameSrv.DisconnectConnector();
  234. gameSrv.StartReconnect();
  235. }
  236. }
  237. }
  238. }
  239. private void SendPing()
  240. {
  241. NetPkg msg = NetPkg.CreateNetReqPkg(c_pingCmdId, null);
  242. gameSrv.ImmeSendPackage(msg);
  243. }
  244. private void HandleLobbyMsgRecv()
  245. {
  246. if (gameSrv != null)
  247. {
  248. List<NetPkg> msgList = gameSrv.RecvPackage();
  249. while (msgList != null && msgList.Count > 0)
  250. {
  251. for (int idx = 0; idx < msgList.Count; idx++)
  252. {
  253. NetPkg msg = msgList[idx];
  254. UInt16 msgId = msg.Head.CmdId;
  255. OnProtocolLua(msg.Body.MsgData, msgId);
  256. gameSrv.PostRecvPackage(msg);
  257. }
  258. msgList.Clear();
  259. msgList = gameSrv.RecvPackage();
  260. }
  261. }
  262. }
  263. public void OnProtocolLua(byte[] bodyInfo_, UInt16 msgId)
  264. {
  265. if (msgId == c_pingCmdAck)
  266. {
  267. mHeartAckWaitNum = 0;
  268. try
  269. {
  270. mHeartDelayTime = System.Convert.ToInt32((Time.realtimeSinceStartup - mLastHeartTimeWait) * 1000);
  271. }
  272. catch
  273. {
  274. mHeartDelayTime = 9999;
  275. }
  276. return;
  277. }
  278. if (ReceiveMessageFunc != null)
  279. {
  280. LuaByteBuffer luaByte = new LuaByteBuffer(bodyInfo_);
  281. ReceiveMessageFunc.Call(this, luaByte, msgId);
  282. }
  283. else
  284. {
  285. Debug.Log("LuaNetHandler InitIng : " + msgId);
  286. }
  287. }
  288. public void Resume()
  289. {
  290. if (isOnlineMode && gameSrv != null)
  291. {
  292. try
  293. {
  294. gameSrv.Resume();
  295. }
  296. catch (Exception e)
  297. {
  298. DebugHelper.LogError("NetworkMgr Update Error:" + e.Message);
  299. }
  300. }
  301. }
  302. public void StartReconnect()
  303. {
  304. if (gameSrv != null)
  305. {
  306. gameSrv.StartReconnect();
  307. }
  308. }
  309. public bool HasCmdIdAtConfirmQueue(UInt16 cmdId)
  310. {
  311. if (gameSrv != null)
  312. {
  313. return gameSrv.HasCmdIdAtConfirmQueue(cmdId);
  314. }
  315. return false;
  316. }
  317. }