Class UnityTransport
The Netcode for GameObjects NetworkTransport for UnityTransport. Note: This is highly recommended to use over UNet.
Implements
Inherited Members
Namespace: Unity.Netcode.Transports.UTP
Assembly: solution.dll
Syntax
[AddComponentMenu("Netcode/Unity Transport")]
public class UnityTransport : NetworkTransport, INetworkStreamDriverConstructor
Fields
ConnectionData
The connection (address) data for this UnityTransport instance. This is where you can change IP Address, Port, or server's listen address. UnityTransport.ConnectionAddressData
Declaration
public UnityTransport.ConnectionAddressData ConnectionData
Field Value
Type | Description |
---|---|
UnityTransport.ConnectionAddressData |
DebugSimulator
Can be used to simulate poor network conditions such as:
- packet delay/latency
- packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
- packet drop rate (packet loss)
Declaration
public UnityTransport.SimulatorParameters DebugSimulator
Field Value
Type | Description |
---|---|
UnityTransport.SimulatorParameters |
InitialMaxPacketQueueSize
The default maximum (receive) packet queue size
Declaration
public const int InitialMaxPacketQueueSize = 128
Field Value
Type | Description |
---|---|
int |
InitialMaxPayloadSize
The default maximum payload size
Declaration
public const int InitialMaxPayloadSize = 6144
Field Value
Type | Description |
---|---|
int |
InitialMaxSendQueueSize
The default maximum send queue size
Declaration
[Obsolete("MaxSendQueueSize is now determined dynamically (can still be set programmatically using the MaxSendQueueSize property). This initial value is not used anymore.", false)]
public const int InitialMaxSendQueueSize = 98304
Field Value
Type | Description |
---|---|
int |
s_DriverConstructor
The global INetworkStreamDriverConstructor implementation
Declaration
public static INetworkStreamDriverConstructor s_DriverConstructor
Field Value
Type | Description |
---|---|
INetworkStreamDriverConstructor |
Properties
ConnectTimeoutMS
Timeout in milliseconds indicating how long we will wait until we send a new connection attempt.
Declaration
public int ConnectTimeoutMS { get; set; }
Property Value
Type | Description |
---|---|
int |
DisconnectTimeoutMS
Inactivity timeout after which a connection will be disconnected.
Declaration
public int DisconnectTimeoutMS { get; set; }
Property Value
Type | Description |
---|---|
int |
Remarks
The connection needs to receive data from the connected endpoint within this timeout. Note that with heartbeats enabled, simply not sending any data will not be enough to trigger this timeout (since heartbeats count as connection events).
DriverConstructor
Returns either the global INetworkStreamDriverConstructor implementation or the current UnityTransport instance
Declaration
public INetworkStreamDriverConstructor DriverConstructor { get; }
Property Value
Type | Description |
---|---|
INetworkStreamDriverConstructor |
HeartbeatTimeoutMS
Timeout in milliseconds after which a heartbeat is sent if there is no activity.
Declaration
public int HeartbeatTimeoutMS { get; set; }
Property Value
Type | Description |
---|---|
int |
MaxConnectAttempts
The maximum amount of connection attempts we will try before disconnecting.
Declaration
public int MaxConnectAttempts { get; set; }
Property Value
Type | Description |
---|---|
int |
MaxPacketQueueSize
The maximum amount of packets that can be in the internal send/receive queues.
Declaration
public int MaxPacketQueueSize { get; set; }
Property Value
Type | Description |
---|---|
int |
Remarks
Basically this is how many packets can be sent/received in a single update/frame.
MaxPayloadSize
The maximum size of an unreliable payload that can be handled by the transport.
Declaration
public int MaxPayloadSize { get; set; }
Property Value
Type | Description |
---|---|
int |
MaxSendQueueSize
The maximum size in bytes of the transport send queue.
Declaration
public int MaxSendQueueSize { get; set; }
Property Value
Type | Description |
---|---|
int |
Remarks
The send queue accumulates messages for batching and stores messages when other internal send queues are full. Note that there should not be any need to set this value manually since the send queue size is dynamically sized based on need.
This value should only be set if you have particular requirements (e.g. if you want to limit the memory usage of the send queues). Note however that setting this value too low can easily lead to disconnections under heavy traffic.
Protocol
The current ProtocolType used by the transport
Declaration
public UnityTransport.ProtocolType Protocol { get; }
Property Value
Type | Description |
---|---|
UnityTransport.ProtocolType |
ServerClientId
The client id used to represent the server.
Declaration
public override ulong ServerClientId { get; }
Property Value
Type | Description |
---|---|
ulong |
Overrides
Methods
CreateDriver(UnityTransport, out NetworkDriver, out NetworkPipeline, out NetworkPipeline, out NetworkPipeline)
Creates the internal NetworkDriver
Declaration
public void CreateDriver(UnityTransport transport, out NetworkDriver driver, out NetworkPipeline unreliableFragmentedPipeline, out NetworkPipeline unreliableSequencedFragmentedPipeline, out NetworkPipeline reliableSequencedPipeline)
Parameters
Type | Name | Description |
---|---|---|
UnityTransport | transport | The owner transport |
NetworkDriver | driver | The driver |
NetworkPipeline | unreliableFragmentedPipeline | The UnreliableFragmented NetworkPipeline |
NetworkPipeline | unreliableSequencedFragmentedPipeline | The UnreliableSequencedFragmented NetworkPipeline |
NetworkPipeline | reliableSequencedPipeline | The ReliableSequenced NetworkPipeline |
DisconnectLocalClient()
Disconnects the local client from the remote
Declaration
public override void DisconnectLocalClient()
Overrides
DisconnectRemoteClient(ulong)
Disconnects a remote client from the server
Declaration
public override void DisconnectRemoteClient(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The client to disconnect |
Overrides
GetCurrentRtt(ulong)
Gets the current RTT for a specific client
Declaration
public override ulong GetCurrentRtt(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The client RTT to get |
Returns
Type | Description |
---|---|
ulong | The RTT |
Overrides
Initialize(NetworkManager)
Initializes the transport
Declaration
public override void Initialize(NetworkManager networkManager = null)
Parameters
Type | Name | Description |
---|---|---|
NetworkManager | networkManager | The NetworkManager that initialized and owns the transport |
Overrides
PollEvent(out ulong, out ArraySegment<byte>, out float)
Polls for incoming events, with an extra output parameter to report the precise time the event was received.
Declaration
public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The clientId this event is for |
ArraySegment<byte> | payload | The incoming data payload |
float | receiveTime | The time the event was received, as reported by m_RealTimeProvider.RealTimeSinceStartup. |
Returns
Type | Description |
---|---|
NetworkEvent | Returns the event type |
Overrides
Send(ulong, ArraySegment<byte>, NetworkDelivery)
Send a payload to the specified clientId, data and networkDelivery.
Declaration
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The clientId to send to |
ArraySegment<byte> | payload | The data to send |
NetworkDelivery | networkDelivery | The delivery type (QoS) to send data with |
Overrides
SetClientRelayData(string, ushort, byte[], byte[], byte[], byte[], bool)
Set the relay server data for the host.
Declaration
public void SetClientRelayData(string ipAddress, ushort port, byte[] allocationId, byte[] key, byte[] connectionData, byte[] hostConnectionData, bool isSecure = false)
Parameters
Type | Name | Description |
---|---|---|
string | ipAddress | IP address or hostname of the relay server. |
ushort | port | UDP port of the relay server. |
byte[] | allocationId | Allocation ID as a byte array. |
byte[] | key | Allocation key as a byte array. |
byte[] | connectionData | Connection data as a byte array. |
byte[] | hostConnectionData | Host's connection data as a byte array. |
bool | isSecure | Whether the connection is secure (uses DTLS). |
SetClientSecrets(string, string)
Set the client parameters for encryption.
Declaration
public void SetClientSecrets(string serverCommonName, string caCertificate = null)
Parameters
Type | Name | Description |
---|---|---|
string | serverCommonName | Common name of the server (typically hostname). |
string | caCertificate | CA certificate used to validate the server's authenticity. |
Remarks
If the CA certificate is not provided, validation will be done against the OS/browser certificate store. This is what you'd want if using certificates from a known provider. For self-signed certificates, the CA certificate needs to be provided.
The CA certificate (if provided) is expected to be in the PEM format, including the
begin/end markers like -----BEGIN CERTIFICATE-----
.
SetConnectionData(string, ushort, string)
Sets IP and Port information. This will be ignored if using the Unity Relay and you should call SetRelayServerData(string, ushort, byte[], byte[], byte[], byte[], bool)
Declaration
public void SetConnectionData(string ipv4Address, ushort port, string listenAddress = null)
Parameters
Type | Name | Description |
---|---|---|
string | ipv4Address | The remote IP address (despite the name, can be an IPv6 address) |
ushort | port | The remote port |
string | listenAddress | The local listen address |
SetConnectionData(NetworkEndPoint, NetworkEndPoint)
Sets IP and Port information. This will be ignored if using the Unity Relay and you should call SetRelayServerData(string, ushort, byte[], byte[], byte[], byte[], bool)
Declaration
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default)
Parameters
Type | Name | Description |
---|---|---|
NetworkEndPoint | endPoint | The remote end point |
NetworkEndPoint | listenEndPoint | The local listen endpoint |
SetDebugSimulatorParameters(int, int, int)
Set the parameters for the debug simulator.
Declaration
public void SetDebugSimulatorParameters(int packetDelay, int packetJitter, int dropRate)
Parameters
Type | Name | Description |
---|---|---|
int | packetDelay | Packet delay in milliseconds. |
int | packetJitter | Packet jitter in milliseconds. |
int | dropRate | Packet drop percentage. |
SetHostRelayData(string, ushort, byte[], byte[], byte[], bool)
Set the relay server data for the host.
Declaration
public void SetHostRelayData(string ipAddress, ushort port, byte[] allocationId, byte[] key, byte[] connectionData, bool isSecure = false)
Parameters
Type | Name | Description |
---|---|---|
string | ipAddress | IP address or hostname of the relay server. |
ushort | port | UDP port of the relay server. |
byte[] | allocationId | Allocation ID as a byte array. |
byte[] | key | Allocation key as a byte array. |
byte[] | connectionData | Connection data as a byte array. |
bool | isSecure | Whether the connection is secure (uses DTLS). |
SetRelayServerData(string, ushort, byte[], byte[], byte[], byte[], bool)
Set the relay server data for the server.
Declaration
public void SetRelayServerData(string ipv4Address, ushort port, byte[] allocationIdBytes, byte[] keyBytes, byte[] connectionDataBytes, byte[] hostConnectionDataBytes = null, bool isSecure = false)
Parameters
Type | Name | Description |
---|---|---|
string | ipv4Address | IP address or hostname of the relay server. |
ushort | port | UDP port of the relay server. |
byte[] | allocationIdBytes | Allocation ID as a byte array. |
byte[] | keyBytes | Allocation key as a byte array. |
byte[] | connectionDataBytes | Connection data as a byte array. |
byte[] | hostConnectionDataBytes | The HostConnectionData as a byte array. |
bool | isSecure | Whether the connection is secure (uses DTLS). |
SetRelayServerData(RelayServerData)
Set the relay server data (using the lower-level Unity Transport data structure).
Declaration
public void SetRelayServerData(RelayServerData serverData)
Parameters
Type | Name | Description |
---|---|---|
RelayServerData | serverData | Data for the Relay server to use. |
SetServerSecrets(string, string)
Set the server parameters for encryption.
Declaration
public void SetServerSecrets(string serverCertificate, string serverPrivateKey)
Parameters
Type | Name | Description |
---|---|---|
string | serverCertificate | Public certificate for the server (PEM format). |
string | serverPrivateKey | Private key for the server (PEM format). |
Remarks
The public certificate and private key are expected to be in the PEM format, including
the begin/end markers like -----BEGIN CERTIFICATE-----
.
Shutdown()
Shuts down the transport
Declaration
public override void Shutdown()
Overrides
StartClient()
Connects client to the server Note: When this method returns false it could mean:
- You are trying to start a client that is already started
- It failed during the initial port binding when attempting to begin to connect
Declaration
public override bool StartClient()
Returns
Type | Description |
---|---|
bool | true if the client was started and false if it failed to start the client |
Overrides
StartServer()
Starts to listening for incoming clients Note: When this method returns false it could mean:
- You are trying to start a client that is already started
- It failed during the initial port binding when attempting to begin to connect
Declaration
public override bool StartServer()
Returns
Type | Description |
---|---|
bool | true if the server was started and false if it failed to start the server |