Skip to content

Commit

Permalink
Upgraded Litenetlib
Browse files Browse the repository at this point in the history
  • Loading branch information
dartasen committed Feb 26, 2024
1 parent ffe3352 commit cd7a75e
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion NitroxModel/NitroxModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.4" />
<PackageReference Include="LiteNetLib" Version="1.0.1.1" />
<PackageReference Include="LiteNetLib" Version="1.2.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Mono.Nat" Version="3.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
Expand Down
8 changes: 4 additions & 4 deletions NitroxServer/Communication/LiteNetLib/LiteNetLibConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace NitroxServer.Communication.LiteNetLib;

public class LiteNetLibConnection : NitroxConnection, IEquatable<LiteNetLibConnection>
public class LiteNetLibConnection : INitroxConnection, IEquatable<LiteNetLibConnection>
{
private readonly NetDataWriter dataWriter = new();
private readonly NetPeer peer;

public IPEndPoint Endpoint => peer.EndPoint;
public IPEndPoint Endpoint => peer;
public NitroxConnectionState State => peer.ConnectionState.ToNitrox();

public LiteNetLibConnection(NetPeer peer)
Expand All @@ -33,7 +33,7 @@ public void SendPacket(Packet packet)
}
else
{
Log.Warn($"Cannot send packet {packet?.GetType()} to a closed connection {peer.EndPoint}");
Log.Warn($"Cannot send packet {packet?.GetType()} to a closed connection {peer as IPEndPoint}");
}
}

Expand All @@ -49,7 +49,7 @@ public void SendPacket(Packet packet)

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void NetworkDataReceived(NetPeer peer, NetDataReader reader, byte channe
{
reader.GetBytes(packetData, packetDataLength);
Packet packet = Packet.Deserialize(packetData);
NitroxConnection connection = GetConnection(peer.Id);
INitroxConnection connection = GetConnection(peer.Id);
ProcessIncomingData(connection, packet);
}
finally
Expand All @@ -140,9 +140,9 @@ private void NetworkDataReceived(NetPeer peer, NetDataReader reader, byte channe
}
}

private NitroxConnection GetConnection(int remoteIdentifier)
private INitroxConnection GetConnection(int remoteIdentifier)
{
NitroxConnection connection;
INitroxConnection connection;
lock (connectionsByRemoteIdentifier)
{
connectionsByRemoteIdentifier.TryGetValue(remoteIdentifier, out connection);
Expand Down
17 changes: 9 additions & 8 deletions NitroxServer/Communication/NitroxConnection.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Net;
using System.Net;
using NitroxModel.Packets;
using NitroxModel.Packets.Processors.Abstract;

namespace NitroxServer.Communication
namespace NitroxServer.Communication;

public interface INitroxConnection : IProcessorContext
{
public interface NitroxConnection : IProcessorContext
{
IPEndPoint Endpoint { get; }
NitroxConnectionState State { get; }
void SendPacket(Packet packet);
}
IPEndPoint Endpoint { get; }

NitroxConnectionState State { get; }

void SendPacket(Packet packet);
}
6 changes: 3 additions & 3 deletions NitroxServer/Communication/NitroxServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static NitroxServer()

protected readonly PacketHandler packetHandler;
protected readonly EntitySimulation entitySimulation;
protected readonly Dictionary<int, NitroxConnection> connectionsByRemoteIdentifier = new();
protected readonly Dictionary<int, INitroxConnection> connectionsByRemoteIdentifier = new();
protected readonly PlayerManager playerManager;

public NitroxServer(PacketHandler packetHandler, PlayerManager playerManager, EntitySimulation entitySimulation, ServerConfig serverConfig)
Expand All @@ -42,7 +42,7 @@ public NitroxServer(PacketHandler packetHandler, PlayerManager playerManager, En

public abstract void Stop();

protected void ClientDisconnected(NitroxConnection connection)
protected void ClientDisconnected(INitroxConnection connection)
{
Player player = playerManager.GetPlayer(connection);

Expand All @@ -67,7 +67,7 @@ protected void ClientDisconnected(NitroxConnection connection)
}
}

protected void ProcessIncomingData(NitroxConnection connection, Packet packet)
protected void ProcessIncomingData(INitroxConnection connection, Packet packet)
{
try
{
Expand Down
4 changes: 2 additions & 2 deletions NitroxServer/Communication/Packets/PacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public PacketHandler(PlayerManager playerManager, DefaultServerPacketProcessor p
defaultServerPacketProcessor = packetProcessor;
}

public void Process(Packet packet, NitroxConnection connection)
public void Process(Packet packet, INitroxConnection connection)
{
Player player = playerManager.GetPlayer(connection);
if (player == null)
Expand Down Expand Up @@ -61,7 +61,7 @@ private void ProcessAuthenticated(Packet packet, Player player)
}
}

private void ProcessUnauthenticated(Packet packet, NitroxConnection connection)
private void ProcessUnauthenticated(Packet packet, INitroxConnection connection)
{
Type packetType = packet.GetType();
if (!packetProcessorUnauthCache.TryGetValue(packetType, out PacketProcessor processor))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public abstract class UnauthenticatedPacketProcessor<T> : PacketProcessor where
{
public override void ProcessPacket(Packet packet, IProcessorContext connection)
{
Process((T)packet, (NitroxConnection)connection);
Process((T)packet, (INitroxConnection)connection);
}

public abstract void Process(T packet, NitroxConnection connection);
public abstract void Process(T packet, INitroxConnection connection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MultiplayerSessionPolicyRequestProcessor(ServerConfig config)
}

// This will extend in the future when we look into different options for auth
public override void Process(MultiplayerSessionPolicyRequest packet, NitroxConnection connection)
public override void Process(MultiplayerSessionPolicyRequest packet, INitroxConnection connection)
{
Log.Info("Providing session policies...");
connection.SendPacket(new MultiplayerSessionPolicy(packet.CorrelationId, config.DisableConsole, config.MaxConnections, config.IsPasswordRequired));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MultiplayerSessionReservationRequestProcessor(PlayerManager playerManager
this.playerManager = playerManager;
}

public override void Process(MultiplayerSessionReservationRequest packet, NitroxConnection connection)
public override void Process(MultiplayerSessionReservationRequest packet, INitroxConnection connection)
{
Log.Info($"Processing reservation request from {packet.AuthenticationContext.Username}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PlayerJoiningMultiplayerSessionProcessor(ScheduleKeeper scheduleKeeper, S
this.buildingManager = buildingManager;
}

public override void Process(PlayerJoiningMultiplayerSession packet, NitroxConnection connection)
public override void Process(PlayerJoiningMultiplayerSession packet, INitroxConnection connection)
{
Player player = playerManager.PlayerConnected(connection, packet.ReservationKey, out bool wasBrandNewPlayer);
NitroxId assignedEscapePodId = world.EscapePodManager.AssignPlayerToEscapePod(player.Id, out Optional<EscapePodWorldEntity> newlyCreatedEscapePod);
Expand Down
4 changes: 2 additions & 2 deletions NitroxServer/GameLogic/InitialSyncTimerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NitroxServer.GameLogic
/// </summary>
internal class InitialSyncTimerData
{
public readonly NitroxConnection Connection;
public readonly INitroxConnection Connection;
public readonly AuthenticationContext Context;
public readonly int MaxCounter;

Expand All @@ -25,7 +25,7 @@ internal class InitialSyncTimerData
/// </summary>
public bool Disposing = false;

public InitialSyncTimerData(NitroxConnection connection, AuthenticationContext context, int initialSyncTimeout)
public InitialSyncTimerData(INitroxConnection connection, AuthenticationContext context, int initialSyncTimeout)
{
Connection = connection;
Context = context;
Expand Down
20 changes: 10 additions & 10 deletions NitroxServer/GameLogic/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace NitroxServer.GameLogic
public class PlayerManager
{
private readonly ThreadSafeDictionary<string, Player> allPlayersByName;
private readonly ThreadSafeDictionary<NitroxConnection, ConnectionAssets> assetsByConnection = new();
private readonly ThreadSafeDictionary<INitroxConnection, ConnectionAssets> assetsByConnection = new();
private readonly ThreadSafeDictionary<string, PlayerContext> reservations = new();
private readonly ThreadSafeSet<string> reservedPlayerNames = new("Player"); // "Player" is often used to identify the local player and should not be used by any user

private ThreadSafeQueue<KeyValuePair<NitroxConnection, MultiplayerSessionReservationRequest>> JoinQueue { get; set; } = new();
private ThreadSafeQueue<KeyValuePair<INitroxConnection, MultiplayerSessionReservationRequest>> JoinQueue { get; set; } = new();
private bool PlayerCurrentlyJoining { get; set; }

private Timer initialSyncTimer;
Expand Down Expand Up @@ -56,7 +56,7 @@ public IEnumerable<Player> GetAllPlayers()
}

public MultiplayerSessionReservation ReservePlayerContext(
NitroxConnection connection,
INitroxConnection connection,
PlayerSettings playerSettings,
AuthenticationContext authenticationContext,
string correlationId)
Expand Down Expand Up @@ -88,7 +88,7 @@ public MultiplayerSessionReservation ReservePlayerContext(
return new MultiplayerSessionReservation(correlationId, MultiplayerSessionReservationState.REJECTED);
}

JoinQueue.Enqueue(new KeyValuePair<NitroxConnection, MultiplayerSessionReservationRequest>(
JoinQueue.Enqueue(new KeyValuePair<INitroxConnection, MultiplayerSessionReservationRequest>(
connection,
new MultiplayerSessionReservationRequest(correlationId, playerSettings, authenticationContext)));

Expand Down Expand Up @@ -175,13 +175,13 @@ private void InitialSyncTimerElapsed(object state)
}
}

public void NonPlayerDisconnected(NitroxConnection connection)
public void NonPlayerDisconnected(INitroxConnection connection)
{
// Remove any requests sent by the connection from the join queue
JoinQueue = new(JoinQueue.Where(pair => !Equals(pair.Key, connection)));
}

public Player PlayerConnected(NitroxConnection connection, string reservationKey, out bool wasBrandNewPlayer)
public Player PlayerConnected(INitroxConnection connection, string reservationKey, out bool wasBrandNewPlayer)
{
PlayerContext playerContext = reservations[reservationKey];
Validate.NotNull(playerContext);
Expand Down Expand Up @@ -229,7 +229,7 @@ public Player PlayerConnected(NitroxConnection connection, string reservationKey
return player;
}

public void PlayerDisconnected(NitroxConnection connection)
public void PlayerDisconnected(INitroxConnection connection)
{
assetsByConnection.TryGetValue(connection, out ConnectionAssets assetPackage);
if (assetPackage == null)
Expand Down Expand Up @@ -273,8 +273,8 @@ public void FinishProcessingReservation(Player player = null)
// Tell next client that it can start joining.
if (JoinQueue.Count > 0)
{
KeyValuePair<NitroxConnection, MultiplayerSessionReservationRequest> keyValuePair = JoinQueue.Dequeue();
NitroxConnection requestConnection = keyValuePair.Key;
KeyValuePair<INitroxConnection, MultiplayerSessionReservationRequest> keyValuePair = JoinQueue.Dequeue();
INitroxConnection requestConnection = keyValuePair.Key;
MultiplayerSessionReservationRequest reservationRequest = keyValuePair.Value;

MultiplayerSessionReservation reservation = ReservePlayerContext(requestConnection,
Expand All @@ -301,7 +301,7 @@ public bool TryGetPlayerByName(string playerName, out Player foundPlayer)
return false;
}

public Player GetPlayer(NitroxConnection connection)
public Player GetPlayer(INitroxConnection connection)
{
if (!assetsByConnection.TryGetValue(connection, out ConnectionAssets assetPackage))
{
Expand Down
4 changes: 2 additions & 2 deletions NitroxServer/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Player : IProcessorContext
public ThreadSafeList<NitroxTechType> UsedItems { get; }
public Optional<NitroxId>[] QuickSlotsBindingIds { get; set; }

public NitroxConnection Connection { get; set; }
public INitroxConnection Connection { get; set; }
public PlayerSettings PlayerSettings => PlayerContext.PlayerSettings;
public PlayerContext PlayerContext { get; set; }
public ushort Id { get; }
Expand All @@ -42,7 +42,7 @@ public class Player : IProcessorContext

public PlayerWorldEntity Entity { get; set; }

public Player(ushort id, string name, bool isPermaDeath, PlayerContext playerContext, NitroxConnection connection,
public Player(ushort id, string name, bool isPermaDeath, PlayerContext playerContext, INitroxConnection connection,
NitroxVector3 position, NitroxQuaternion rotation, NitroxId playerId, Optional<NitroxId> subRootId, Perms perms, PlayerStatsData stats, NitroxGameMode gameMode,
IEnumerable<NitroxTechType> usedItems, Optional<NitroxId>[] quickSlotsBindingIds,
IEnumerable<EquippedItemData> equippedItems, IEnumerable<EquippedItemData> modules, IDictionary<string, float> personalCompletedGoalsWithTimestamp, IDictionary<string, PingInstancePreference> pingInstancePreferences, IList<int> pinnedRecipePreferences)
Expand Down

0 comments on commit cd7a75e

Please sign in to comment.