refactor
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using RebootKit.Engine.Simulation;
|
||||
using Unity.Collections;
|
||||
using Unity.Netcode;
|
||||
@@ -8,14 +11,31 @@ using UnityEngine;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine.Main {
|
||||
class NetworkClientState {
|
||||
enum NetworkClientSyncState {
|
||||
NotReady,
|
||||
LoadingWorld,
|
||||
PreparingForActorsSync,
|
||||
SyncingActors,
|
||||
Ready
|
||||
}
|
||||
|
||||
struct NetworkClientState : INetworkSerializable {
|
||||
public ulong ClientID;
|
||||
public bool IsWorldLoaded;
|
||||
public bool AreActorsSynced;
|
||||
public bool IsReadyForActorsSync;
|
||||
public NetworkClientSyncState SyncState;
|
||||
public int ActorsSyncPacketsLeft;
|
||||
|
||||
public bool IsReady;
|
||||
public bool IsReady {
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get {
|
||||
return SyncState == NetworkClientSyncState.Ready;
|
||||
}
|
||||
}
|
||||
|
||||
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
|
||||
serializer.SerializeValue(ref ClientID);
|
||||
serializer.SerializeValue(ref SyncState);
|
||||
serializer.SerializeValue(ref ActorsSyncPacketsLeft);
|
||||
}
|
||||
}
|
||||
|
||||
public class NetworkSystem : NetworkBehaviour {
|
||||
@@ -25,13 +45,26 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
internal readonly Dictionary<ulong, NetworkClientState> Clients = new Dictionary<ulong, NetworkClientState>();
|
||||
|
||||
FixedString512Bytes m_WorldID = new FixedString512Bytes("");
|
||||
public FixedString512Bytes WorldID { get; private set; } = new FixedString512Bytes("");
|
||||
bool m_IsChangingWorld = false;
|
||||
|
||||
public ulong LocalClientID {
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get {
|
||||
return NetworkManager.Singleton.LocalClientId;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// @MARK: Unity callbacks
|
||||
//
|
||||
void Awake() {
|
||||
RR.NetworkSystemInstance = this;
|
||||
}
|
||||
|
||||
//
|
||||
// @MARK: NetworkBehaviour callbacks
|
||||
//
|
||||
public override void OnNetworkSpawn() {
|
||||
base.OnNetworkSpawn();
|
||||
|
||||
@@ -55,35 +88,42 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
NetworkClientState newClientState = new NetworkClientState {
|
||||
ClientID = clientID,
|
||||
IsWorldLoaded = false,
|
||||
AreActorsSynced = false,
|
||||
IsReadyForActorsSync = false,
|
||||
IsReady = false
|
||||
SyncState = NetworkClientSyncState.NotReady
|
||||
};
|
||||
Clients.Add(clientID, newClientState);
|
||||
|
||||
if (!m_WorldID.IsEmpty) {
|
||||
s_Logger.Info($"Synchronizing world load for client {clientID} with world ID '{m_WorldID}'");
|
||||
ClientLoadWorldRpc(m_WorldID.ToString(), RpcTarget.Single(clientID, RpcTargetUse.Temp));
|
||||
if (clientID != NetworkManager.Singleton.LocalClientId) {
|
||||
foreach (NetworkClientState state in Clients.Values) {
|
||||
UpdateClientStateRpc(state, RpcTarget.Single(clientID, RpcTargetUse.Temp));
|
||||
}
|
||||
}
|
||||
|
||||
if (!WorldID.IsEmpty) {
|
||||
s_Logger.Info($"Synchronizing world load for client {clientID} with world ID '{WorldID}'");
|
||||
ClientLoadWorldRpc(WorldID.ToString(), RpcTarget.Single(clientID, RpcTargetUse.Temp));
|
||||
}
|
||||
}
|
||||
|
||||
void OnClientDisconnect(ulong clientID) {
|
||||
if (!IsServer) {
|
||||
return;
|
||||
}
|
||||
|
||||
s_Logger.Info($"OnClientDisconnect: {clientID}");
|
||||
Clients.Remove(clientID);
|
||||
}
|
||||
|
||||
internal NetworkClientState GetClientState(ulong clientID) {
|
||||
if (Clients.TryGetValue(clientID, out NetworkClientState clientState)) {
|
||||
return clientState;
|
||||
|
||||
//
|
||||
// @MARK: Server API
|
||||
//
|
||||
public void KickClient(ulong clientID, string reason = "Kicked by server") {
|
||||
if (!IsServer) {
|
||||
s_Logger.Error("Only server can kick clients.");
|
||||
return;
|
||||
}
|
||||
|
||||
s_Logger.Error($"Client state for {clientID} not found.");
|
||||
return null;
|
||||
if (NetworkManager.Singleton.ConnectedClients.TryGetValue(clientID, out NetworkClient client)) {
|
||||
NetworkManager.Singleton.DisconnectClient(clientID, reason);
|
||||
s_Logger.Info($"Kicked client {clientID}: {reason}");
|
||||
} else {
|
||||
s_Logger.Error($"Client {clientID} not found.");
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCurrentWorld(string worldID) {
|
||||
@@ -93,7 +133,7 @@ namespace RebootKit.Engine.Main {
|
||||
}
|
||||
|
||||
if (m_IsChangingWorld) {
|
||||
s_Logger.Error($"Already changing world to '{m_WorldID}'. Please wait until the current world change is complete.");
|
||||
s_Logger.Error($"Already changing world to '{WorldID}'. Please wait until the current world change is complete.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -103,13 +143,12 @@ namespace RebootKit.Engine.Main {
|
||||
return;
|
||||
}
|
||||
|
||||
m_WorldID = worldID;
|
||||
|
||||
foreach (KeyValuePair<ulong, NetworkClientState> kv in Clients) {
|
||||
kv.Value.IsWorldLoaded = false;
|
||||
kv.Value.AreActorsSynced = false;
|
||||
kv.Value.IsReadyForActorsSync = false;
|
||||
kv.Value.IsReady = false;
|
||||
WorldID = worldID;
|
||||
|
||||
foreach ((ulong _, NetworkClientState clientState) in Clients.ToList()) {
|
||||
NetworkClientState state = clientState;
|
||||
state.SyncState = NetworkClientSyncState.LoadingWorld;
|
||||
UpdateClientState(state);
|
||||
}
|
||||
|
||||
ServerLoadWorldAsync(worldConfigAsset, destroyCancellationToken).Forget();
|
||||
@@ -127,8 +166,14 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
m_IsChangingWorld = false;
|
||||
|
||||
NetworkClientState localClientState = GetClientState(NetworkManager.Singleton.LocalClientId);
|
||||
localClientState.IsReady = true;
|
||||
if (!TryGetClientState(NetworkManager.Singleton.LocalClientId, out NetworkClientState localClientState)) {
|
||||
s_Logger.Error($"Local client state not found for client ID {NetworkManager.Singleton.LocalClientId}.");
|
||||
RR.Disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
localClientState.SyncState = NetworkClientSyncState.Ready;
|
||||
UpdateClientState(localClientState);
|
||||
|
||||
RR.GameInstance.PlayerBecameReady(localClientState.ClientID);
|
||||
|
||||
@@ -161,7 +206,7 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
await RR.World.LoadAsync(worldConfigAsset.Config, cancellationToken);
|
||||
|
||||
m_WorldID = worldID;
|
||||
WorldID = worldID;
|
||||
ClientLoadedWorldRpc(worldID);
|
||||
}
|
||||
|
||||
@@ -169,30 +214,60 @@ namespace RebootKit.Engine.Main {
|
||||
void ClientLoadedWorldRpc(string worldID, RpcParams rpcParams = default) {
|
||||
ulong clientID = rpcParams.Receive.SenderClientId;
|
||||
|
||||
if (!m_WorldID.Equals(worldID)) {
|
||||
s_Logger.Error($"Client {clientID} tried to load world '{worldID}', but server is in world '{m_WorldID}'.");
|
||||
if (!WorldID.Equals(worldID)) {
|
||||
s_Logger.Error($"Client {clientID} tried to load world '{worldID}', but server is in world '{WorldID}'.");
|
||||
NetworkManager.Singleton.DisconnectClient(clientID, "World mismatch!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Clients.TryGetValue(clientID, out NetworkClientState clientState)) {
|
||||
clientState.IsWorldLoaded = true;
|
||||
clientState.IsReadyForActorsSync = false;
|
||||
Actors.SynchronizeActorsForClient(clientID);
|
||||
Actors.InitializeActorsForClient(clientID);
|
||||
} else {
|
||||
NetworkManager.Singleton.DisconnectClient(clientID, "Client is not registered!");
|
||||
}
|
||||
}
|
||||
|
||||
internal void ClientSynchronizedActors(ulong clientID) {
|
||||
NetworkClientState clientState = GetClientState(clientID);
|
||||
if (clientState is null) {
|
||||
s_Logger.Error($"Client state for {clientID} not found.");
|
||||
|
||||
//
|
||||
// @MARK: Internal
|
||||
//
|
||||
internal bool TryGetClientState(ulong clientID, out NetworkClientState clientState) {
|
||||
return Clients.TryGetValue(clientID, out clientState);
|
||||
}
|
||||
|
||||
internal void UpdateClientState(NetworkClientState clientState) {
|
||||
if (!IsServer) {
|
||||
s_Logger.Error("UpdateClientState can only be called on the server.");
|
||||
return;
|
||||
}
|
||||
|
||||
Clients[clientState.ClientID] = clientState;
|
||||
UpdateClientStateRpc(clientState, RpcTarget.NotServer);
|
||||
}
|
||||
|
||||
[Rpc(SendTo.SpecifiedInParams, Delivery = RpcDelivery.Reliable)]
|
||||
void UpdateClientStateRpc(NetworkClientState newState, RpcParams rpcParams) {
|
||||
Clients[newState.ClientID] = newState;
|
||||
}
|
||||
|
||||
clientState.IsReady = true;
|
||||
RR.GameInstance.PlayerBecameReady(clientID);
|
||||
internal void ClientSynchronizedActors(ulong clientID) {
|
||||
if (TryGetClientState(clientID, out NetworkClientState state)) {
|
||||
state.SyncState = NetworkClientSyncState.Ready;
|
||||
UpdateClientState(state);
|
||||
|
||||
RR.GameInstance.PlayerBecameReady(clientID);
|
||||
} else {
|
||||
s_Logger.Error($"Client state for {clientID} not found.");
|
||||
}
|
||||
}
|
||||
|
||||
internal int GetReadyClientsCount() {
|
||||
int count = 0;
|
||||
foreach (NetworkClientState clientState in Clients.Values) {
|
||||
if (clientState.IsReady) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user