LRMDirectConnectModule.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 (directConnectTransport is kcp2k.KcpTransport kcpTransport)
  57. kcpTransport.Port = (ushort)port;
  58. else
  59. {
  60. throw new Exception("DIRECT CONNECT MODULE ONLY SUPPORTS KCP AT THE MOMENT.");
  61. }
  62. }
  63. public int GetTransportPort()
  64. {
  65. if (directConnectTransport is kcp2k.KcpTransport kcpTransport)
  66. return kcpTransport.Port;
  67. else
  68. {
  69. throw new Exception("DIRECT CONNECT MODULE ONLY SUPPORTS KCP AT THE MOMENT.");
  70. }
  71. }
  72. public bool SupportsNATPunch()
  73. {
  74. return directConnectTransport is kcp2k.KcpTransport;
  75. }
  76. public bool KickClient(int clientID)
  77. {
  78. if (showDebugLogs)
  79. Debug.Log("Kicked direct connect client.");
  80. #if MIRROR_37_0_OR_NEWER
  81. directConnectTransport.ServerDisconnect(clientID);
  82. return true;
  83. #else
  84. return directConnectTransport.ServerDisconnect(clientID);
  85. #endif
  86. }
  87. public void ClientDisconnect()
  88. {
  89. directConnectTransport.ClientDisconnect();
  90. }
  91. public void ServerSend(int clientID, ArraySegment<byte> data, int channel)
  92. {
  93. #if MIRROR_40_0_OR_NEWER
  94. directConnectTransport.ServerSend(clientID, data, channel);
  95. #else
  96. directConnectTransport.ServerSend(clientID, channel, data);
  97. #endif
  98. }
  99. public void ClientSend(ArraySegment<byte> data, int channel)
  100. {
  101. #if MIRROR_40_0_OR_NEWER
  102. directConnectTransport.ClientSend(data, channel);
  103. #else
  104. directConnectTransport.ClientSend(channel, data);
  105. #endif
  106. }
  107. #region Transport Callbacks
  108. void OnServerConnected(int clientID)
  109. {
  110. if (showDebugLogs)
  111. Debug.Log("Direct Connect Client Connected");
  112. lightMirrorTransport.DirectAddClient(clientID);
  113. }
  114. void OnServerDataReceived(int clientID, ArraySegment<byte> data, int channel)
  115. {
  116. lightMirrorTransport.DirectReceiveData(data, channel, clientID);
  117. }
  118. void OnServerDisconnected(int clientID)
  119. {
  120. lightMirrorTransport.DirectRemoveClient(clientID);
  121. }
  122. void OnServerError(int client, Exception error)
  123. {
  124. if (showDebugLogs)
  125. Debug.Log("Direct Server Error: " + error);
  126. }
  127. void OnClientConnected()
  128. {
  129. if (showDebugLogs)
  130. Debug.Log("Direct Connect Client Joined");
  131. lightMirrorTransport.DirectClientConnected();
  132. }
  133. void OnClientDisconnected()
  134. {
  135. lightMirrorTransport.DirectDisconnected();
  136. }
  137. void OnClientDataReceived(ArraySegment<byte> data, int channel)
  138. {
  139. lightMirrorTransport.DirectReceiveData(data, channel);
  140. }
  141. void OnClientError(Exception error)
  142. {
  143. if (showDebugLogs)
  144. Debug.Log("Direct Client Error: " + error);
  145. }
  146. #endregion
  147. }