LRMDirectConnectModule.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // This is an optional module for adding direct connect support
  2. using Mirror;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using LightReflectiveMirror;
  7. [RequireComponent(typeof(LightReflectiveMirrorTransport))]
  8. public class LRMDirectConnectModule : MonoBehaviour
  9. {
  10. [HideInInspector]
  11. public Transport directConnectTransport;
  12. public bool showDebugLogs;
  13. private LightReflectiveMirrorTransport lightMirrorTransport;
  14. void Awake()
  15. {
  16. lightMirrorTransport = GetComponent<LightReflectiveMirrorTransport>();
  17. if (directConnectTransport == null)
  18. {
  19. Debug.Log("Direct Connect Transport is null!");
  20. return;
  21. }
  22. if (directConnectTransport is LightReflectiveMirrorTransport)
  23. {
  24. Debug.Log("Direct Connect Transport Cannot be the relay, silly. :P");
  25. return;
  26. }
  27. directConnectTransport.OnServerConnected = (OnServerConnected);
  28. directConnectTransport.OnServerDataReceived = (OnServerDataReceived);
  29. directConnectTransport.OnServerDisconnected = (OnServerDisconnected);
  30. directConnectTransport.OnServerError = (OnServerError);
  31. directConnectTransport.OnClientConnected = (OnClientConnected);
  32. directConnectTransport.OnClientDataReceived = (OnClientDataReceived);
  33. directConnectTransport.OnClientDisconnected = (OnClientDisconnected);
  34. directConnectTransport.OnClientError = (OnClientError);
  35. }
  36. public void StartServer(int port)
  37. {
  38. if(port > 0)
  39. SetTransportPort(port);
  40. directConnectTransport.ServerStart();
  41. if (showDebugLogs)
  42. Debug.Log("Direct Connect Server Created!");
  43. }
  44. public void StopServer()
  45. {
  46. directConnectTransport.ServerStop();
  47. }
  48. public void JoinServer(string ip, int port)
  49. {
  50. if (SupportsNATPunch())
  51. SetTransportPort(port);
  52. directConnectTransport.ClientConnect(ip);
  53. }
  54. public void SetTransportPort(int port)
  55. {
  56. #if !IGNORANCE
  57. if (directConnectTransport is kcp2k.KcpTransport kcpTransport)
  58. kcpTransport.Port = (ushort)port;
  59. else
  60. {
  61. ThrowIfNotSupported();
  62. }
  63. #else
  64. if (directConnectTransport is kcp2k.KcpTransport kcpTransport)
  65. kcpTransport.Port = (ushort)port;
  66. else if (directConnectTransport is IgnoranceTransport.Ignorance ignorance)
  67. ignorance.port = (ushort)port;
  68. else
  69. {
  70. ThrowIfNotSupported();
  71. }
  72. #endif
  73. }
  74. public int GetTransportPort()
  75. {
  76. #if !IGNORANCE
  77. if (directConnectTransport is kcp2k.KcpTransport kcpTransport)
  78. return kcpTransport.Port;
  79. else
  80. {
  81. ThrowIfNotSupported();
  82. return -1;
  83. }
  84. #else
  85. if (directConnectTransport is kcp2k.KcpTransport kcpTransport)
  86. return kcpTransport.Port;
  87. else if (directConnectTransport is IgnoranceTransport.Ignorance ignorance)
  88. return ignorance.port;
  89. else
  90. {
  91. ThrowIfNotSupported();
  92. return -1;
  93. }
  94. #endif
  95. }
  96. private static int ThrowIfNotSupported()
  97. {
  98. #if !IGNORANCE
  99. throw new Exception("DIRECT CONNECT MODULE ONLY SUPPORTS KCP AT THE MOMENT.");
  100. #else
  101. throw new Exception("DIRECT CONNECT MODULE ONLY SUPPORTS KCP AND IGNORANCE");
  102. #endif
  103. }
  104. public bool SupportsNATPunch()
  105. {
  106. #if !IGNORANCE
  107. return directConnectTransport is kcp2k.KcpTransport;
  108. #else
  109. return directConnectTransport is kcp2k.KcpTransport || directConnectTransport is IgnoranceTransport.Ignorance;
  110. #endif
  111. }
  112. public void KickClient(int clientID)
  113. {
  114. if (showDebugLogs)
  115. Debug.Log("Kicked direct connect client.");
  116. directConnectTransport.ServerDisconnect(clientID);
  117. }
  118. public void ClientDisconnect()
  119. {
  120. directConnectTransport.ClientDisconnect();
  121. }
  122. public void ServerSend(int clientID, ArraySegment<byte> data, int channel)
  123. {
  124. directConnectTransport.ServerSend(clientID, data, channel);
  125. }
  126. public void ClientSend(ArraySegment<byte> data, int channel)
  127. {
  128. directConnectTransport.ClientSend(data, channel);
  129. }
  130. #region Transport Callbacks
  131. void OnServerConnected(int clientID)
  132. {
  133. if (showDebugLogs)
  134. Debug.Log("Direct Connect Client Connected");
  135. lightMirrorTransport.DirectAddClient(clientID);
  136. }
  137. void OnServerDataReceived(int clientID, ArraySegment<byte> data, int channel)
  138. {
  139. lightMirrorTransport.DirectReceiveData(data, channel, clientID);
  140. }
  141. void OnServerDisconnected(int clientID)
  142. {
  143. lightMirrorTransport.DirectRemoveClient(clientID);
  144. }
  145. void OnServerError(int client, Exception error)
  146. {
  147. if (showDebugLogs)
  148. Debug.Log("Direct Server Error: " + error);
  149. }
  150. void OnClientConnected()
  151. {
  152. if (showDebugLogs)
  153. Debug.Log("Direct Connect Client Joined");
  154. lightMirrorTransport.DirectClientConnected();
  155. }
  156. void OnClientDisconnected()
  157. {
  158. lightMirrorTransport.DirectDisconnected();
  159. }
  160. void OnClientDataReceived(ArraySegment<byte> data, int channel)
  161. {
  162. lightMirrorTransport.DirectReceiveData(data, channel);
  163. }
  164. void OnClientError(Exception error)
  165. {
  166. if (showDebugLogs)
  167. Debug.Log("Direct Client Error: " + error);
  168. }
  169. #endregion
  170. }