Struct NetworkDriver
The NetworkDriver is an implementation of Virtual Connections over any transport.
Basic usage:
var driver = NetworkDriver.Create();
Implements
Inherited Members
Namespace: Unity.Networking.Transport
Assembly: Unity.Networking.Transport.dll
Syntax
public struct NetworkDriver : IDisposable
Constructors
NetworkDriver(INetworkInterface)
Declaration
public NetworkDriver(INetworkInterface netIf)
Parameters
Type | Name | Description |
---|---|---|
INetwork |
netIf |
NetworkDriver(INetworkInterface, params INetworkParameter[])
Declaration
[Obsolete("Use NetworkDriver(INetworkInterface, NetworkSettings) instead", false)]
public NetworkDriver(INetworkInterface netIf, params INetworkParameter[] param)
Parameters
Type | Name | Description |
---|---|---|
INetwork |
netIf | |
INetwork |
param |
NetworkDriver(INetworkInterface, NetworkSettings)
Declaration
public NetworkDriver(INetworkInterface netIf, NetworkSettings settings)
Parameters
Type | Name | Description |
---|---|---|
INetwork |
netIf | |
Network |
settings |
Properties
Bound
Whether the driver is bound to an endpoint.
Declaration
public bool Bound { get; }
Property Value
Type | Description |
---|---|
bool |
IsCreated
Returns if NetworkDriver has been created
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool |
LastUpdateTime
Gets the value of the last update time
Declaration
public long LastUpdateTime { get; }
Property Value
Type | Description |
---|---|
long |
Listening
Whether the driver is listening for connections.
Declaration
public bool Listening { get; }
Property Value
Type | Description |
---|---|
bool |
ReceiveErrorCode
Gets or sets Receive Error Code
Declaration
public int ReceiveErrorCode { get; }
Property Value
Type | Description |
---|---|
int |
Methods
AbortSend(DataStreamWriter)
Aborts a asynchronous send.
Declaration
public void AbortSend(DataStreamWriter writer)
Parameters
Type | Name | Description |
---|---|---|
Data |
writer | If you require the payload to be of certain size. |
Exceptions
Type | Condition |
---|---|
Invalid |
If endsend is called with a matching BeginSend call. |
Invalid |
If the connection got closed between the call of being and end send. |
Accept()
Checks to see if there are any new connections to Accept.
Declaration
public NetworkConnection Accept()
Returns
Type | Description |
---|---|
Network |
If accept fails it returns a default NetworkConnection. |
BeginSend(NetworkConnection, out DataStreamWriter, int)
Acquires a DataStreamWriter for starting a asynchronous send.
Declaration
public int BeginSend(NetworkConnection id, out DataStreamWriter writer, int requiredPayloadSize = 0)
Parameters
Type | Name | Description |
---|---|---|
Network |
id | The NetworkConnection id to write through |
Data |
writer | A DataStreamWriter to write to |
int | requiredPayloadSize | If you require the payload to be of certain size |
Returns
Type | Description |
---|---|
int | Returns Success on a successful acquire. Otherwise returns an Status |
Remarks
Will throw a Invalid
BeginSend(NetworkPipeline, NetworkConnection, out DataStreamWriter, int)
Acquires a DataStreamWriter for starting a asynchronous send.
Declaration
public int BeginSend(NetworkPipeline pipe, NetworkConnection id, out DataStreamWriter writer, int requiredPayloadSize = 0)
Parameters
Type | Name | Description |
---|---|---|
Network |
pipe | The NetworkPipeline to write through |
Network |
id | The NetworkConnection id to write through |
Data |
writer | A DataStreamWriter to write to |
int | requiredPayloadSize | If you require the payload to be of certain size |
Returns
Type | Description |
---|---|
int | Returns Success on a successful acquire. Otherwise returns an Status |
Remarks
Will throw a Invalid
Bind(NetworkEndPoint)
Bind the driver to a endpoint.
Declaration
public int Bind(NetworkEndPoint endpoint)
Parameters
Type | Name | Description |
---|---|---|
Network |
endpoint | The endpoint to bind to. |
Returns
Type | Description |
---|---|
int | Returns 0 on success. And a negative value if a error occured. |
Exceptions
Type | Condition |
---|---|
Invalid |
If the driver is not created properly |
Invalid |
If bind is called more then once on the driver |
Invalid |
If bind is called after a connection has already been established |
Connect(NetworkEndPoint)
Connects the driver to a endpoint
Declaration
public NetworkConnection Connect(NetworkEndPoint endpoint)
Parameters
Type | Name | Description |
---|---|---|
Network |
endpoint | Endpoint to connect to. |
Returns
Type | Description |
---|---|
Network |
If connect fails it returns a default NetworkConnection. |
Exceptions
Type | Condition |
---|---|
Invalid |
If the driver is not created properly |
Create()
Helper function for creating a NetworkDriver.
Declaration
public static NetworkDriver Create()
Returns
Type | Description |
---|---|
Network |
The new Network |
Create(params INetworkParameter[])
Declaration
[Obsolete("Use Create(NetworkSettings) instead", false)]
public static NetworkDriver Create(params INetworkParameter[] param)
Parameters
Type | Name | Description |
---|---|---|
INetwork |
param |
Returns
Type | Description |
---|---|
Network |
Create(NetworkSettings)
Helper function for creating a NetworkDriver.
Declaration
public static NetworkDriver Create(NetworkSettings settings)
Parameters
Type | Name | Description |
---|---|---|
Network |
settings | The NewtorkSettings for the new driver. |
Returns
Type | Description |
---|---|
Network |
The new Network |
Exceptions
Type | Condition |
---|---|
Invalid |
CreatePipeline(params Type[])
Create a new pipeline.
Declaration
public NetworkPipeline CreatePipeline(params Type[] stages)
Parameters
Type | Name | Description |
---|---|---|
Type[] | stages | An array of stages the pipeline should contain. |
Returns
Type | Description |
---|---|
Network |
The newly-created Network |
Exceptions
Type | Condition |
---|---|
Invalid |
If the driver is not created properly |
Invalid |
A connection has already been established |
Create<N>(N)
Helper function for creating a NetworkDriver.
Declaration
public static NetworkDriver Create<N>(N networkInterface) where N : INetworkInterface
Parameters
Type | Name | Description |
---|---|---|
N | networkInterface | The custom interface to use. |
Returns
Type | Description |
---|---|
Network |
The new Network |
Type Parameters
Name | Description |
---|---|
N |
Create<N>(N, NetworkSettings)
Helper function for creating a NetworkDriver.
Declaration
public static NetworkDriver Create<N>(N networkInterface, NetworkSettings settings) where N : INetworkInterface
Parameters
Type | Name | Description |
---|---|---|
N | networkInterface | The custom interface to use. |
Network |
settings | The NewtorkSettings for the new NetworkDriver. |
Returns
Type | Description |
---|---|
Network |
The new Network |
Type Parameters
Name | Description |
---|---|
N |
Disconnect(NetworkConnection)
Disconnects a NetworkConnection
Declaration
public int Disconnect(NetworkConnection id)
Parameters
Type | Name | Description |
---|---|---|
Network |
id | The NetworkConnection we want to Disconnect. |
Returns
Type | Description |
---|---|
int | Return 0 on success. |
Dispose()
Declaration
public void Dispose()
EndSend(DataStreamWriter)
Ends a asynchronous send.
Declaration
public int EndSend(DataStreamWriter writer)
Parameters
Type | Name | Description |
---|---|---|
Data |
writer | If you require the payload to be of certain size. |
Returns
Type | Description |
---|---|
int | The length of the buffer sent if nothing went wrong. |
Exceptions
Type | Condition |
---|---|
Invalid |
If endsend is called with a matching BeginSend call. |
Invalid |
If the connection got closed between the call of being and end send. |
GetConnectionState(NetworkConnection)
Gets the connection state using the specified Network
Declaration
public NetworkConnection.State GetConnectionState(NetworkConnection con)
Parameters
Type | Name | Description |
---|---|---|
Network |
con | The connection |
Returns
Type | Description |
---|---|
Network |
The network connection state |
GetEventQueueSizeForConnection(NetworkConnection)
Returns the size of the EventQueue for a specific connection
Declaration
public int GetEventQueueSizeForConnection(NetworkConnection connectionId)
Parameters
Type | Name | Description |
---|---|---|
Network |
connectionId | Connection for which to get the event queue size. |
Returns
Type | Description |
---|---|
int | If the connection is valid it returns the size of the event queue otherwise it returns 0. |
GetPipelineBuffers(NetworkPipeline, NetworkPipelineStageId, NetworkConnection, out NativeArray<byte>, out NativeArray<byte>, out NativeArray<byte>)
Returns the PipelineBuffers for a specific pipeline and stage.
Declaration
public void GetPipelineBuffers(NetworkPipeline pipeline, NetworkPipelineStageId stageId, NetworkConnection connection, out NativeArray<byte> readProcessingBuffer, out NativeArray<byte> writeProcessingBuffer, out NativeArray<byte> sharedBuffer)
Parameters
Type | Name | Description |
---|---|---|
Network |
pipeline | Pipeline for which to get the buffers. |
Network |
stageId | Pipeline for which to get the buffers. |
Network |
connection | Connection for which to the buffers. |
Native |
readProcessingBuffer | The buffer used to process read (receive) operations. |
Native |
writeProcessingBuffer | The buffer used to process write (send) operations. |
Native |
sharedBuffer | The buffer containing the internal state of the pipeline stage. |
Exceptions
Type | Condition |
---|---|
Invalid |
If the the connection is invalid. |
Listen()
Set the driver to Listen for incoming connections
Declaration
public int Listen()
Returns
Type | Description |
---|---|
int | Returns 0 on success. |
Exceptions
Type | Condition |
---|---|
Invalid |
If the driver is not created properly |
Invalid |
If listen is called more then once on the driver |
Invalid |
If bind has not been called before calling Listen. |
LocalEndPoint()
Returns local Network
Declaration
public NetworkEndPoint LocalEndPoint()
Returns
Type | Description |
---|---|
Network |
The network end point |
MaxHeaderSize(NetworkPipeline)
Max headersize including optional Network
Declaration
public int MaxHeaderSize(NetworkPipeline pipe)
Parameters
Type | Name | Description |
---|---|---|
Network |
pipe | The pipeline for which to get the maximum header size. |
Returns
Type | Description |
---|---|
int | The maximum header size. |
PopEvent(out NetworkConnection, out DataStreamReader)
Pops an event
Declaration
public NetworkEvent.Type PopEvent(out NetworkConnection con, out DataStreamReader reader)
Parameters
Type | Name | Description |
---|---|---|
Network |
con | Connection on which the event occured. |
Data |
reader | Stream reader for the event's data. |
Returns
Type | Description |
---|---|
Network |
The event's type |
PopEvent(out NetworkConnection, out DataStreamReader, out NetworkPipeline)
Pops an event
Declaration
public NetworkEvent.Type PopEvent(out NetworkConnection con, out DataStreamReader reader, out NetworkPipeline pipeline)
Parameters
Type | Name | Description |
---|---|---|
Network |
con | Connection on which the event occured. |
Data |
reader | Stream reader for the event's data. |
Network |
pipeline | Pipeline on which the event was received. |
Returns
Type | Description |
---|---|
Network |
The event's type |
PopEventForConnection(NetworkConnection, out DataStreamReader)
Pops an event for a specific connection
Declaration
public NetworkEvent.Type PopEventForConnection(NetworkConnection connectionId, out DataStreamReader reader)
Parameters
Type | Name | Description |
---|---|---|
Network |
connectionId | Connection for which to pop the next event. |
Data |
reader | Stream reader for the event's data. |
Returns
Type | Description |
---|---|
Network |
Returns the type of event received, if the value is a Disconnect event then the DataStreamReader will contain the disconnect reason. |
PopEventForConnection(NetworkConnection, out DataStreamReader, out NetworkPipeline)
Pops an event for a specific connection
Declaration
public NetworkEvent.Type PopEventForConnection(NetworkConnection connectionId, out DataStreamReader reader, out NetworkPipeline pipeline)
Parameters
Type | Name | Description |
---|---|---|
Network |
connectionId | Connection for which to pop the next event. |
Data |
reader | Stream reader for the event's data. |
Network |
pipeline | Pipeline on which the event was received. |
Returns
Type | Description |
---|---|
Network |
The event's type |
RemoteEndPoint(NetworkConnection)
Declaration
public NetworkEndPoint RemoteEndPoint(NetworkConnection id)
Parameters
Type | Name | Description |
---|---|---|
Network |
id |
Returns
Type | Description |
---|---|
Network |
ScheduleFlushSend(JobHandle)
Schedules flushing the sendqueue. Should be called in cases where you want the driver to send before the
next Schedule
Declaration
public JobHandle ScheduleFlushSend(JobHandle dep)
Parameters
Type | Name | Description |
---|---|---|
Job |
dep | Job on which to depend. |
Returns
Type | Description |
---|---|
Job |
The job handle |
ScheduleUpdate(JobHandle)
Schedules update for driver. This should be called once a frame.
Declaration
public JobHandle ScheduleUpdate(JobHandle dep = default)
Parameters
Type | Name | Description |
---|---|---|
Job |
dep | Job on which to depend. |
Returns
Type | Description |
---|---|
Job |
The update job's handle |
ToConcurrent()
Create a Concurrent Copy of the NetworkDriver.
Declaration
public NetworkDriver.Concurrent ToConcurrent()
Returns
Type | Description |
---|---|
Network |
Concurrent version of the driver. |