Transport.cs 8.8 KB

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