1
0

Transport.cs 8.3 KB

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