Skip to content

Commit

Permalink
Work on logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
DJGosnell committed Sep 15, 2023
1 parent 9098568 commit 287f623
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/NexNet.IntegrationTests/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ConsoleLogger : INexusLogger
private int _currentLineIndex = 0;
private int _totalLinesWritten = 0;

public string? Category { get; }
public string? Category { get; set; }

public bool LogEnabled { get; set; } = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task Server_PipeReaderReceivesDataMultipleTimesWithLargeData(Type t
var count = 0;
var largeData = new byte[1024 * 32];
// TODO: Review adding a test for increased iterations as this has been found to sometimes fail on CI.
const int iterations = 100000;
const int iterations = 1000;
sNexus.ServerTaskValueWithDuplexPipeEvent = async (nexus, pipe) =>
{
var result = await pipe.Input.ReadAsync().Timeout(1);
Expand All @@ -71,6 +71,8 @@ public async Task Server_PipeReaderReceivesDataMultipleTimesWithLargeData(Type t
}

await tcs.Task.Timeout(1);

Assert.Fail("Test is not complete");
}

[TestCase(Type.Uds)]
Expand Down
2 changes: 1 addition & 1 deletion src/NexNet/INexusLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface INexusLogger
/// <summary>
/// Category for the log event.
/// </summary>
public string? Category { get; }
public string? Category { get; set; }

/// <summary>
/// Level for the log event.
Expand Down
2 changes: 1 addition & 1 deletion src/NexNet/Internals/NexusSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public NexusSession(in NexusSessionConfigurations<TNexus, TProxy> configurations
? new ServerSessionContext<TProxy>(this, _sessionManager!)
: new ClientSessionContext<TProxy>(this);

Logger = configurations.Configs.Logger?.CreateLogger($"NexusSession [{Id}]");
Logger = configurations.Configs.Logger?.CreateLogger($"NexusSession:S{Id}");

PipeManager = _cacheManager.PipeManagerCache.Rent(this);
PipeManager.Setup(this);
Expand Down
32 changes: 27 additions & 5 deletions src/NexNet/Pipes/NexusDuplexPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ internal enum State : byte
/// <summary>
/// True if this pipe is the pipe used for initiating the pipe connection.
/// </summary>
internal bool InitiatingPipe;
internal readonly bool InitiatingPipe;

/// <summary>
/// Complete compiled ID containing the client bit and the server bit.
/// </summary>
public ushort Id { get; set; }
public ushort Id
{
get => _id;
set
{
if(Logger != null)
Logger.Category = $"NexusDuplexPipe:S{_session!.Id}:P{value}";

_id = value;
}
}

/// <summary>
/// Initial ID of this side of the connection.
Expand Down Expand Up @@ -115,15 +125,27 @@ internal enum State : byte
/// </summary>
protected INexusLogger? Logger;

private ushort _id;

NexusPipeWriter INexusDuplexPipe.WriterCore => _outputPipeWriter;
NexusPipeReader INexusDuplexPipe.ReaderCore => _inputPipeReader;

internal NexusDuplexPipe(byte localId, INexusSession session)
internal NexusDuplexPipe(ushort fullId, byte localId, INexusSession session)
{
_readyTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
_session = session;
Logger = session.Logger?.CreateLogger<NexusDuplexPipe>();

if(fullId == 0)
Logger = session.Logger?.CreateLogger($"NexusDuplexPipe:S{session.Id}:L{localId:000}");
else
{
Logger = session.Logger?.CreateLogger($"NexusDuplexPipe:S{session.Id}:P{fullId:00000}");
}
LocalId = localId;
_id = fullId;

if(fullId == 0)
InitiatingPipe = true;

_inputPipeReader = new NexusPipeReader(
this,
Expand Down Expand Up @@ -222,7 +244,7 @@ public bool UpdateState(State updatedState, bool remove = false)

var isServer = _session.IsServer;

Logger?.LogTrace($"{Id}: Current State: {currentState}; Update State: {updatedState};");
Logger?.LogTrace($"Current State: {currentState}; Update State: {updatedState};");

if (currentState == State.Unset)
{
Expand Down
12 changes: 5 additions & 7 deletions src/NexNet/Pipes/NexusPipeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Setup(INexusSession session)
_usedIds.SetAll(false);
_isCanceled = false;
_session = session;
_logger = session.Logger?.CreateLogger<NexusPipeManager>();
_logger = session.Logger?.CreateLogger($"NexusPipeManager:S{session.Id}");
}

public IRentedNexusDuplexPipe? RentPipe()
Expand All @@ -38,7 +38,6 @@ public void Setup(INexusSession session)
var partialId = GetPartialIdFromLocalId(localId);
var pipe = new RentedNexusDuplexPipe(localId, _session)
{
InitiatingPipe = true,
Manager = this
};

Expand Down Expand Up @@ -84,11 +83,7 @@ public async ValueTask<INexusDuplexPipe> RegisterPipe(byte otherId)

var id = GetCompleteId(otherId, out var localId);

var pipe = new NexusDuplexPipe(localId, _session)
{
InitiatingPipe = false,
Id = id
};
var pipe = new NexusDuplexPipe(id, localId, _session);

if (!_activePipes.TryAdd(id, pipe))
throw new Exception("Could not add NexusDuplexPipe to the list of current pipes.");
Expand Down Expand Up @@ -174,7 +169,10 @@ public DisconnectReason UpdateState(ushort id, NexusDuplexPipe.State state)

// Move the pipe to the main active pipes.
_activePipes.TryAdd(id, pipe);

// Set the full ID of the pipe.
pipe.Id = id;

pipe.UpdateState(state);
_logger?.LogError($"Readies pipe {id}");
return DisconnectReason.None;
Expand Down
2 changes: 1 addition & 1 deletion src/NexNet/Pipes/RentedNexusDuplexPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NexNet.Pipes;
internal class RentedNexusDuplexPipe : NexusDuplexPipe, IRentedNexusDuplexPipe
{
public RentedNexusDuplexPipe(byte localId, INexusSession session)
: base(localId, session)
: base(0, localId, session)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/NexNetDemo/SampleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SampleLogger : INexusLogger
{
private readonly string _prefix;

public string? Category { get; }
public string? Category { get; set; }

public SampleLogger(string prefix, string? category = null)
{
Expand Down

0 comments on commit 287f623

Please sign in to comment.