NetWorkClient.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental.NetWork
  10. {
  11. using System;
  12. /// <summary> A net work client. </summary>
  13. public class NetWorkClient : IDisposable
  14. {
  15. /// <summary> Event queue for all listeners interested in OnJoinRoomResult events. </summary>
  16. public event Action<bool> OnJoinRoomResult;
  17. /// <summary> Event queue for all listeners interested in OnCameraParamUpdate events. </summary>
  18. public event Action<CameraParam> OnCameraParamUpdate;
  19. /// <summary> Event queue for all listeners interested in OnMessageResponse events. </summary>
  20. public event Action<byte[]> OnMessageResponse;
  21. /// <summary> Event queue for all listeners interested in OnDisconnect events. </summary>
  22. public event Action OnDisconnect;
  23. /// <summary> Event queue for all listeners interested in OnConnect events. </summary>
  24. public event Action OnConnect;
  25. /// <summary> Default constructor. </summary>
  26. public NetWorkClient()
  27. {
  28. NetworkSession.Register(MessageType.Connected, OnConnected);
  29. NetworkSession.Register(MessageType.Disconnect, OnDisConnected);
  30. NetworkSession.Register(MessageType.HeartBeat, HeartbeatResponse);
  31. NetworkSession.Register(MessageType.EnterRoom, EnterRoomResponse);
  32. NetworkSession.Register(MessageType.ExitRoom, ExitRoomResponse);
  33. NetworkSession.Register(MessageType.UpdateCameraParam, UpdateCameraParamResponse);
  34. NetworkSession.Register(MessageType.MessageSynchronization, MessageSynchronizationResponse);
  35. }
  36. /// <summary> Join the server's room. </summary>
  37. public void EnterRoomRequest()
  38. {
  39. NetworkSession.Enqueue(MessageType.EnterRoom);
  40. }
  41. /// <summary> Exit room request. </summary>
  42. public void ExitRoomRequest()
  43. {
  44. NetworkSession.Enqueue(MessageType.ExitRoom);
  45. }
  46. /// <summary> Updates the camera parameter request. </summary>
  47. public void UpdateCameraParamRequest()
  48. {
  49. NetworkSession.Enqueue(MessageType.UpdateCameraParam);
  50. }
  51. /// <summary> Join the server's room. </summary>
  52. public void SendMessage(byte[] data)
  53. {
  54. NetworkSession.Enqueue(MessageType.MessageSynchronization, data);
  55. }
  56. /// <summary> Connects. </summary>
  57. /// <param name="ip"> The IP.</param>
  58. /// <param name="port"> The port.</param>
  59. public void Connect(string ip, int port)
  60. {
  61. NetworkSession.Connect(ip, port);
  62. }
  63. /// <summary> Executes the 'connected' action. </summary>
  64. /// <param name="data"> The data.</param>
  65. private void OnConnected(byte[] data)
  66. {
  67. OnConnect?.Invoke();
  68. }
  69. /// <summary>
  70. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
  71. /// resources. </summary>
  72. public void Dispose()
  73. {
  74. NetworkSession.UnRegister(MessageType.Connected, OnConnected);
  75. NetworkSession.UnRegister(MessageType.Disconnect, OnDisConnected);
  76. NetworkSession.UnRegister(MessageType.HeartBeat, HeartbeatResponse);
  77. NetworkSession.UnRegister(MessageType.EnterRoom, EnterRoomResponse);
  78. NetworkSession.UnRegister(MessageType.ExitRoom, ExitRoomResponse);
  79. NetworkSession.UnRegister(MessageType.UpdateCameraParam, UpdateCameraParamResponse);
  80. NetworkSession.UnRegister(MessageType.MessageSynchronization, MessageSynchronizationResponse);
  81. NetworkSession.Close();
  82. }
  83. /// <summary> Executes the 'dis connected' action. </summary>
  84. /// <param name="data"> The data.</param>
  85. private void OnDisConnected(byte[] data)
  86. {
  87. OnDisconnect?.Invoke();
  88. }
  89. #region Net msg response
  90. /// <summary> Heartbeat response. </summary>
  91. /// <param name="data"> The data.</param>
  92. private void HeartbeatResponse(byte[] data)
  93. {
  94. NetworkSession.Received = true;
  95. NRDebugger.Debug("Receive a heart beat package.");
  96. }
  97. /// <summary> Enter room response. </summary>
  98. /// <param name="data"> The data.</param>
  99. private void EnterRoomResponse(byte[] data)
  100. {
  101. EnterRoomData result = SerializerFactory.Create().Deserialize<EnterRoomData>(data);
  102. if (result.result)
  103. {
  104. NRDebugger.Debug("Join the room success.");
  105. OnJoinRoomResult?.Invoke(true);
  106. }
  107. else
  108. {
  109. NRDebugger.Warning("Join the room faild.");
  110. OnJoinRoomResult?.Invoke(false);
  111. }
  112. }
  113. /// <summary> Exit room response. </summary>
  114. /// <param name="data"> The data.</param>
  115. private void ExitRoomResponse(byte[] data)
  116. {
  117. ExitRoomData result = SerializerFactory.Create().Deserialize<ExitRoomData>(data);
  118. if (result.Suc)
  119. {
  120. NRDebugger.Debug("Exit the room success.");
  121. }
  122. else
  123. {
  124. NRDebugger.Warning("Exit the room faild.");
  125. }
  126. }
  127. /// <summary> Updates the camera parameter response described by data. </summary>
  128. /// <param name="data"> The data.</param>
  129. private void UpdateCameraParamResponse(byte[] data)
  130. {
  131. CameraParam result = SerializerFactory.Create().Deserialize<CameraParam>(data);
  132. OnCameraParamUpdate?.Invoke(result);
  133. NRDebugger.Info(result.fov.ToString());
  134. }
  135. private void MessageSynchronizationResponse(byte[] data)
  136. {
  137. OnMessageResponse?.Invoke(data);
  138. }
  139. #endregion
  140. }
  141. }