Skip to content

Commit

Permalink
Feature packet log (SubnauticaNitrox#1038)
Browse files Browse the repository at this point in the history
* Created the network debugger for incomming and outgoing packets

* Sorted count of packets

* Cleaned up code
  • Loading branch information
EtienneDx authored Apr 18, 2020
1 parent 7457b22 commit b73d55d
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 36 deletions.
40 changes: 24 additions & 16 deletions NitroxClient/ClientAutoFacRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NitroxClient.Communication.MultiplayerSession;
using NitroxClient.Communication.NetworkingLayer.LiteNetLib;
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.Debuggers;
using NitroxClient.GameLogic;
using NitroxClient.GameLogic.Bases;
using NitroxClient.GameLogic.ChatUI;
Expand All @@ -24,6 +25,7 @@ namespace NitroxClient
{
public class ClientAutoFacRegistrar : IAutoFacRegistrar
{
private static readonly Assembly currentAssembly = Assembly.GetExecutingAssembly();
private readonly IModule[] modules;

public ClientAutoFacRegistrar(params IModule[] modules)
Expand All @@ -46,31 +48,37 @@ public void RegisterDependencies(ContainerBuilder containerBuilder)

private static void RegisterCoreDependencies(ContainerBuilder containerBuilder)
{
containerBuilder.Register(c => new NitroxProtobufSerializer("NitroxModel.dll"));
containerBuilder.RegisterAssemblyTypes(currentAssembly)
.AssignableTo<BaseDebugger>()
.As<BaseDebugger>()
.AsSelf()
.SingleInstance();

containerBuilder.Register(c => new NitroxProtobufSerializer($"{nameof(NitroxModel)}.dll"));

containerBuilder.RegisterType<UnityPreferenceStateProvider>()
.As<IPreferenceStateProvider>()
.SingleInstance();
.As<IPreferenceStateProvider>()
.SingleInstance();

containerBuilder.RegisterType<PlayerPreferenceManager>().SingleInstance();

containerBuilder.RegisterType<MultiplayerSessionManager>()
.As<IMultiplayerSession>()
.As<IPacketSender>()
.InstancePerLifetimeScope();
.As<IMultiplayerSession>()
.As<IPacketSender>()
.InstancePerLifetimeScope();

containerBuilder.RegisterType<LiteNetLibClient>()
.As<IClient>()
.InstancePerLifetimeScope();
.As<IClient>()
.InstancePerLifetimeScope();

containerBuilder.RegisterType<LocalPlayer>()
.AsSelf() //Would like to deprecate this registration at some point and just work through an abstraction.
.As<ILocalNitroxPlayer>()
.InstancePerLifetimeScope();
.AsSelf() //Would like to deprecate this registration at some point and just work through an abstraction.
.As<ILocalNitroxPlayer>()
.InstancePerLifetimeScope();

containerBuilder.RegisterType<SubnauticaRotationMetadataFactory>()
.As<RotationMetadataFactory>()
.InstancePerLifetimeScope();
.As<RotationMetadataFactory>()
.InstancePerLifetimeScope();

containerBuilder.RegisterType<PlayerManager>().InstancePerLifetimeScope();
containerBuilder.RegisterType<PlayerModelManager>().InstancePerLifetimeScope();
Expand Down Expand Up @@ -109,15 +117,15 @@ private static void RegisterCoreDependencies(ContainerBuilder containerBuilder)
private void RegisterPacketProcessors(ContainerBuilder containerBuilder)
{
containerBuilder
.RegisterAssemblyTypes(Assembly.GetAssembly(GetType()))
.RegisterAssemblyTypes(currentAssembly)
.AsClosedTypesOf(typeof(ClientPacketProcessor<>))
.InstancePerLifetimeScope();
}

private void RegisterColorSwapManagers(ContainerBuilder containerBuilder)
{
containerBuilder
.RegisterAssemblyTypes(Assembly.GetAssembly(GetType()))
.RegisterAssemblyTypes(currentAssembly)
.AssignableTo<IColorSwapManager>()
.As<IColorSwapManager>()
.InstancePerLifetimeScope();
Expand All @@ -126,7 +134,7 @@ private void RegisterColorSwapManagers(ContainerBuilder containerBuilder)
private void RegisterInitialSyncProcessors(ContainerBuilder containerBuilder)
{
containerBuilder
.RegisterAssemblyTypes(Assembly.GetAssembly(GetType()))
.RegisterAssemblyTypes(currentAssembly)
.AssignableTo<InitialSyncProcessor>()
.As<InitialSyncProcessor>()
.InstancePerLifetimeScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using NitroxClient.Communication.Abstract;
using NitroxClient.Communication.MultiplayerSession.ConnectionState;
using NitroxClient.Debuggers;
using NitroxClient.GameLogic;
using NitroxModel;
using NitroxModel.Helper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LiteNetLib;
using LiteNetLib.Utils;
using NitroxClient.Communication.Abstract;
using NitroxClient.Debuggers;
using NitroxClient.MonoBehaviours.Gui.InGame;
using NitroxModel.Core;
using NitroxModel.Logger;
Expand All @@ -15,14 +16,16 @@ public class LiteNetLibClient : IClient
public bool IsConnected { get; private set; }

private readonly NetPacketProcessor netPacketProcessor = new NetPacketProcessor();
private AutoResetEvent connectedEvent = new AutoResetEvent(false);
private readonly AutoResetEvent connectedEvent = new AutoResetEvent(false);
private readonly PacketReceiver packetReceiver;
private readonly NetworkDebugger networkDebugger;

private NetManager client;

public LiteNetLibClient()
public LiteNetLibClient(PacketReceiver packetReceiver, NetworkDebugger networkDebugger)
{
packetReceiver = NitroxServiceLocator.LocateService<PacketReceiver>();
this.packetReceiver = packetReceiver;
this.networkDebugger = networkDebugger;
}

public void Start(string ipAddress, int serverPort)
Expand Down Expand Up @@ -50,6 +53,7 @@ public void Start(string ipAddress, int serverPort)

public void Send(Packet packet)
{
networkDebugger?.PacketSent(packet);
client.SendToAll(netPacketProcessor.Write(packet.ToWrapperPacket()), NitroxDeliveryMethod.ToLiteNetLib(packet.DeliveryMethod));
client.Flush();
}
Expand Down
6 changes: 5 additions & 1 deletion NitroxClient/Communication/PacketReceiver.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System.Collections.Generic;
using NitroxClient.Debuggers;
using NitroxModel.Packets;

namespace NitroxClient.Communication
{
// TODO: Spinlocks don't seem to be necessary here, but I don't know for certain.
public class PacketReceiver
{
private readonly NetworkDebugger networkDebugger;
private readonly Queue<Packet> receivedPackets;

public PacketReceiver()
public PacketReceiver(NetworkDebugger networkDebugger = null)
{
receivedPackets = new Queue<Packet>();
this.networkDebugger = networkDebugger;
}

public void PacketReceived(Packet packet)
{
lock (receivedPackets)
{
networkDebugger?.PacketReceived(packet);
receivedPackets.Enqueue(packet);
}
}
Expand Down
Loading

0 comments on commit b73d55d

Please sign in to comment.