Files
sbox-public/engine/Sandbox.Engine/Systems/Networking/System/NetworkSystem.Sockets.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

61 lines
1.2 KiB
C#

using System.Collections.Concurrent;
namespace Sandbox.Network;
internal partial class NetworkSystem
{
[SkipHotload] readonly ConcurrentBag<NetworkSocket> sockets = new();
internal IEnumerable<NetworkSocket> Sockets => sockets;
internal void AddSocket( NetworkSocket socket )
{
if ( IsDisconnected )
{
Log.Warning( "Tried to call AddSocket on a disconnected NetworkSystem!" );
socket.Dispose();
return;
}
sockets.Add( socket );
socket.OnClientConnect = OnConnected;
socket.OnClientDisconnect = OnDisconnected;
socket.OnHostChanged = OnHostChanged;
socket.Initialize( this );
}
void CloseSockets()
{
Log.Trace( $"NetworkSystem.CloseSockets" );
foreach ( var socket in sockets )
{
if ( !socket.AutoDispose )
continue;
socket?.Dispose();
}
sockets.Clear();
}
void OnHostChanged( (Connection previous, Connection current) state )
{
if ( state.previous is not null )
{
Log.Info( $"The network host has changed from {state.previous} to {state.current}" );
}
var wasHost = IsHost;
IsHost = state.current == Connection.Local;
if ( IsHost && !wasHost )
{
GameSystem?.OnBecameHost( state.previous );
}
GameSystem?.OnHostChanged( state.previous, state.current );
}
}