Struct MultiNetworkDriver
The MultiNetworkDriver
structure is a way to manipulate multiple instances of
NetworkDriver at the same time. This abstraction is meant to make it easy to
work with servers that must accept different connection types (e.g. both UDP and WebSocket
connections). This is useful for cross-play support across different platforms.
Implements
Inherited Members
Namespace: Unity.Networking.Transport
Assembly: solution.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
NetworkDriver since they share most of the same APIs.
Fields
Name | Description |
---|---|
MaxDriverCount | The maximum number of drivers that can be added to a |
Properties
Name | Description |
---|---|
DriverCount | Number of drivers that have been added. |
IsCreated | Whether the |
Methods
Name | Description |
---|---|
AbortSend(DataStreamWriter) | Aborts a send started with BeginSend(NetworkPipeline, NetworkConnection, out DataStreamWriter, int). |
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 NetworkConnection value for new connections on servers. |
AddDriver(NetworkDriver) | Add a NetworkDriver instance to the |
BeginSend(NetworkConnection, out DataStreamWriter, int) | Begin sending data on the given connection (default pipeline). |
BeginSend(NetworkPipeline, NetworkConnection, out DataStreamWriter, int) | Begin sending data on the given connection and pipeline. |
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 succeeds (a Connect event will pop on the connection) or fail (a Disconnect event will pop on the connection) at a later time. |
Create() | Create a new |
CreatePipeline(params Type[]) | Create a new pipeline from stage types. |
CreatePipeline(NativeArray<NetworkPipelineStageId>) | Create a new pipeline from stage types. |
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 ScheduleUpdate(JobHandle) and complete it. Failing to do could leave the remote peer unaware that the connection has been closed (however it will time out on its own after a while). |
Dispose() | Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
EndSend(DataStreamWriter) | Enqueue a send operation for the data in the given DataStreamWriter, which must have been obtained by a prior call to BeginSend(NetworkPipeline, NetworkConnection, out DataStreamWriter, int). |
GetConnectionState(NetworkConnection) | Get the current state of the given connection. |
GetDriver(int) | Get a NetworkDriver previously added with AddDriver(NetworkDriver). |
GetDriverForConnection(NetworkConnection) | Get the NetworkDriver associated with the given connection. The connection
must have been obtained from the |
GetRemoteEndpoint(NetworkConnection) | Get the remote endpoint of a connection (the endpoint used to reach the remote peer on the connection). |
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. |
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. |
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. |
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. |
RegisterPipelineStage<T>(T) | Register a custom pipeline stage. |
ScheduleFlushSend(JobHandle) | Schedule a send job. This job is basically a subset of the update job (see ScheduleUpdate(JobHandle)) and only takes care of sending packets queued with EndSend(DataStreamWriter). It should be lightweight enough to schedule multiple times per tick to improve latency if there's a significant amount of packets being sent. |
ScheduleUpdate(JobHandle) | Schedule an update job. This job will process incoming packets and check timeouts (queueing up the relevant events to be consumed by PopEvent(out NetworkConnection, out DataStreamReader) and Accept()) and will send any packets queued with EndSend(DataStreamWriter). This job should generally be scheduled once per tick. |
ToConcurrent() | Create a NetworkDriver.Concurrent copy of the |