NetworkMgr.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using IFramework.Custom;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using Unity.Netcode;
  7. using Unity.Netcode.Transports.UTP;
  8. using UnityEngine;
  9. namespace Rokid.MRC
  10. {
  11. public enum HostType
  12. {
  13. Client,
  14. Host,
  15. }
  16. public enum CastType
  17. {
  18. BroadCast,
  19. MultiCast,
  20. }
  21. public class NetworkMgr : UnitySingleton<NetworkMgr>
  22. {
  23. public bool NeedBroadCast;
  24. public HostType hostType;
  25. public UDPServer udpServer;
  26. public CastType CastType;
  27. //连接管理
  28. private int MaxConnectedClients = 1;
  29. public int ReconnectAttempts = 2;
  30. public override void Awake()
  31. {
  32. base.Awake();
  33. }
  34. private void Start()
  35. {
  36. Init();
  37. }
  38. public void Init()
  39. {
  40. //Debug.Log(IPAddress.Broadcast.ToString());
  41. //Debug.Log(IPAddress.Any.ToString());
  42. //Debug.Log("OutSide IP : " + MRCUtility.GetOutSideIP());
  43. InitConnection();
  44. InitNetEventHandle();
  45. if(hostType == HostType.Host)
  46. {
  47. if(NeedBroadCast)
  48. {
  49. udpServer = new UDPServer();
  50. udpServer.Init(CastType == CastType.BroadCast);
  51. }
  52. else
  53. {
  54. gameObject.AddComponent<UDPClientProvider>();
  55. }
  56. ConnectionManager.Instance.ConnectionMethod.SetConnectionData("HostIP", "Host", false, false);
  57. ConnectionManager.Instance.StartHostIp("0.0.0.0", 7777);
  58. }
  59. }
  60. private void InitConnection()
  61. {
  62. ConnectionManager.Instance.SetNetworkManager(NetworkManager.Singleton);
  63. ConnectionManager.Instance.MaxConnectedPlayers = MaxConnectedClients;
  64. ConnectionManager.Instance.ReconnectAttempts = ReconnectAttempts;
  65. ConnectionManager.Instance.Init();
  66. }
  67. private void InitNetEventHandle()
  68. {
  69. MessageCenter.AddMsgListener(EventDefine.ServerConnectionEvent, ConnectionEventHandle);
  70. MessageCenter.AddMsgListener(EventDefine.ConnectStatusEvent, ConnectStatusEventHandle);
  71. MessageCenter.AddMsgListener(EventDefine.ClientDisConnectionEvent, DisConnectionEventHandle);
  72. MessageCenter.AddMsgListener(EventDefine.ClientReConnectionEvent, ReConnectionEventHandle);
  73. }
  74. //服务端
  75. private void ConnectionEventHandle(object val)
  76. {
  77. ConnectionEventMessage msg = (ConnectionEventMessage)val;
  78. //Debug.Log($"ConnectionEventHandle : ConnectStatus —— {msg.ConnectStatus} PlayerName —— {msg.PlayerName}");
  79. if(msg.ConnectStatus == ConnectStatus.GenericDisconnect)
  80. {
  81. //某个客户端断开
  82. UIManager.Instance.ShowTips(string.Format(LocalizationMgr.Instance.GetTextByKey("TipsConnected"), msg.PlayerName));
  83. //隐藏校准提示界面(todo,目前先用弹窗界面)
  84. UIManager.Instance.ClosePanel(UIType.Confirm);
  85. //隐藏合成相机
  86. ResourceManager.Instance.ShowRenderCamera(false);
  87. TransmissionClient.Release();
  88. }
  89. }
  90. //通用
  91. private void ConnectStatusEventHandle(object val)
  92. {
  93. ConnectStatus status = (ConnectStatus)val;
  94. //Debug.Log($"ConnectStatusEventHandle : ConnectStatus —— {status}");
  95. //Host启动失败
  96. if(status == ConnectStatus.StartHostFailed)
  97. {
  98. UIManager.Instance.ShowTips(LocalizationMgr.Instance.GetTextByKey("TipsConnected"));
  99. }
  100. //Host启动成功
  101. if(status == ConnectStatus.Success)
  102. {
  103. }
  104. }
  105. //客户端
  106. private void DisConnectionEventHandle(object val)
  107. {
  108. ConnectStatus reason = (ConnectStatus)val;
  109. Debug.Log($"DisConnectionEventHandle : Reason —— {reason}");
  110. }
  111. //客户端
  112. private void ReConnectionEventHandle(object val)
  113. {
  114. ReconnectMessage msg = (ReconnectMessage)val;
  115. Debug.Log($"ReConnectionEventHandle : CurrentAttempt —— {msg.CurrentAttempt} MaxAttempt —— {msg.MaxAttempt}");
  116. }
  117. //作为客户端启动
  118. public void StartNetCodeClient(string ip)
  119. {
  120. ConnectionManager.Instance.ChangeState(ConnectionManager.Instance.m_Offline);
  121. ConnectionManager.Instance.StartClientIp(ip, 7777);
  122. }
  123. public void OnDestroy()
  124. {
  125. udpServer?.OnDestroy();
  126. }
  127. private void OnApplicationQuit()
  128. {
  129. //Debug.LogError("NetworkMgr OnApplicationQuit");
  130. NetworkManager.Singleton?.Shutdown();
  131. }
  132. }
  133. }