Implement most of the net var networking functionality
This commit is contained in:
@@ -7,7 +7,7 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace Barotrauma.LuaCs;
|
namespace Barotrauma.LuaCs;
|
||||||
|
|
||||||
public partial class NetworkingService : INetworkingService, IEventConnectedToServer, IEventServerRawNetMessageReceived
|
partial class NetworkingService : INetworkingService, IEventConnectedToServer, IEventServerRawNetMessageReceived
|
||||||
{
|
{
|
||||||
private ConcurrentDictionary<ushort, ConcurrentQueue<IReadMessage>> receiveQueue = new();
|
private ConcurrentDictionary<ushort, ConcurrentQueue<IReadMessage>> receiveQueue = new();
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ using Barotrauma.LuaCs.Events;
|
|||||||
using Barotrauma.Networking;
|
using Barotrauma.Networking;
|
||||||
using FluentResults;
|
using FluentResults;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Barotrauma.LuaCs;
|
namespace Barotrauma.LuaCs;
|
||||||
|
|
||||||
public partial class NetworkingService : INetworkingService
|
internal partial class NetworkingService : INetworkingService
|
||||||
{
|
{
|
||||||
public readonly record struct NetId
|
public readonly record struct NetId
|
||||||
{
|
{
|
||||||
@@ -47,9 +48,12 @@ public partial class NetworkingService : INetworkingService
|
|||||||
ReceiveNetIds
|
ReceiveNetIds
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<NetId, NetMessageReceived> netReceives = new Dictionary<NetId, NetMessageReceived>();
|
|
||||||
private Dictionary<ushort, NetId> packetToId = new Dictionary<ushort, NetId>();
|
private ConcurrentDictionary<INetworkSyncVar, NetId> netVars = [];
|
||||||
private Dictionary<NetId, ushort> idToPacket = new Dictionary<NetId, ushort>();
|
|
||||||
|
private ConcurrentDictionary<NetId, NetMessageReceived> netReceives = [];
|
||||||
|
private ConcurrentDictionary<ushort, NetId> packetToId = [];
|
||||||
|
private ConcurrentDictionary<NetId, ushort> idToPacket = [];
|
||||||
|
|
||||||
public bool IsActive
|
public bool IsActive
|
||||||
{
|
{
|
||||||
@@ -64,10 +68,12 @@ public partial class NetworkingService : INetworkingService
|
|||||||
|
|
||||||
private readonly IEventService _eventService;
|
private readonly IEventService _eventService;
|
||||||
private readonly ILoggerService _loggerService;
|
private readonly ILoggerService _loggerService;
|
||||||
|
private readonly INetworkIdProvider _networkIdProvider;
|
||||||
|
|
||||||
public NetworkingService(IEventService eventService, ILoggerService loggerService)
|
internal NetworkingService(IEventService eventService, INetworkIdProvider networkIdProvider, ILoggerService loggerService)
|
||||||
{
|
{
|
||||||
_eventService = eventService;
|
_eventService = eventService;
|
||||||
|
_networkIdProvider = networkIdProvider;
|
||||||
_loggerService = loggerService;
|
_loggerService = loggerService;
|
||||||
|
|
||||||
#if SERVER
|
#if SERVER
|
||||||
@@ -82,7 +88,7 @@ public partial class NetworkingService : INetworkingService
|
|||||||
public IWriteMessage Start(string netIdString) => Start(new NetId(netIdString));
|
public IWriteMessage Start(string netIdString) => Start(new NetId(netIdString));
|
||||||
public IWriteMessage Start(Guid netIdGuid) => Start(new NetId(netIdGuid.ToString()));
|
public IWriteMessage Start(Guid netIdGuid) => Start(new NetId(netIdGuid.ToString()));
|
||||||
|
|
||||||
public void Receive(NetId netId, NetMessageReceived callback)
|
internal void Receive(NetId netId, NetMessageReceived callback)
|
||||||
{
|
{
|
||||||
#if SERVER
|
#if SERVER
|
||||||
RegisterId(netId);
|
RegisterId(netId);
|
||||||
@@ -101,7 +107,7 @@ public partial class NetworkingService : INetworkingService
|
|||||||
#if CLIENT
|
#if CLIENT
|
||||||
netReceives[netId](netMessage);
|
netReceives[netId](netMessage);
|
||||||
#elif SERVER
|
#elif SERVER
|
||||||
netReceives[netId](netMessage, client.Connection);
|
netReceives[netId](netMessage, client);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -141,25 +147,78 @@ public partial class NetworkingService : INetworkingService
|
|||||||
|
|
||||||
public Guid GetNetworkIdForInstance(INetworkSyncVar var)
|
public Guid GetNetworkIdForInstance(INetworkSyncVar var)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
return _networkIdProvider.GetNetworkIdForInstance(var);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterNetVar(INetworkSyncVar netVar)
|
public void RegisterNetVar(INetworkSyncVar netVar)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
netVar.SetNetworkOwner(this);
|
||||||
|
|
||||||
|
NetId netId = new NetId(netVar.InstanceId.ToString());
|
||||||
|
netVars[netVar] = netId;
|
||||||
|
|
||||||
|
#if CLIENT
|
||||||
|
Receive(netId, (IReadMessage message) =>
|
||||||
|
{
|
||||||
|
if (netVar.SyncType == NetSync.None)
|
||||||
|
{
|
||||||
|
_loggerService.LogWarning($"Received net var from server but {nameof(NetSync)} is {netVar.SyncType.ToString()}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
netVar.ReadNetMessage(message);
|
||||||
|
});
|
||||||
|
#elif SERVER
|
||||||
|
Receive(netId, (IReadMessage message, Client client) =>
|
||||||
|
{
|
||||||
|
if (netVar.SyncType == NetSync.None || netVar.SyncType == NetSync.ServerAuthority)
|
||||||
|
{
|
||||||
|
_loggerService.LogWarning($"Received net var from {GameServer.ClientLogName(client)} but {nameof(NetSync)} is {netVar.SyncType.ToString()}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!client.HasPermission(netVar.WritePermissions))
|
||||||
|
{
|
||||||
|
_loggerService.LogWarning($"Received net var from {GameServer.ClientLogName(client)} but the client lacks permissions to modify it");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
netVar.ReadNetMessage(message);
|
||||||
|
});
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendNetVar(INetworkSyncVar netVar)
|
public void SendNetVar(INetworkSyncVar netVar)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
if (!netVars.TryGetValue(netVar, out NetId netId))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Tried to send net var across network without registering first");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (netVar.SyncType == NetSync.None) { return; }
|
||||||
|
#if CLIENT
|
||||||
|
if (netVar.SyncType == NetSync.ServerAuthority) { return; }
|
||||||
|
#elif SERVER
|
||||||
|
if (netVar.SyncType == NetSync.ClientOneWay) { return; }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
IWriteMessage message = Start(netId);
|
||||||
|
netVar.WriteNetMessage(message);
|
||||||
|
#if CLIENT
|
||||||
|
SendToServer(message);
|
||||||
|
#elif SERVER
|
||||||
|
SendToClient(message);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public FluentResults.Result Reset()
|
public FluentResults.Result Reset()
|
||||||
{
|
{
|
||||||
IsSynchronized = false;
|
IsSynchronized = false;
|
||||||
netReceives = new Dictionary<NetId, NetMessageReceived>();
|
netReceives = new ConcurrentDictionary<NetId, NetMessageReceived>();
|
||||||
packetToId = new Dictionary<ushort, NetId>();
|
packetToId = new ConcurrentDictionary<ushort, NetId>();
|
||||||
idToPacket = new Dictionary<NetId, ushort>();
|
idToPacket = new ConcurrentDictionary<NetId, ushort>();
|
||||||
|
netVars = new ConcurrentDictionary<INetworkSyncVar, NetId>();
|
||||||
|
|
||||||
SubscribeToEvents();
|
SubscribeToEvents();
|
||||||
return FluentResults.Result.Ok();
|
return FluentResults.Result.Ok();
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -9,10 +9,10 @@ namespace Barotrauma.LuaCs;
|
|||||||
#if CLIENT
|
#if CLIENT
|
||||||
public delegate void NetMessageReceived(IReadMessage netMessage);
|
public delegate void NetMessageReceived(IReadMessage netMessage);
|
||||||
#elif SERVER
|
#elif SERVER
|
||||||
public delegate void NetMessageReceived(IReadMessage netMessage, NetworkConnection connection);
|
internal delegate void NetMessageReceived(IReadMessage netMessage, Client connection);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public interface INetworkingService : IReusableService, ILuaCsNetworking, IEntityNetworkingService
|
internal interface INetworkingService : IReusableService, ILuaCsNetworking, IEntityNetworkingService
|
||||||
{
|
{
|
||||||
bool IsActive { get; }
|
bool IsActive { get; }
|
||||||
bool IsSynchronized { get; }
|
bool IsSynchronized { get; }
|
||||||
|
|||||||
Reference in New Issue
Block a user