Struct MultiNetworkDriver
The MultiNetworkDriver
structure is a way to manipulate multiple instances of
Network
Implements
Inherited Members
Namespace: Unity.Networking.Transport
Assembly: Unity.Networking.Transport.dll
Syntax
public struct MultiNetworkDriver : IDisposable
Examples
This code below shows how to create a MultiNetworkDriver
that accepts both UDP and
WebSocket connections.
var udpDriver = NetworkDriver.Create(new UDPNetworkInterface());
udpDriver.Bind(NetworkEndpoint.AnyIpv4.WithPort(7777)); // UDP port
udpDriver.Listen();
var wsDriver = NetworkDriver.Create(new WebSocketNetworkInterface());
wsDriver.Bind(NetworkEndpoint.AnyIpv4.WithPort(7777)); // TCP port
wsDriver.Listen();
var multiDriver = MultiNetworkDriver.Create();
multiDriver.AddDriver(udpDriver);
multiDriver.AddDriver(wsDriver);
The created MultiNetworkDriver
can then be used as one would use a
Network
Fields
MaxDriverCount
The maximum number of drivers that can be added to a MultiNetworkDriver
.
Declaration
public const int MaxDriverCount = 4
Field Value
Type | Description |
---|---|
int | Number of drivers. |
Properties
DriverCount
Number of drivers that have been added.
Declaration
public readonly int DriverCount { get; }
Property Value
Type | Description |
---|---|
int | Number of drivers. |
IsCreated
Whether the MultiNetworkDriver
has been created.
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool | True if created, false otherwise. |
Methods
AbortSend(DataStreamWriter)
Aborts a send started with Begin
Declaration
public void AbortSend(DataStreamWriter writer)
Parameters
Type | Name | Description |
---|---|---|
Data |
writer | Unity. |
Exceptions
Type | Condition |
---|---|
Argument |
If |
Accept()
Accept any new incoming connections. Connections must be accepted before data can be
sent on them. It's also the only way to obtain the Network
Declaration
public NetworkConnection Accept()
Returns
Type | Description |
---|---|
Network |
New connection if any, otherwise a default-value object. |
Accept(out NativeArray<byte>)
Accept any new incoming connections. Connections must be accepted before data can be
sent on them. It's also the only way to obtain the Network
Declaration
public NetworkConnection Accept(out NativeArray<byte> payload)
Parameters
Type | Name | Description |
---|---|---|
Native |
payload | Payload that was provided on the new connection's Connect(Network |
Returns
Type | Description |
---|---|
Network |
New connection if any, otherwise a default-value object. |
AddDriver(NetworkDriver)
Add a NetworkMultiNetworkDriver
. This driver
instance must not already have any active connections, and must have the same number of
pipelines as previously added instances. Drivers that are intended to take on a server
role must also already be in the listening state.
Declaration
public int AddDriver(NetworkDriver driver)
Parameters
Type | Name | Description |
---|---|---|
Network |
driver | Network |
Returns
Type | Description |
---|---|
int | An ID identifying the Network |
Remarks
The MultiNetworkDriver
takes ownership of the NetworkMultiNetworkDriver
API).
Exceptions
Type | Condition |
---|---|
Invalid |
If the |
Argument |
If the driver already has active connections, or if it doesn't have as many pipelines as previously added drivers. Note that this exception is only thrown when safety checks are enabled (i.e. in the editor), otherwise the driver will be added anyway. |
BeginSend(NetworkConnection, out DataStreamWriter, int)
Begin sending data on the given connection (default pipeline).
Declaration
public int BeginSend(NetworkConnection connection, out DataStreamWriter writer, int requiredPayloadSize = 0)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection to send the data to. |
Data |
writer | Unity. |
int | requiredPayloadSize | Size that the returned Unity. |
Returns
Type | Description |
---|---|
int | 0 on success, a negative value on error. |
Exceptions
Type | Condition |
---|---|
Argument |
If |
BeginSend(NetworkPipeline, NetworkConnection, out DataStreamWriter, int)
Begin sending data on the given connection and pipeline.
Declaration
public int BeginSend(NetworkPipeline pipe, NetworkConnection connection, out DataStreamWriter writer, int requiredPayloadSize = 0)
Parameters
Type | Name | Description |
---|---|---|
Network |
pipe | Pipeline to send the data on. |
Network |
connection | Connection to send the data to. |
Data |
writer | Unity. |
int | requiredPayloadSize | Size that the returned Unity. |
Returns
Type | Description |
---|---|
int | 0 on success, a negative error code on error. |
Exceptions
Type | Condition |
---|---|
Argument |
If |
Connect(int, NetworkEndpoint)
Establish a new connection to the given endpoint. Note that this only starts establishing the new connection. From there it will either succeed (a Connect event will pop on the connection) or fail (a Disconnect event will pop on the connection) at a later time.
Declaration
public NetworkConnection Connect(int driverId, NetworkEndpoint endpoint)
Parameters
Type | Name | Description |
---|---|---|
int | driverId | ID of the driver to connect, as obtained with Add |
Network |
endpoint | Endpoint to connect to. |
Returns
Type | Description |
---|---|
Network |
New connection object, or a default-valued object on failure. |
Remarks
Establishing a new connection normally requires the driver to be bound (e.g. with the
Bind(Networkconnect
automatically binds the socket to
an ephemeral port. For custom endpoints (endpoints where the network family is set to
Custom), the implicit bind is done on the endpoint passed to
the method. If binding to a separate endpoint is required, call Bind(Network
Connect(int, NetworkEndpoint, NativeArray<byte>)
Establish a new connection to the given endpoint. Note that this only starts establishing the new connection. From there it will either succeed (a Connect event will pop on the connection) or fail (a Disconnect event will pop on the connection) at a later time.
Declaration
public NetworkConnection Connect(int driverId, NetworkEndpoint endpoint, NativeArray<byte> payload)
Parameters
Type | Name | Description |
---|---|---|
int | driverId | ID of the driver to connect, as obtained with Add |
Network |
endpoint | Endpoint to connect to. |
Native |
payload | Payload to send to the remote peer along with the connection request. This can be
recovered server-side when calling Accept(out Native |
Returns
Type | Description |
---|---|
Network |
New connection object, or a default-valued object on failure. |
Remarks
Establishing a new connection normally requires the driver to be bound (e.g. with the
Bind(Networkconnect
automatically binds the socket to
an ephemeral port. For custom endpoints (endpoints where the network family is set to
Custom), the implicit bind is done on the endpoint passed to
the method. If binding to a separate endpoint is required, call Bind(Network
Create()
Create a new MultiNetworkDriver
instance.
Declaration
public static MultiNetworkDriver Create()
Returns
Type | Description |
---|---|
Multi |
The new |
CreatePipeline(params Type[])
Create a new pipeline from stage types.
Declaration
public NetworkPipeline CreatePipeline(params Type[] stages)
Parameters
Type | Name | Description |
---|---|---|
Type[] | stages | Array of stages the pipeline should contain. |
Returns
Type | Description |
---|---|
Network |
Remarks
The order of the different stages is important, as that is the order in which the stages will process a packet when sending messages (the reverse order is used when processing received packets).
Exceptions
Type | Condition |
---|---|
Invalid |
If called after the driver has established connections or before it is created. Note this is only thrown if safety checks are enabled (i.e. in the editor). |
CreatePipeline(NativeArray<NetworkPipelineStageId>)
Create a new pipeline from stage types.
Declaration
public NetworkPipeline CreatePipeline(NativeArray<NetworkPipelineStageId> stages)
Parameters
Type | Name | Description |
---|---|---|
Native |
stages | Array of stages the pipeline should contain. |
Returns
Type | Description |
---|---|
Network |
Remarks
The order of the different stages is important, as that is the order in which the stages will process a packet when sending messages (the reverse order is used when processing received packets).
Exceptions
Type | Condition |
---|---|
Invalid |
If called after the driver has established connections or before it is created. Note this is only thrown if safety checks are enabled (i.e. in the editor). |
Disconnect(NetworkConnection)
Close a connection. Note that to properly notify a peer of this disconnection, it is
required to schedule at least one update with Schedule
Declaration
public void Disconnect(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection to close. |
Exceptions
Type | Condition |
---|---|
Argument |
If |
Dispose()
Declaration
public void Dispose()
EndSend(DataStreamWriter)
Enqueue a send operation for the data in the given Unity.
Declaration
public int EndSend(DataStreamWriter writer)
Parameters
Type | Name | Description |
---|---|---|
Data |
writer | Unity. |
Returns
Type | Description |
---|---|
int | The number of bytes to be sent on success, a negative value on error. |
Remarks
This method doesn't actually send anything on the wire. It simply enqueues the send
operation, which will be performed in the next Schedule
Exceptions
Type | Condition |
---|---|
Argument |
If |
GetConnectionState(NetworkConnection)
Get the current state of the given connection.
Declaration
public NetworkConnection.State GetConnectionState(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection to get the state of. |
Returns
Type | Description |
---|---|
Network |
State of the connection. |
Exceptions
Type | Condition |
---|---|
Argument |
If |
GetDriver(int)
Get a Network
Declaration
public NetworkDriver GetDriver(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | ID of the driver as returned by Add |
Returns
Type | Description |
---|---|
Network |
The Network |
Remarks
This method is provided as an escape hatch for use cases not covered by the
MultiNetworkDriver
API. Using this method to perform operations on a driver that
could be performed through MultiNetworkDriver
is likely to result in errors or
corrupted state.
Exceptions
Type | Condition |
---|---|
Argument |
If |
GetDriverForConnection(NetworkConnection)
Get the NetworkMultiNetworkDriver
before.
Declaration
public NetworkDriver GetDriverForConnection(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection to get the driver of. |
Returns
Type | Description |
---|---|
Network |
The Network |
Exceptions
Type | Condition |
---|---|
Argument |
If |
GetRemoteEndpoint(NetworkConnection)
Get the remote endpoint of a connection (the endpoint used to reach the remote peer on the connection).
Declaration
public NetworkEndpoint GetRemoteEndpoint(NetworkConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection to get the endpoint of. |
Returns
Type | Description |
---|---|
Network |
The remote endpoint of the connection. |
Remarks
The returned value should not be assumed to be constant for a given connection, as it is possible for remote peers to change address during the course of a session (e.g. if a mobile client changes IP address because they're hopping between cell towers).
Exceptions
Type | Condition |
---|---|
Argument |
If |
PopEvent(out NetworkConnection, out DataStreamReader)
Pops the next event from the event queue, Empty will be returned if there are no more events to pop.
Declaration
public NetworkEvent.Type PopEvent(out NetworkConnection connection, out DataStreamReader reader)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection on which the event occured. |
Data |
reader | Unity. |
Returns
Type | Description |
---|---|
Network |
The type of the event popped. |
Remarks
The reader
obtained from this method will contain different things for different
event types. For Data, it contains the actual received
payload. For Disconnect, it contains a single byte
identifying the reason for the disconnection (see Disconnect
PopEvent(out NetworkConnection, out DataStreamReader, out NetworkPipeline)
Pops the next event from the event queue, Empty will be returned if there are no more events to pop.
Declaration
public NetworkEvent.Type PopEvent(out NetworkConnection connection, out DataStreamReader reader, out NetworkPipeline pipe)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection on which the event occured. |
Data |
reader | Unity. |
Network |
pipe | Pipeline on which the data event was received. |
Returns
Type | Description |
---|---|
Network |
The type of the event popped. |
Remarks
The reader
obtained from this method will contain different things for different
event types. For Data, it contains the actual received
payload. For Disconnect, it contains a single byte
identifying the reason for the disconnection (see Disconnect
PopEventForConnection(NetworkConnection, out DataStreamReader)
Pops the next event from the event queue for the given connection, Empty will be returned if there are no more events.
Declaration
public NetworkEvent.Type PopEventForConnection(NetworkConnection connection, out DataStreamReader reader)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection for which to pop the event. |
Data |
reader | Unity. |
Returns
Type | Description |
---|---|
Network |
The type of the event popped. |
Remarks
The reader
obtained from this method will contain different things for different
event types. For Data, it contains the actual received
payload. For Disconnect, it contains a single byte
identifying the reason for the disconnection (see Disconnect
Exceptions
Type | Condition |
---|---|
Argument |
If |
PopEventForConnection(NetworkConnection, out DataStreamReader, out NetworkPipeline)
Pops the next event from the event queue for the given connection, Empty will be returned if there are no more events.
Declaration
public NetworkEvent.Type PopEventForConnection(NetworkConnection connection, out DataStreamReader reader, out NetworkPipeline pipe)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection for which to pop the event. |
Data |
reader | Unity. |
Network |
pipe | Pipeline on which the data event was received. |
Returns
Type | Description |
---|---|
Network |
The type of the event popped. |
Remarks
The reader
obtained from this method will contain different things for different
event types. For Data, it contains the actual received
payload. For Disconnect, it contains a single byte
identifying the reason for the disconnection (see Disconnect
Exceptions
Type | Condition |
---|---|
Argument |
If |
RegisterPipelineStage<T>(T)
Register a custom pipeline stage.
Declaration
public void RegisterPipelineStage<T>(T stage) where T : unmanaged, INetworkPipelineStage
Parameters
Type | Name | Description |
---|---|---|
T | stage | An instance of the pipeline stage. |
Type Parameters
Name | Description |
---|---|
T | Type of the pipeline stage (must be unmanaged). |
Remarks
Can only be called before a driver is bound (see Bind(Network
Note that the default pipeline stages (Fragmentation
Exceptions
Type | Condition |
---|---|
Invalid |
If the driver is not created or bound. Note that this is only thrown if safety checks are enabled (i.e. in the editor). Otherwise the pipeline is registered anyway (with likely erroneous behavior down the line). |
ScheduleFlushSend(JobHandle)
Schedule a send job. This job is basically a subset of the update job (see
Schedule
Declaration
public JobHandle ScheduleFlushSend(JobHandle dependency = default)
Parameters
Type | Name | Description |
---|---|---|
Job |
dependency | Job to depend on. |
Returns
Type | Description |
---|---|
Job |
Handle to the send job. |
ScheduleUpdate(JobHandle)
Schedule an update job. This job will process incoming packets and check timeouts
(queueing up the relevant events to be consumed by Pop
Declaration
public JobHandle ScheduleUpdate(JobHandle dependency = default)
Parameters
Type | Name | Description |
---|---|---|
Job |
dependency | Job to depend on. |
Returns
Type | Description |
---|---|
Job |
Handle to the update job. |
ToConcurrent()
Create a NetworkNetworkDriver
.
Declaration
public MultiNetworkDriver.Concurrent ToConcurrent()
Returns
Type | Description |
---|---|
Multi |
A Network |