Skip to main content

Class UnityTransport

The Netcode for GameObjects NetworkTransport for UnityTransport. Note: This is highly recommended to use over UNet.

Inheritance

System.Object

NetworkTransport

UnityTransport

Implements

INetworkStreamDriverConstructor

Inherited Members

NetworkTransport.IsSupported

NetworkTransport.OnTransportEvent

NetworkTransport.InvokeOnTransportEvent(NetworkEvent, UInt64, ArraySegment\<Byte>, Single)

Namespace: Unity.Netcode.Transports.UTP
Assembly: MLAPI.dll
Syntax
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
TypeDescription
UnityTransport.ConnectionAddressData

DebugSimulator

Can be used to simulate poor network conditions such as:

Declaration
public UnityTransport.SimulatorParameters DebugSimulator
Field Value
TypeDescription
UnityTransport.SimulatorParameters

InitialMaxPacketQueueSize

The default maximum (receive) packet queue size

Declaration
public const int InitialMaxPacketQueueSize = 128
Field Value
TypeDescription
System.Int32

InitialMaxPayloadSize

The default maximum payload size

Declaration
public const int InitialMaxPayloadSize = 6144
Field Value
TypeDescription
System.Int32

InitialMaxSendQueueSize

The default maximum send queue size

Declaration
public const int InitialMaxSendQueueSize = 98304
Field Value
TypeDescription
System.Int32

s_DriverConstructor

The global INetworkStreamDriverConstructor implementation

Declaration
public static INetworkStreamDriverConstructor s_DriverConstructor
Field Value
TypeDescription
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
TypeDescription
System.Int32

DisconnectTimeoutMS

Inactivity timeout after which a connection will be disconnected.

Declaration
public int DisconnectTimeoutMS { get; set; }
Property Value
TypeDescription
System.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
TypeDescription
INetworkStreamDriverConstructor

HeartbeatTimeoutMS

Timeout in milliseconds after which a heartbeat is sent if there is no activity.

Declaration
public int HeartbeatTimeoutMS { get; set; }
Property Value
TypeDescription
System.Int32

MaxConnectAttempts

The maximum amount of connection attempts we will try before disconnecting.

Declaration
public int MaxConnectAttempts { get; set; }
Property Value
TypeDescription
System.Int32

MaxPacketQueueSize

The maximum amount of packets that can be in the internal send/receive queues.

Declaration
public int MaxPacketQueueSize { get; set; }
Property Value
TypeDescription
System.Int32
Remarks

Basically this is how many packets can be sent/received in a single update/frame.

MaxPayloadSize

The maximum size of a payload that can be handled by the transport.

Declaration
public int MaxPayloadSize { get; set; }
Property Value
TypeDescription
System.Int32

MaxSendQueueSize

The maximum size in bytes of the transport send queue.

Declaration
public int MaxSendQueueSize { get; set; }
Property Value
TypeDescription
System.Int32
Remarks

The send queue accumulates messages for batching and stores messages when other internal send queues are full. If you routinely observe an error about too many in-flight packets, try increasing this.

Protocol

The current ProtocolType used by the transport

Declaration
public UnityTransport.ProtocolType Protocol { get; }
Property Value
TypeDescription
UnityTransport.ProtocolType

ServerClientId

The client id used to represent the server.

Declaration
public override ulong ServerClientId { get; }
Property Value
TypeDescription
System.UInt64
Overrides

NetworkTransport.ServerClientId

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
TypeNameDescription
UnityTransporttransportThe owner transport
NetworkDriverdriverThe driver
NetworkPipelineunreliableFragmentedPipelineThe UnreliableFragmented NetworkPipeline
NetworkPipelineunreliableSequencedFragmentedPipelineThe UnreliableSequencedFragmented NetworkPipeline
NetworkPipelinereliableSequencedPipelineThe ReliableSequenced NetworkPipeline

DisconnectLocalClient()

Disconnects the local client from the remote

Declaration
public override void DisconnectLocalClient()
Overrides

NetworkTransport.DisconnectLocalClient()

DisconnectRemoteClient(UInt64)

Disconnects a remote client from the server

Declaration
public override void DisconnectRemoteClient(ulong clientId)
Parameters
TypeNameDescription
System.UInt64clientIdThe client to disconnect
Overrides

NetworkTransport.DisconnectRemoteClient(UInt64)

GetCurrentRtt(UInt64)

Gets the current RTT for a specific client

Declaration
public override ulong GetCurrentRtt(ulong clientId)
Parameters
TypeNameDescription
System.UInt64clientIdThe client RTT to get
Returns
TypeDescription
System.UInt64The RTT
Overrides

NetworkTransport.GetCurrentRtt(UInt64)

Initialize(NetworkManager)

Initializes the transport

Declaration
public override void Initialize(NetworkManager networkManager = null)
Parameters
TypeNameDescription
NetworkManagernetworkManagerThe NetworkManager that initialized and owns the transport
Overrides

NetworkTransport.Initialize(NetworkManager)

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
TypeNameDescription
System.UInt64clientIdThe clientId this event is for
System.ArraySegment\<System.Byte>payloadThe incoming data payload
System.SinglereceiveTimeThe time the event was received, as reported by Time.realtimeSinceStartup.
Returns
TypeDescription
NetworkEventReturns the event type
Overrides

NetworkTransport.PollEvent(out UInt64, out ArraySegment\<Byte>, out Single)

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
TypeNameDescription
System.UInt64clientIdThe clientId to send to
System.ArraySegment\<System.Byte>payloadThe data to send
NetworkDeliverynetworkDeliveryThe delivery type (QoS) to send data with
Overrides

NetworkTransport.Send(UInt64, ArraySegment\<Byte>, NetworkDelivery)

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
TypeNameDescription
System.StringipAddressIP address of the relay server.
System.UInt16portUDP port of the relay server.
System.Byte[]allocationIdAllocation ID as a byte array.
System.Byte[]keyAllocation key as a byte array.
System.Byte[]connectionDataConnection data as a byte array.
System.Byte[]hostConnectionDataHost's connection data as a byte array.
System.BooleanisSecureWhether the connection is secure (uses DTLS).

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 = null)
Parameters
TypeNameDescription
NetworkEndPointendPointThe remote end point
NetworkEndPointlistenEndPointThe local listen endpoint

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
TypeNameDescription
System.Stringipv4AddressThe remote IP address
System.UInt16portThe remote port
System.StringlistenAddressThe local listen address

SetDebugSimulatorParameters(Int32, Int32, Int32)

Set the parameters for the debug simulator.

Declaration
public void SetDebugSimulatorParameters(int packetDelay, int packetJitter, int dropRate)
Parameters
TypeNameDescription
System.Int32packetDelayPacket delay in milliseconds.
System.Int32packetJitterPacket jitter in milliseconds.
System.Int32dropRatePacket 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
TypeNameDescription
System.StringipAddressIP address of the relay server.
System.UInt16portUDP port of the relay server.
System.Byte[]allocationIdAllocation ID as a byte array.
System.Byte[]keyAllocation key as a byte array.
System.Byte[]connectionDataConnection data as a byte array.
System.BooleanisSecureWhether 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
TypeNameDescription
System.Stringipv4AddressIP address of the relay server.
System.UInt16portUDP port of the relay server.
System.Byte[]allocationIdBytesAllocation ID as a byte array.
System.Byte[]keyBytesAllocation key as a byte array.
System.Byte[]connectionDataBytesConnection data as a byte array.
System.Byte[]hostConnectionDataBytesThe HostConnectionData as a byte array.
System.BooleanisSecureWhether the connection is secure (uses DTLS).

Shutdown()

Shuts down the transport

Declaration
public override void Shutdown()
Overrides

NetworkTransport.Shutdown()

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
TypeDescription
System.Booleantrue if the client was started and false if it failed to start the client
Overrides

NetworkTransport.StartClient()

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
TypeDescription
System.Booleantrue if the server was started and false if it failed to start the server
Overrides

NetworkTransport.StartServer()

Implements

INetworkStreamDriverConstructor