Transport.cs 8.5 KB

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