Class UnityTransport
The Netcode for GameObjects NetworkTransport for UnityTransport. Note: This is highly recommended to use over UNet.
Inherited Members
Namespace: Unity.Netcode.Transports.UTP
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 |
---|---|
Int32 |
InitialMaxPayloadSize
The default maximum payload size
Declaration
public const int InitialMaxPayloadSize = 6144
Field Value
Type | Description |
---|---|
Int32 |
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 |
---|---|
Int32 |
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 |
---|---|
Int32 |
DisconnectTimeoutMS
Inactivity timeout after which a connection will be disconnected.
Declaration
public int DisconnectTimeoutMS { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
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 |
---|---|
Int32 |
MaxConnectAttempts
The maximum amount of connection attempts we will try before disconnecting.
Declaration
public int MaxConnectAttempts { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
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 |
---|---|
Int32 |
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 |
---|---|
Int32 |
MaxSendQueueSize
The maximum size in bytes of the transport send queue.
Declaration
public int MaxSendQueueSize { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
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 |
---|---|
UInt64 |
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 |
Implements
DisconnectLocalClient()
Disconnects the local client from the remote
Declaration
public override void DisconnectLocalClient()
Overrides
DisconnectRemoteClient(UInt64)
Disconnects a remote client from the server
Declaration
public override void DisconnectRemoteClient(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | clientId | The client to disconnect |
Overrides
GetCurrentRtt(UInt64)
Gets the current RTT for a specific client
Declaration
public override ulong GetCurrentRtt(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | clientId | The client RTT to get |
Returns
Type | Description |
---|---|
UInt64 | 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 UInt64, out ArraySegment<Byte>, out Single)
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 |
---|---|---|
UInt64 | clientId | The clientId this event is for |
ArraySegment<Byte> | payload | The incoming data payload |
Single | receiveTime | The time the event was received, as reported by m_RealTimeProvider.RealTimeSinceStartup. |
Returns
Type | Description |
---|---|
NetworkEvent | Returns the event type |
Overrides
Send(UInt64, 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 |
---|---|---|
UInt64 | 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, UInt16, Byte[], Byte[], Byte[], Byte[], Boolean)
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. |
UInt16 | 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. |
Boolean | 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, UInt16, String)
Sets IP and Port information. This will be ignored if using the Unity Relay and you should call SetRelayServerData(String, UInt16, Byte[], Byte[], Byte[], Byte[], Boolean)
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) |
UInt16 | 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, UInt16, Byte[], Byte[], Byte[], Byte[], Boolean)
Declaration
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default(NetworkEndPoint))
Parameters
Type | Name | Description |
---|---|---|
NetworkEndPoint | endPoint | The remote end point |
NetworkEndPoint | listenEndPoint | The local listen endpoint |
SetDebugSimulatorParameters(Int32, Int32, Int32)
Set the parameters for the debug simulator.
Declaration
public void SetDebugSimulatorParameters(int packetDelay, int packetJitter, int dropRate)
Parameters
Type | Name | Description |
---|---|---|
Int32 | packetDelay | Packet delay in milliseconds. |
Int32 | packetJitter | Packet jitter in milliseconds. |
Int32 | dropRate | Packet drop percentage. |
SetHostRelayData(String, UInt16, Byte[], Byte[], Byte[], Boolean)
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. |
UInt16 | 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. |
Boolean | isSecure | Whether the connection is secure (uses DTLS). |
SetRelayServerData(String, UInt16, Byte[], Byte[], Byte[], Byte[], Boolean)
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. |
UInt16 | 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. |
Boolean | 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 |
---|---|
Boolean | 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 |
---|---|
Boolean | true if the server was started and false if it failed to start the server |