Skip to content

Commit

Permalink
refactor: replacing Debug with ILogger
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Mar 18, 2024
1 parent 42691a6 commit b5be86b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Assets/Mirage/Runtime/SocketLayer/Connection/AckSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class AckSystem : IDisposable
/// <param name="connection"></param>
/// <param name="ackTimeout">how long after last send before sending empty ack</param>
/// <param name="time"></param>
public AckSystem(IRawConnection connection, Config config, int maxPacketSize, ITime time, Pool<ByteBuffer> bufferPool, Metrics metrics = null)
public AckSystem(IRawConnection connection, Config config, int maxPacketSize, ITime time, Pool<ByteBuffer> bufferPool, ILogger logger = null, Metrics metrics = null)
{
if (config == null) throw new ArgumentNullException(nameof(config));

Expand All @@ -97,9 +97,9 @@ public AckSystem(IRawConnection connection, Config config, int maxPacketSize, IT

var size = config.SequenceSize;
if (size > 16) throw new ArgumentOutOfRangeException("SequenceSize", size, "SequenceSize has a max value of 16");
_sentAckablePackets = new RingBuffer<AckablePacket>(size);
_sentAckablePackets = new RingBuffer<AckablePacket>(size, logger);
_reliableOrder = new Sequencer(size);
_reliableReceive = new RingBuffer<ReliableReceived>(size);
_reliableReceive = new RingBuffer<ReliableReceived>(size, logger);

// set lastest to value before 0 so that first packet will be received
// max will be 1 less than 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal ReliableConnection(Peer peer, IEndPoint endPoint, IDataHandler dataHand
{

_bufferPool = bufferPool;
_ackSystem = new AckSystem(this, config, maxPacketSize, time, bufferPool, metrics);
_ackSystem = new AckSystem(this, config, maxPacketSize, time, bufferPool, logger, metrics);
}

public void Dispose()
Expand Down
23 changes: 23 additions & 0 deletions Assets/Mirage/Runtime/SocketLayer/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using UnityEngine;

namespace Mirage.SocketLayer
Expand All @@ -12,6 +13,28 @@ internal static void Assert<T>(this ILogger logger, bool condition, T msg)
{
if (!condition) logger.Log(LogType.Assert, $"Failed Assertion: {msg}");
}

/// <summary>
/// Assert only when DEBUG
/// </summary>
/// <param name="logger"></param>
/// <param name="condition"></param>
[Conditional("DEBUG")]
internal static void DebugAssert(this ILogger logger, bool condition)
{
if (!condition) logger.Log(LogType.Assert, "Failed Assertion");
}
/// <summary>
/// Assert only when DEBUG
/// </summary>
/// <param name="logger"></param>
/// <param name="condition"></param>
[Conditional("DEBUG")]
internal static void DebugAssert<T>(this ILogger logger, bool condition, T msg)
{
if (!condition) logger.Log(LogType.Assert, $"Failed Assertion: {msg}");
}

internal static void Error<T>(this ILogger logger, T msg = default)
{
logger.Log(LogType.Error, msg);
Expand Down
17 changes: 10 additions & 7 deletions Assets/Mirage/Runtime/SocketLayer/RingBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using ILogger = UnityEngine.ILogger;

namespace Mirage.SocketLayer
{
public class RingBuffer<T>
{
public readonly Sequencer Sequencer;
private readonly IEqualityComparer<T> _comparer;
private readonly ILogger _logger;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsDefault(T value)
{
Expand Down Expand Up @@ -53,12 +55,13 @@ public T this[int index]
get => _buffer[index];
}

public RingBuffer(int bitCount) : this(bitCount, EqualityComparer<T>.Default) { }
public RingBuffer(int bitCount, IEqualityComparer<T> comparer)
public RingBuffer(int bitCount, ILogger logger) : this(bitCount, EqualityComparer<T>.Default, logger) { }
public RingBuffer(int bitCount, IEqualityComparer<T> comparer, ILogger logger)
{
Sequencer = new Sequencer(bitCount);
_buffer = new T[1 << bitCount];
_comparer = comparer;
_logger = logger;
}

public bool IsFull => Sequencer.Distance(_write, _read) == -1;
Expand All @@ -74,7 +77,7 @@ public long DistanceToRead(uint from)
/// <returns>sequance of written item</returns>
public uint Enqueue(T item)
{
Debug.Assert(NotDefault(item), "Adding item, but it was null");
_logger?.DebugAssert(NotDefault(item), "Adding item, but it was null");

var dist = Sequencer.Distance(_write, _read);
if (dist == -1) { throw new InvalidOperationException($"Buffer is full, write:{_write} read:{_read}"); }
Expand Down Expand Up @@ -117,7 +120,7 @@ public bool Exists(uint index)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveNext()
{
Debug.Assert(NotDefault(_buffer[_read]), "Removing item, but it was already null");
_logger?.DebugAssert(NotDefault(_buffer[_read]), "Removing item, but it was already null");
_buffer[_read] = default;
_read = (uint)Sequencer.NextAfter(_read);
_count--;
Expand Down Expand Up @@ -156,13 +159,13 @@ public bool TryDequeue(out T item)

public void InsertAt(uint index, T item)
{
Debug.Assert(NotDefault(item), "Adding item, but it was null");
_logger?.DebugAssert(NotDefault(item), "Adding item, but it was null");
_count++;
_buffer[index] = item;
}
public void RemoveAt(uint index)
{
Debug.Assert(NotDefault(_buffer[index]), "Removing item, but it was already null");
_logger?.DebugAssert(NotDefault(_buffer[index]), "Removing item, but it was already null");
_count--;
_buffer[index] = default;
}
Expand Down

0 comments on commit b5be86b

Please sign in to comment.