Transport.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // For future reference, here is what Transports need to do in Mirror:
  2. //
  3. // Connecting:
  4. // * Transports are responsible to call either OnConnected || OnDisconnected
  5. // in a certain time after a Connect was called. It can not end in limbo.
  6. //
  7. // Disconnecting:
  8. // * Connections might disconnect voluntarily by the other end.
  9. // * Connections might be disconnect involuntarily by the server.
  10. // * Either way, Transports need to detect it and call OnDisconnected.
  11. //
  12. // Timeouts:
  13. // * Transports should expose a configurable timeout
  14. // * Transports are responsible for calling OnDisconnected after a timeout
  15. //
  16. // Channels:
  17. // * Default channel is Reliable, as in reliable ordered (OR DISCONNECT)
  18. // * Where possible, Unreliable should be supported (unordered, no guarantee)
  19. //
  20. // Other:
  21. // * Transports functions are all bound to the main thread.
  22. // (Transports can use other threads in the background if they manage them)
  23. // * Transports should only process messages while the component is enabled.
  24. //
  25. using System;
  26. using UnityEngine;
  27. namespace Mirror
  28. {
  29. /// <summary>Abstract transport layer component</summary>
  30. public abstract class Transport : MonoBehaviour
  31. {
  32. // common //////////////////////////////////////////////////////////////
  33. /// <summary>The current transport used by Mirror.</summary>
  34. [Obsolete(".activeTransport was renamed to .active")] // 2021-09-25
  35. public static Transport activeTransport;
  36. /// <summary>The current transport used by Mirror.</summary>
  37. public static Transport active;
  38. /// <summary>Is this transport available in the current platform?</summary>
  39. public abstract bool Available();
  40. // client //////////////////////////////////////////////////////////////
  41. /// <summary>Called by Transport when the client connected to the server.</summary>
  42. public Action OnClientConnected;
  43. /// <summary>Called by Transport when the client received a message from the server.</summary>
  44. public Action<ArraySegment<byte>, int> OnClientDataReceived;
  45. /// <summary>Called by Transport when the client sent a message to the server.</summary>
  46. // Transports are responsible for calling it because:
  47. // - groups it together with OnReceived responsibility
  48. // - allows transports to decide if anything was sent or not
  49. // - allows transports to decide the actual used channel (i.e. tcp always sending reliable)
  50. public Action<ArraySegment<byte>, int> OnClientDataSent;
  51. /// <summary>Called by Transport when the client encountered an error.</summary>
  52. public Action<TransportError, string> OnClientError;
  53. /// <summary>Called by Transport when the client disconnected from the server.</summary>
  54. public Action OnClientDisconnected;
  55. // server //////////////////////////////////////////////////////////////
  56. /// <summary>Called by Transport when a new client connected to the server.</summary>
  57. public Action<int> OnServerConnected;
  58. /// <summary>Called by Transport when the server received a message from a client.</summary>
  59. public Action<int, ArraySegment<byte>, int> OnServerDataReceived;
  60. /// <summary>Called by Transport when the server sent a message to a client.</summary>
  61. // Transports are responsible for calling it because:
  62. // - groups it together with OnReceived responsibility
  63. // - allows transports to decide if anything was sent or not
  64. // - allows transports to decide the actual used channel (i.e. tcp always sending reliable)
  65. public Action<int, ArraySegment<byte>, int> OnServerDataSent;
  66. /// <summary>Called by Transport when a server's connection encountered a problem.</summary>
  67. /// If a Disconnect will also be raised, raise the Error first.
  68. public Action<int, TransportError, string> OnServerError;
  69. /// <summary>Called by Transport when a client disconnected from the server.</summary>
  70. public Action<int> OnServerDisconnected;
  71. // client functions ////////////////////////////////////////////////////
  72. /// <summary>True if the client is currently connected to the server.</summary>
  73. public abstract bool ClientConnected();
  74. /// <summary>Connects the client to the server at the address.</summary>
  75. public abstract void ClientConnect(string address);
  76. /// <summary>Connects the client to the server at the Uri.</summary>
  77. public virtual void ClientConnect(Uri uri)
  78. {
  79. // By default, to keep backwards compatibility, just connect to the host
  80. // in the uri
  81. ClientConnect(uri.Host);
  82. }
  83. /// <summary>Sends a message to the server over the given channel.</summary>
  84. // The ArraySegment is only valid until returning. Copy if needed.
  85. public abstract void ClientSend(ArraySegment<byte> segment, int channelId = Channels.Reliable);
  86. /// <summary>Disconnects the client from the server</summary>
  87. public abstract void ClientDisconnect();
  88. // server functions ////////////////////////////////////////////////////
  89. /// <summary>Returns server address as Uri.</summary>
  90. // Useful for NetworkDiscovery.
  91. public abstract Uri ServerUri();
  92. /// <summary>True if the server is currently listening for connections.</summary>
  93. public abstract bool ServerActive();
  94. /// <summary>Start listening for connections.</summary>
  95. public abstract void ServerStart();
  96. /// <summary>Send a message to a client over the given channel.</summary>
  97. public abstract void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId = Channels.Reliable);
  98. /// <summary>Disconnect a client from the server.</summary>
  99. public abstract void ServerDisconnect(int connectionId);
  100. /// <summary>Get a client's address on the server.</summary>
  101. // Can be useful for Game Master IP bans etc.
  102. public abstract string ServerGetClientAddress(int connectionId);
  103. /// <summary>Stop listening and disconnect all connections.</summary>
  104. public abstract void ServerStop();
  105. /// <summary>Maximum message size for the given channel.</summary>
  106. // Different channels often have different sizes, ranging from MTU to
  107. // several megabytes.
  108. //
  109. // Needs to return a value at all times, even if the Transport isn't
  110. // running or available because it's needed for initializations.
  111. public abstract int GetMaxPacketSize(int channelId = Channels.Reliable);
  112. /// <summary>Recommended Batching threshold for this transport.</summary>
  113. // Uses GetMaxPacketSize by default.
  114. // Some transports like kcp support large max packet sizes which should
  115. // not be used for batching all the time because they end up being too
  116. // slow (head of line blocking etc.).
  117. public virtual int GetBatchThreshold(int channelId = Channels.Reliable)
  118. {
  119. return GetMaxPacketSize(channelId);
  120. }
  121. // block Update & LateUpdate to show warnings if Transports still use
  122. // them instead of using
  123. // Client/ServerEarlyUpdate: to process incoming messages
  124. // Client/ServerLateUpdate: to process outgoing messages
  125. // those are called by NetworkClient/Server at the right time.
  126. //
  127. // allows transports to implement the proper network update order of:
  128. // process_incoming()
  129. // update_world()
  130. // process_outgoing()
  131. //
  132. // => see NetworkLoop.cs for detailed explanations!
  133. #pragma warning disable UNT0001 // Empty Unity message
  134. public void Update() {}
  135. public void LateUpdate() {}
  136. #pragma warning restore UNT0001 // Empty Unity message
  137. /// <summary>
  138. /// NetworkLoop NetworkEarly/LateUpdate were added for a proper network
  139. /// update order. the goal is to:
  140. /// process_incoming()
  141. /// update_world()
  142. /// process_outgoing()
  143. /// in order to avoid unnecessary latency and data races.
  144. /// </summary>
  145. // => split into client and server parts so that we can cleanly call
  146. // them from NetworkClient/Server
  147. // => VIRTUAL for now so we can take our time to convert transports
  148. // without breaking anything.
  149. public virtual void ClientEarlyUpdate() {}
  150. public virtual void ServerEarlyUpdate() {}
  151. public virtual void ClientLateUpdate() {}
  152. public virtual void ServerLateUpdate() {}
  153. /// <summary>Shut down the transport, both as client and server</summary>
  154. public abstract void Shutdown();
  155. /// <summary>Called by Unity when quitting. Inheriting Transports should call base for proper Shutdown.</summary>
  156. public virtual void OnApplicationQuit()
  157. {
  158. // stop transport (e.g. to shut down threads)
  159. // (when pressing Stop in the Editor, Unity keeps threads alive
  160. // until we press Start again. so if Transports use threads, we
  161. // really want them to end now and not after next start)
  162. Shutdown();
  163. }
  164. }
  165. }