ServerMessageDispatcher.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using Unity.Netcode;
  5. using UnityEngine;
  6. namespace Rokid.MRC
  7. {
  8. public class ClientRequest : INetworkSerializable
  9. {
  10. public enum OpType
  11. {
  12. StartBuild,
  13. CancelBuild,
  14. ChangeCoord,
  15. ChangeScreenOrit,
  16. }
  17. public ulong ClientId;
  18. public ARCalibrationMode CalibrationMode;
  19. public OpType op;
  20. public SpacePose Pose;
  21. public bool isPortrait;
  22. public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
  23. {
  24. serializer.SerializeValue(ref ClientId);
  25. serializer.SerializeValue(ref op);
  26. serializer.SerializeValue(ref CalibrationMode);
  27. serializer.SerializeValue(ref Pose);
  28. serializer.SerializeValue(ref isPortrait);
  29. }
  30. }
  31. public class ServerRequest : INetworkSerializable
  32. {
  33. public enum OpType
  34. {
  35. ChangeCoord,
  36. StartStream,
  37. }
  38. public OpType op;
  39. public SpacePose Pose;
  40. public string msg;
  41. public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
  42. {
  43. serializer.SerializeValue(ref op);
  44. serializer.SerializeValue(ref Pose);
  45. serializer.SerializeValue(ref msg);
  46. }
  47. }
  48. public class ServerMessageDispatcher : Singleton<ServerMessageDispatcher>
  49. {
  50. public ScreenOrientation clientScreenOrientation;
  51. public override void Init()
  52. {
  53. base.Init();
  54. //NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler(nameof(StartBuild), StartBuild);
  55. if(NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
  56. {
  57. NetworkManager.Singleton.CustomMessagingManager.OnUnnamedMessage += OnReceivedMessage;
  58. }
  59. }
  60. //所有客户端的空间坐标系
  61. public List<SpacePose> ClientSpacePoseList = new List<SpacePose>();
  62. //所有服务端的空间坐标系
  63. public List<SpacePose> ServerSpacePoseList = new List<SpacePose>();
  64. //更新所有客户端的坐标系
  65. public void UpdateClientSpaceCoord(ulong clientId, SpacePose spacePose)
  66. {
  67. //服务端更新该SpacePose
  68. for(int i = 0;i < ClientSpacePoseList.Count;i++)
  69. {
  70. if(ClientSpacePoseList[i].ClientId == spacePose.ClientId)
  71. {
  72. ClientSpacePoseList[i] = spacePose;
  73. return;
  74. }
  75. }
  76. //服务端添加该SpacePose
  77. ClientSpacePoseList.Add(spacePose);
  78. }
  79. //更新所有服务端坐标系
  80. public void UpdateServerSpaceCoord(ulong clientId, SpacePose serverSpacePose)
  81. {
  82. bool isUpdate = false;
  83. for(int i = 0;i < ServerSpacePoseList.Count;i++)
  84. {
  85. if(ServerSpacePoseList[i].ClientId == serverSpacePose.ClientId)
  86. {
  87. ServerSpacePoseList[i] = serverSpacePose;
  88. isUpdate = true;
  89. }
  90. }
  91. if(!isUpdate)
  92. {
  93. ServerSpacePoseList.Add(serverSpacePose);
  94. }
  95. //发包给手机端
  96. SyncServerPoseToClient(clientId, serverSpacePose);
  97. }
  98. //眼镜端同步位姿到手机端
  99. private void SyncServerPoseToClient(ulong clientId, SpacePose pose)
  100. {
  101. FastBufferWriter writer = new FastBufferWriter(1100, Allocator.Temp);
  102. using(writer)
  103. {
  104. ServerRequest serverReq = new ServerRequest();
  105. serverReq.op = ServerRequest.OpType.ChangeCoord;
  106. serverReq.Pose = pose;
  107. serverReq.msg = MRCUtility.GetWirelessIP(AddressType.IPv4);
  108. //写入消息体
  109. writer.WriteValueSafe(serverReq);
  110. //NetworkManager.Singleton.CustomMessagingManager.SendUnnamedMessage(clientId, writer, NetworkDelivery.ReliableFragmentedSequenced);
  111. NetworkManager.Singleton.CustomMessagingManager.SendUnnamedMessage(clientId, writer);
  112. }
  113. }
  114. public void SyncStartStream(ulong clientId)
  115. {
  116. FastBufferWriter writer = new FastBufferWriter(1100, Allocator.Temp);
  117. using(writer)
  118. {
  119. ServerRequest serverReq = new ServerRequest();
  120. serverReq.op = ServerRequest.OpType.StartStream;
  121. serverReq.msg = MRCUtility.GetWirelessIP(AddressType.IPv4);
  122. //写入消息体
  123. writer.WriteValueSafe(serverReq);
  124. NetworkManager.Singleton.CustomMessagingManager.SendUnnamedMessage(clientId, writer);
  125. }
  126. }
  127. private void OnReceivedMessage(ulong clientId, FastBufferReader reader)
  128. {
  129. //服务端收到数据
  130. reader.ReadValueSafe<ClientRequest>(out ClientRequest clientReq);
  131. if(clientReq.op == ClientRequest.OpType.StartBuild)
  132. {
  133. if(clientReq.CalibrationMode == ARCalibrationMode.FaceTrack)
  134. {
  135. //TODO,暂时先用Confirm界面来提示
  136. UIManager.Instance.ShowConfirm(
  137. LocalizationMgr.Instance.GetTextByKey("BuildTitle"),
  138. string.Format(LocalizationMgr.Instance.GetTextByKey("BuildReq"), PlayerManager.Instance.GetPlayerData(clientReq.ClientId)?.PlayerName),
  139. ConfirmType.None);
  140. }
  141. else if(clientReq.CalibrationMode == ARCalibrationMode.SelfCalibration)
  142. {
  143. //自构建,创建手机模型
  144. ResourceManager.Instance.CreatePhoneAnchor();
  145. MRCDriver.Instance.SelfCalibrationPhone.gameObject.SetActive(true);
  146. }
  147. }
  148. else if(clientReq.op == ClientRequest.OpType.CancelBuild)
  149. {
  150. UIManager.Instance.ClosePanel(UIType.Confirm);
  151. }
  152. else if(clientReq.op == ClientRequest.OpType.ChangeCoord)
  153. {
  154. //更新客户端Pose列表
  155. UpdateClientSpaceCoord(clientId, clientReq.Pose);
  156. SpacePose serverSpacePose = default(SpacePose);
  157. if(clientReq.CalibrationMode == ARCalibrationMode.FaceTrack)
  158. {
  159. //更新服务端Pose列表
  160. serverSpacePose = new SpacePose(clientId, Camera.main.transform.position, Camera.main.transform.rotation, clientReq.CalibrationMode);
  161. }
  162. else if(clientReq.CalibrationMode == ARCalibrationMode.SelfCalibration)
  163. {
  164. serverSpacePose = new SpacePose(clientId, MRCDriver.Instance.SelfCalibrationPhone.position, MRCDriver.Instance.SelfCalibrationPhone.rotation, clientReq.CalibrationMode);
  165. MRCDriver.Instance.SelfCalibrationPhone.gameObject.SetActive(false);
  166. }
  167. //同步眼镜端位姿
  168. UpdateServerSpaceCoord(clientId, serverSpacePose);
  169. //通知构建提示界面关闭
  170. MessageCenter.SendMessage(GlobalDefine.SpaceBuildEnd, null);
  171. //CompositionManager.Instance.StartConnectStream();
  172. }
  173. else if(clientReq.op == ClientRequest.OpType.ChangeScreenOrit)
  174. {
  175. //横竖屏切换
  176. Debug.Log($"ChangeScreenOrit Is Portrait : {clientReq.isPortrait}");
  177. CameraController.Instance.ChangeFollowTargetOrientation(clientReq.isPortrait);
  178. }
  179. }
  180. }
  181. }