Transports
Last updated
Last updated
While DOTSNET handles all the higher logic like spawning, unspawning, observers, etc., the Transport handles only the low level packet sending.
DOTSNET can work with different low level transports, and comes with a few by default:
kcp2k: kcp translated to C#, line by line
kcp is just a reliability algorithm
Over a C# UDP socket
Supports reliable & unreliable channels
Telepathy: battle tested C# TCP
libuv2k: DEPRECATED
LiteNetLib: DEPRECATED
MemoryTransport:
uses memcpy to move messages between client & server
Can be used for benchmarking and for single player games if needed.
Does not work over the network.
DOTSNET uses kcp
by default.
If you want to try a different transport, select the NetworkClient/Server GameObject in your scene, remove the KcpTransportServer/ClientAuthoring component and add the ones from the transport of choice, for example:
Note: not all Transports will use a Port, which is why Port is always a transport specific property.
Note: NetworkClient/Server.Send() has a Channel parameter that will be forwarded to the Transport. Some transports (like TCP) ignore channels.
Inherit from TransportClientSystem
and TransportServerSystem
to implement your own transport.
Note that Send
and OnData
always send and receive exactly one message. So if a client sends 3 and then 2 bytes, the server should receive exactly 3 and then 2 bytes instead of 5 bytes.
Note that all TransportServer/ClientSystem functions are not thread safe and should only be called and handled in the main thread. If you want more threads, you will have to add them yourself in the background.
Event:
Description:
OnConnected
Call this after successfully connecting to the server.
Only call this from the main thread!
OnData<ArraySegment<byte>>
Call this after receiving a message from the server.
Only call this with exactly the message bytes. Not more, not less.
Pass an ArraySegment for allocation free message processing.
The client will handle the message immediately, so the ArraySegment’s array can be modified again immediately after returning.
Only call this from the main thread!
OnDisconnected
Call this after disconnecting from the server.
Only call this from the main thread!
Function:
Description:
IsConnected
Return true if the client is currently successfully connected to the server.
Connect(address)
Start connecting the client to the server. Should not be blocking, and only initiate the connect process in the background.
Send(ArraySegment<byte>)
Send bytes to the server. Only called with exactly the message bytes. Not more, not less.
The ArraySegment’s internal array is only valid until returning, so either send it directly, or copy it into an internal buffer.
Disconnect
Disconnect from the server.
Event:
Description:
OnConnected<connectionId>
Call this after a new client connected.
Only call this from the main thread!
OnData<connectionId, ArraySegment<byte>>
Call this after receiving a message from a client connection.
Only call this with exactly the message bytes. Not more, not less.
Pass an ArraySegment for allocation free message processing.
The server will handle the message immediately, so the ArraySegment’s array can be modified again immediately after returning.
Only call this from the main thread!
OnDisconnected<connectionId>
Call this after a client disconnected from the server.
Only call this from the main thread!
Function:
Description:
IsActive
Return true if the server is still listening to incoming connections.
Start
Start listening to incoming connections.
Send(connectionId, ArraySegment<byte>)
Send bytes to the client. Only called with exactly the message bytes. Not more, not less.
The ArraySegment’s internal array is only valid until returning, so either send it directly, or copy it into an internal buffer.
Disconnect(connectionId)
Disconnect the connection from the server.
GetAddress
Get a connection’s IP address. Can be useful for IP bans etc.
Stop
Stop listening to incoming connections.