@@ -13,9 +13,6 @@ public struct NetcodeConnection
1313 internal Entity Entity ;
1414 public int NetworkId ;
1515
16- internal float ConnectedTime ;
17- internal bool IsSynced ;
18-
1916 public bool IsServer => World . IsServer ( ) ;
2017 public void GoInGame ( )
2118 {
@@ -48,6 +45,8 @@ protected override void OnUpdate()
4845 {
4946 var isServer = World . IsServer ( ) ;
5047 var commandBuffer = new EntityCommandBuffer ( Allocator . Temp ) ;
48+ var networkManager = NetworkManager . Singleton ;
49+
5150 foreach ( var ( networkId , connectionState , entity ) in SystemAPI . Query < NetworkId , ConnectionState > ( ) . WithNone < NetworkStreamConnection > ( ) . WithEntityAccess ( ) )
5251 {
5352 commandBuffer . RemoveComponent < ConnectionState > ( entity ) ;
@@ -60,16 +59,12 @@ protected override void OnUpdate()
6059
6160 m_TempConnections . Clear ( ) ;
6261
63-
62+ // TODO: We should figure out how to associate the N4E NetworkId with the NGO ClientId
6463 foreach ( var ( networkId , entity ) in SystemAPI . Query < NetworkId > ( ) . WithAll < NetworkStreamConnection > ( ) . WithNone < NetworkStreamInGame > ( ) . WithEntityAccess ( ) )
6564 {
66- // TODO-Unified: For new connections, we have a delay before the N4E in-game state for the client to provide time for the NGO side of the client to synchronize.
67- // Note: Once both are using the same transport we should be able to get the transport id and determine the NGO assigned client-id and at that point once the
68- // client has signaled that it has synchronized (or has been sent the synchronization data) we finalize the in-game connection state (or something along those lines).
6965 if ( ! m_NewConnections . ContainsKey ( networkId . Value ) )
7066 {
71- var delayTime = 0.0f ; // isServer ? 0.2f : 0.1f;
72- var newConnection = new NetcodeConnection { World = World , Entity = entity , NetworkId = networkId . Value , ConnectedTime = UnityEngine . Time . realtimeSinceStartup + delayTime , IsSynced = isServer } ;
67+ var newConnection = new NetcodeConnection { World = World , Entity = entity , NetworkId = networkId . Value } ;
7368 m_NewConnections . Add ( networkId . Value , newConnection ) ;
7469 }
7570 }
@@ -79,8 +74,9 @@ protected override void OnUpdate()
7974 {
8075 foreach ( var entry in m_NewConnections )
8176 {
82- // Check if the delay time has passed.
83- if ( entry . Value . IsSynced && entry . Value . ConnectedTime < UnityEngine . Time . realtimeSinceStartup )
77+ // Server: always connect
78+ // Client: wait until we have synchronized before announcing we are ready to receive snapshots
79+ if ( networkManager . IsServer || ( ! networkManager . IsServer && networkManager . IsConnectedClient ) )
8480 {
8581 // Set the connection in-game
8682 commandBuffer . AddComponent < NetworkStreamInGame > ( entry . Value . Entity ) ;
@@ -97,21 +93,29 @@ protected override void OnUpdate()
9793 }
9894 m_TempConnections . Clear ( ) ;
9995
96+ // If the local NetworkManager is shutting down or no longer connected, then
97+ // make sure we have disconnected all known connections.
98+ if ( networkManager . ShutdownInProgress || ! networkManager . IsListening )
99+ {
100+ foreach ( var ( networkId , entity ) in SystemAPI . Query < NetworkId > ( ) . WithEntityAccess ( ) )
101+ {
102+ commandBuffer . RemoveComponent < ConnectionState > ( entity ) ;
103+ NetworkManager . OnNetCodeDisconnect ? . Invoke ( new NetcodeConnection { World = World , Entity = entity , NetworkId = networkId . Value } ) ;
104+ }
105+ }
100106 commandBuffer . Playback ( EntityManager ) ;
101107 }
102108
109+ /// <summary>
110+ /// Always disconnect all known connections when being destroyed.
111+ /// </summary>
103112 protected override void OnDestroy ( )
104113 {
105114 var commandBuffer = new EntityCommandBuffer ( Allocator . Temp ) ;
106115 foreach ( var ( networkId , entity ) in SystemAPI . Query < NetworkId > ( ) . WithEntityAccess ( ) )
107116 {
108117 commandBuffer . RemoveComponent < ConnectionState > ( entity ) ;
109- // TODO: maybe disconnect reason?
110- m_TempConnections . Add ( new NetcodeConnection { World = World , Entity = entity , NetworkId = networkId . Value } ) ;
111- }
112- foreach ( var con in m_TempConnections )
113- {
114- NetworkManager . OnNetCodeDisconnect ? . Invoke ( con ) ;
118+ NetworkManager . OnNetCodeDisconnect ? . Invoke ( new NetcodeConnection { World = World , Entity = entity , NetworkId = networkId . Value } ) ;
115119 }
116120 commandBuffer . Playback ( EntityManager ) ;
117121 base . OnDestroy ( ) ;
0 commit comments