docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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
    IDisposable
    Inherited Members
    ValueType.Equals(object)
    ValueType.GetHashCode()
    ValueType.ToString()
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetType()
    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 MultiNetworkDriver.

    Properties

    Name Description
    DriverCount

    Number of drivers that have been added.

    IsCreated

    Whether the MultiNetworkDriver has been created.

    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 MultiNetworkDriver. 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.

    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 MultiNetworkDriver instance.

    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 MultiNetworkDriver before.

    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 NetworkDriver.

    In This Article
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023