Struct NetworkStreamDriver
Singleton that can hold a reference to the Network
Inherited Members
Namespace: Unity.NetCode
Assembly: Unity.NetCode.dll
Syntax
public struct NetworkStreamDriver : IComponentData, IQueryTypeParameter
Properties
ConcurrentDriverStore
A reference to the concurrent version of the Network
Declaration
public ref ConcurrentDriverStore ConcurrentDriverStore { get; }
Property Value
Type | Description |
---|---|
Concurrent |
ConnectionEventsForTick
Stores all Net
It's a self-cleaning list and therefore has no consume API (in other words, there's no need to explicitly
remove entries from this collection, which is why it's read-only). This also means that events are only
valid for a single Simulation
This collection is cleared and repopulated in the Network
[BurstCompile]
void ISystem.OnUpdate(ref SystemState state)
{
foreach (var evt in SystemAPI.GetSingleton<NetworkStreamDriver>().ConnectionEventsForTick)
{
UnityEngine.Debug.Log($"[{state.WorldUnmanaged.Name}] {evt.ToFixedString()}!");
}
}
Declaration
public readonly NativeArray<NetCodeConnectionEvent>.ReadOnly ConnectionEventsForTick { get; }
Property Value
Type | Description |
---|---|
Native |
Remarks
This collection can be safely passed into jobs, as long as the Network
These events are raised on client worlds as well, but only for your own client world.
I.e. Each client does not receive events regarding other clients. Refer to the PlayerList NetcodeSamples sample
(https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/NetcodeSamples/Assets/Samples/PlayerList)
for an example implementation of RPC logic that actually communicates player join and leave events (with
display names and Network
DriverStore
A pointer to the underlying Driver
Warning: You MUST fetch Network
Declaration
public ref NetworkDriverStore DriverStore { get; }
Property Value
Type | Description |
---|---|
Network |
Remarks
Network
Copying such a large struct is expensive, prefer ref var driverStore = ref networkStreamDriver.RefRW.DriverStore;
syntax.
LastEndPoint
Convenience. Records the DriverStore used in the latest call to Listen(Network
Declaration
public readonly NetworkEndpoint LastEndPoint { get; }
Property Value
Type | Description |
---|---|
Network |
Remarks
Note that the actual Endpoint used by each Network
See Sanitize
RequireConnectionApproval
Require all incoming connections to all the drivers in the driver store to go through the connection
approval process. If turned off the connections are immediately approved and go from connecting to
the handshake state.
Server-only. Always false on the client.
Declaration
public bool RequireConnectionApproval { get; set; }
Property Value
Type | Description |
---|---|
bool |
Methods
Connect(EntityManager, NetworkEndpoint, Entity)
Initiate a connection to the remote endpoint
address.
Declaration
public Entity Connect(EntityManager entityManager, NetworkEndpoint endpoint, Entity ent = default)
Parameters
Type | Name | Description |
---|---|---|
Entity |
entityManager | The entity manager to use to create the new entity, if |
Network |
endpoint | The remote address we want to connect |
Entity | ent | An optional entity to use to create the connection. If not set, a new entity will be create instead |
Returns
Type | Description |
---|---|
Entity | The entity that hold the Network |
Exceptions
Type | Condition |
---|---|
Invalid |
Throw an exception if the driver is not created or if multiple drivers are register |
GetConnectionState(NetworkStreamConnection)
The current state of the internal transport connection.
Declaration
public NetworkConnection.State GetConnectionState(NetworkStreamConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection |
Returns
Type | Description |
---|---|
Network |
The current state of the internal transport connection |
Remarks
Is different from the Connection
GetLocalEndPoint()
Get the local endpoint (the endpoint remote peers will use to reach this driver) used by the first driver inside Network
Declaration
public NetworkEndpoint GetLocalEndPoint()
Returns
Type | Description |
---|---|
Network |
The local endpoint of the first driver. |
GetLocalEndPoint(int)
Get the local endpoint used by the driver (the endpoint remote peers will use to reach this driver).
When multiple drivers exist, e.g. when using both IPC and Socket connection, multiple drivers will be available
in the Network
Declaration
public NetworkEndpoint GetLocalEndPoint(int driverId)
Parameters
Type | Name | Description |
---|---|---|
int | driverId | Id of the driver. See Get |
Returns
Type | Description |
---|---|
Network |
The local endpoint of the driver. |
GetRemoteEndPoint(NetworkStreamConnection)
The remote connection address. This is the seen public ip address of the connection.
Declaration
public NetworkEndpoint GetRemoteEndPoint(NetworkStreamConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection |
Returns
Type | Description |
---|---|
Network |
When relay is used, the current relay host address. Otherwise the remote endpoint address. |
Remarks
Be aware that this method work sliglty differnetly than the NetworkDriver.GetRemoteEndpoint.
The Unity.
Listen(NetworkEndpoint)
Tell all the registered Network
Declaration
public bool Listen(NetworkEndpoint endpoint)
Parameters
Type | Name | Description |
---|---|---|
Network |
endpoint | The local address to use. This is the address that will be used to bind the underlying socket. |
Returns
Type | Description |
---|---|
bool | Whether the drivers starts listening |
ResetDriverStore(WorldUnmanaged, ref NetworkDriverStore)
Reset the current Driver
Declaration
public void ResetDriverStore(WorldUnmanaged world, ref NetworkDriverStore driverStore)
Parameters
Type | Name | Description |
---|---|---|
World |
world | The world the NetworkStreamDriver singleton is part of. |
Network |
driverStore | The new driver store to use. |
Examples
var driverStore = new NetworkDriverStore();
var constructor = NetworkStreamReceiveSystem.DriverConstructor;
constructor.CreateServerDriver(serverWorld, ref driverStore, netDebug);
var driver = EntityManager.CreateEntityQuery(typeof(NetworkStreamDriver)).GetSingleton<NetworkStreamDriver>();
driver.ResetDriverStore(driverStore);
var listenEndPoint = NetworkEndpoint.AnyIpv4.WithPort(MyPort);
driver.Listen(listenEndPoint);
UseRelay(NetworkStreamConnection)
Check if the given connection is using relay to connect to the remote endpoint
Declaration
public bool UseRelay(NetworkStreamConnection connection)
Parameters
Type | Name | Description |
---|---|---|
Network |
connection | Connection |
Returns
Type | Description |
---|---|
bool | Either if the connection is using the relay or not. |