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: Unity.Netcode.Runtime.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
[Obsolete("DebugSimulator is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
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 |
m_Driver
Declaration
protected NetworkDriver m_Driver
Field Value
Type | Description |
---|---|
NetworkDriver |
m_NetworkManager
NetworkManager associated to this transport instance
Declaration
protected NetworkManager m_NetworkManager
Field Value
Type | Description |
---|---|
NetworkManager |
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
UseEncryption
Declaration
public bool UseEncryption { get; set; }
Property Value
Type | Description |
---|---|
bool |
UseWebSockets
Declaration
public bool UseWebSockets { get; set; }
Property Value
Type | Description |
---|---|
bool |
Methods
Connect(NetworkEndpoint)
Declaration
protected virtual NetworkConnection Connect(NetworkEndpoint serverEndpoint)
Parameters
Type | Name | Description |
---|---|---|
NetworkEndpoint | serverEndpoint |
Returns
Type | Description |
---|---|
NetworkConnection |
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
GetLocalEndpoint()
Gets the local sytem's Unity.Networking.Transport.NetworkEndpoint that is assigned for the current network session.
Declaration
public NetworkEndpoint GetLocalEndpoint()
Returns
Type | Description |
---|---|
NetworkEndpoint | Unity.Networking.Transport.NetworkEndpoint |
Remarks
If the driver is not created it will return an invalid Unity.Networking.Transport.NetworkEndpoint.
GetNetworkDriver()
Gets a reference to the Unity.Networking.Transport.NetworkDriver.
Declaration
public ref NetworkDriver GetNetworkDriver()
Returns
Type | Description |
---|---|
NetworkDriver | ref Unity.Networking.Transport.NetworkDriver |
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
OnCurrentTopology()
Declaration
protected override NetworkTopologyTypes OnCurrentTopology()
Returns
Type | Description |
---|---|
NetworkTopologyTypes |
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
[Obsolete("SetDebugSimulatorParameters is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
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 |