NetWorkManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Engine.Net
  5. {
  6. /// <summary>网络控制器</summary>
  7. public class NetWorkManager
  8. {
  9. #region 单利
  10. private static NetWorkManager Instance;
  11. /// <summary>单利函数</summary>
  12. public static NetWorkManager GetInstance()
  13. {
  14. if (Instance == null)
  15. {
  16. Instance = new NetWorkManager();
  17. }
  18. return Instance;
  19. }
  20. #endregion
  21. /// <summary>接受消息委托</summary>
  22. public delegate bool HandleNetMessage(NetMsg iMsg);
  23. /// <summary>接受到消息事件</summary>
  24. private HandleNetMessage onHandleNetMessage = null;
  25. /// <summary>全都得游戏连接</summary>
  26. public List<NetSocket> mListSocket = new List<NetSocket>();
  27. /// <summary>连接个数</summary>
  28. private int nSocketCount = 0;
  29. public void OnDestroy()
  30. {
  31. DelSocket();
  32. }
  33. /// <summary>创建Socket连接</summary>
  34. public void CreateSocket(string strIp, int nPort, ESockeType socketType = ESockeType.Game)
  35. {
  36. if (!ServerEnum.IsConnectServer)
  37. {
  38. return;
  39. }
  40. if (GetNetSocket(socketType) != null)
  41. {
  42. DelSocket(socketType);
  43. /*
  44. NetSocket netSocket1 = GetNetSocket(socketType);
  45. netSocket1.Connect(strIp, nPort);
  46. CDebug.LogError("网络重连 socketType=" + socketType.ToString());
  47. return;
  48. CDebug.LogError("网络连接重复 socketType=" + socketType.ToString());
  49. return;
  50. */
  51. }
  52. NetSocket netSocket = new NetSocket(socketType, ESocketConnectType.TCP);
  53. netSocket.Connect(strIp, nPort);
  54. mListSocket.Add(netSocket);
  55. nSocketCount++;
  56. }
  57. public void ReConnectSocket(string strIp, int nPort, ESockeType socketType = ESockeType.Game)
  58. {
  59. if (!ServerEnum.IsConnectServer)
  60. {
  61. return;
  62. }
  63. if (GetNetSocket(socketType) != null)
  64. {
  65. NetSocket netSocket1 = GetNetSocket(socketType);
  66. netSocket1.Connect(strIp, nPort);
  67. CDebug.LogError("网络重连 socketType=" + socketType.ToString());
  68. }
  69. }
  70. /// <summary>删除Socket连接</summary>
  71. public void DelSocket(ESockeType socketType = ESockeType.Game, bool bReceiveDisconnMsg = true)
  72. {
  73. NetSocket socket = this.GetNetSocket(socketType);
  74. if(socket!=null)
  75. {
  76. socket.DoDisconnect(bReceiveDisconnMsg);
  77. mListSocket.Remove(socket);
  78. nSocketCount--;
  79. }
  80. }
  81. /// <summary>添加接受消息事件</summary>
  82. public void AddNetMessageEvent(HandleNetMessage handleNetMessage)
  83. {
  84. onHandleNetMessage = handleNetMessage;
  85. }
  86. /// <summary>删除接受消息事件</summary>
  87. public void DelNetMessageEvent(HandleNetMessage handleNetMessage)
  88. {
  89. onHandleNetMessage += handleNetMessage;
  90. }
  91. /// <summary>发送消息</summary>
  92. public bool SendMessage(NetMsg netMsg, ESockeType type = ESockeType.Game, int orginId = -1)
  93. {
  94. if(!ServerEnum.IsConnectServer)
  95. {
  96. return false;
  97. }
  98. netMsg.UserId = orginId;
  99. //获得消息对象
  100. NetSocket netSocket = this.GetNetSocket(type);
  101. //如果连接已经创建
  102. if (netSocket != null)
  103. {
  104. return netSocket.SendMsg(netMsg);
  105. }
  106. else
  107. {
  108. CDebug.LogError("网络连接不存在 socketType=" + type.ToString());
  109. return false;
  110. }
  111. }
  112. /// <summary>帧频函数</summary>
  113. public void OnFixedUpdate(int nTime)
  114. {
  115. //不存在消息处理逻辑,跳出,缓存全部的已经收到消息
  116. if (onHandleNetMessage == null || nSocketCount == 0)
  117. {
  118. return;
  119. }
  120. int msgCount = 0;
  121. do
  122. {
  123. //消息对象
  124. NetMsg msg = this.GetRecvMsg();
  125. //没有需要处理的消息了
  126. if(msg==null)
  127. {
  128. break;
  129. }
  130. msgCount++;
  131. //抛出消息
  132. onHandleNetMessage(msg);
  133. } while (true);
  134. //if(msgCount>3)
  135. //{
  136. //Debug.Log("msgCount=" + msgCount);
  137. //}
  138. }
  139. /// <summary>获得一个已经接受的消息</summary>
  140. private NetMsg GetRecvMsg()
  141. {
  142. NetMsg msg = null;
  143. for (int i = 0; i < nSocketCount; i++)
  144. {
  145. msg = mListSocket[i].RecvMsg();
  146. if (msg != null)
  147. {
  148. break;
  149. }
  150. }
  151. return msg;
  152. }
  153. /// <summary>获得指定类型的Socket对象</summary>
  154. public NetSocket GetNetSocket(ESockeType type)
  155. {
  156. for (int i = 0; i < nSocketCount; i++)
  157. {
  158. return mListSocket[i];
  159. /*if (mListSocket[i].socketType == type)
  160. {
  161. return mListSocket[i];
  162. }*/
  163. }
  164. return null;
  165. }
  166. }
  167. }