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