FallbackTransport.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // uses the first available transport for server and client.
  2. // example: to use Apathy if on Windows/Mac/Linux and fall back to Telepathy
  3. // otherwise.
  4. using System;
  5. using UnityEngine;
  6. namespace Mirror
  7. {
  8. // Deprecated 2021-05-13
  9. [HelpURL("https://mirror-networking.gitbook.io/docs/transports/fallback-transport")]
  10. [DisallowMultipleComponent]
  11. [Obsolete("Fallback Transport will be retired. It was only needed for Apathy/Libuv. Use kcp or Telepathy instead, those run everywhere.")]
  12. public class FallbackTransport : Transport
  13. {
  14. public Transport[] transports;
  15. // the first transport that is available on this platform
  16. Transport available;
  17. public void Awake()
  18. {
  19. if (transports == null || transports.Length == 0)
  20. {
  21. throw new Exception("FallbackTransport requires at least 1 underlying transport");
  22. }
  23. available = GetAvailableTransport();
  24. Debug.Log("FallbackTransport available: " + available.GetType());
  25. }
  26. void OnEnable()
  27. {
  28. available.enabled = true;
  29. }
  30. void OnDisable()
  31. {
  32. available.enabled = false;
  33. }
  34. // The client just uses the first transport available
  35. Transport GetAvailableTransport()
  36. {
  37. foreach (Transport transport in transports)
  38. {
  39. if (transport.Available())
  40. {
  41. return transport;
  42. }
  43. }
  44. throw new Exception("No transport suitable for this platform");
  45. }
  46. public override bool Available()
  47. {
  48. return available.Available();
  49. }
  50. public override void ClientConnect(string address)
  51. {
  52. available.OnClientConnected = OnClientConnected;
  53. available.OnClientDataReceived = OnClientDataReceived;
  54. available.OnClientError = OnClientError;
  55. available.OnClientDisconnected = OnClientDisconnected;
  56. available.ClientConnect(address);
  57. }
  58. public override void ClientConnect(Uri uri)
  59. {
  60. foreach (Transport transport in transports)
  61. {
  62. if (transport.Available())
  63. {
  64. try
  65. {
  66. transport.ClientConnect(uri);
  67. available = transport;
  68. }
  69. catch (ArgumentException)
  70. {
  71. // transport does not support the schema, just move on to the next one
  72. }
  73. }
  74. }
  75. throw new Exception("No transport suitable for this platform");
  76. }
  77. public override bool ClientConnected()
  78. {
  79. return available.ClientConnected();
  80. }
  81. public override void ClientDisconnect()
  82. {
  83. available.ClientDisconnect();
  84. }
  85. public override void ClientSend(ArraySegment<byte> segment, int channelId)
  86. {
  87. available.ClientSend(segment, channelId);
  88. }
  89. // right now this just returns the first available uri,
  90. // should we return the list of all available uri?
  91. public override Uri ServerUri() => available.ServerUri();
  92. public override bool ServerActive()
  93. {
  94. return available.ServerActive();
  95. }
  96. public override string ServerGetClientAddress(int connectionId)
  97. {
  98. return available.ServerGetClientAddress(connectionId);
  99. }
  100. public override void ServerDisconnect(int connectionId)
  101. {
  102. available.ServerDisconnect(connectionId);
  103. }
  104. public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
  105. {
  106. available.ServerSend(connectionId, segment, channelId);
  107. }
  108. public override void ServerStart()
  109. {
  110. available.OnServerConnected = OnServerConnected;
  111. available.OnServerDataReceived = OnServerDataReceived;
  112. available.OnServerError = OnServerError;
  113. available.OnServerDisconnected = OnServerDisconnected;
  114. available.ServerStart();
  115. }
  116. public override void ServerStop()
  117. {
  118. available.ServerStop();
  119. }
  120. public override void ClientEarlyUpdate() => available.ClientEarlyUpdate();
  121. public override void ServerEarlyUpdate() => available.ServerEarlyUpdate();
  122. public override void ClientLateUpdate() => available.ClientLateUpdate();
  123. public override void ServerLateUpdate() => available.ServerLateUpdate();
  124. public override void Shutdown()
  125. {
  126. available.Shutdown();
  127. }
  128. public override int GetMaxPacketSize(int channelId = 0)
  129. {
  130. // finding the max packet size in a fallback environment has to be
  131. // done very carefully:
  132. // * servers and clients might run different transports depending on
  133. // which platform they are on.
  134. // * there should only ever be ONE true max packet size for everyone,
  135. // otherwise a spawn message might be sent to all tcp sockets, but
  136. // be too big for some udp sockets. that would be a debugging
  137. // nightmare and allow for possible exploits and players on
  138. // different platforms seeing a different game state.
  139. // => the safest solution is to use the smallest max size for all
  140. // transports. that will never fail.
  141. int mininumAllowedSize = int.MaxValue;
  142. foreach (Transport transport in transports)
  143. {
  144. int size = transport.GetMaxPacketSize(channelId);
  145. mininumAllowedSize = Mathf.Min(size, mininumAllowedSize);
  146. }
  147. return mininumAllowedSize;
  148. }
  149. public override string ToString()
  150. {
  151. return available.ToString();
  152. }
  153. }
  154. }