Skip to content

Commit

Permalink
feat: adding SocketLayerException to better handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Jul 9, 2024
1 parent 365c319 commit c76b88a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 14 deletions.
12 changes: 6 additions & 6 deletions Assets/Mirage/Runtime/SocketLayer/Connection/AckSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ public void SendNotify(byte[] inPacket, int inOffset, int inLength, INotifyCallB
{
if (inLength + NOTIFY_HEADER_SIZE > _maxPacketSize)
{
throw new ArgumentException($"Message is bigger than MTU, size:{inLength} but max Notify message size is {_maxPacketSize - NOTIFY_HEADER_SIZE}");
throw new MessageSizeException($"Message is bigger than MTU, size:{inLength} but max Notify message size is {_maxPacketSize - NOTIFY_HEADER_SIZE}");
}
if (_sentAckablePackets.IsFull)
{
throw new InvalidOperationException("Sent queue is full");
throw new BufferFullException($"Sent queue is full for {_connection}");
}

var sequence = (ushort)_sentAckablePackets.Enqueue(new AckablePacket(callBacks));
Expand All @@ -310,13 +310,13 @@ public void SendReliable(byte[] message, int offset, int length)
{
if (_sentAckablePackets.IsFull)
{
throw new InvalidOperationException($"Sent queue is full for {_connection}");
throw new BufferFullException($"Sent queue is full for {_connection}");
}

if (length + MIN_RELIABLE_HEADER_SIZE > _maxPacketSize)
{
if (!_allowFragmented)
throw new ArgumentException($"Message is bigger than MTU and fragmentation is disabled, max Reliable message size is {_maxPacketSize - MIN_RELIABLE_HEADER_SIZE}", nameof(length));
throw new MessageSizeException($"Message is bigger than MTU and fragmentation is disabled, max Reliable message size is {_maxPacketSize - MIN_RELIABLE_HEADER_SIZE}");

// if there is existing batch, send it first
// we need to do this so that fragmented message arrive in order
Expand All @@ -341,7 +341,7 @@ private void SendFragmented(byte[] message, int offset, int length)
{
if (length > _maxFragmentsMessageSize)
{
throw new ArgumentException($"Message is bigger than MTU for fragmentation, max Reliable fragmented size is {_maxFragmentsMessageSize}", nameof(length));
throw new MessageSizeException($"Message is bigger than MTU for fragmentation, max Reliable fragmented size is {_maxFragmentsMessageSize}");
}

var fragments = Mathf.CeilToInt(length / (float)SizePerFragment);
Expand Down Expand Up @@ -406,7 +406,7 @@ private void ThrowIfBufferLimitReached()
// greater or equal, because we are adding 1 adder this check
if (_sentAckablePackets.Count >= _maxPacketsInSendBufferPerConnection)
{
throw new InvalidOperationException($"Max packets in send buffer reached for {_connection}");
throw new BufferFullException($"Max packets in send buffer reached for {_connection}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Runtime/SocketLayer/Connection/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected void ThrowIfNotConnectedOrConnecting()
{
// sending to Connecting is also valid
if (_state != ConnectionState.Connected && _state != ConnectionState.Connecting)
throw new InvalidOperationException("Connection is not connected");
throw new NoConnectionException($"Connection is not connected, ConnectionState: {_state}");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void SendReliable(byte[] message, int offset, int length)

if (length + HEADER_SIZE > _maxPacketSize)
{
throw new ArgumentException($"Message is bigger than MTU, size:{length} but max message size is {_maxPacketSize - HEADER_SIZE}");
throw new MessageSizeException($"Message is bigger than MTU, size:{length} but max message size is {_maxPacketSize - HEADER_SIZE}");
}

_nextBatchReliable.AddMessage(message, offset, length);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/SocketLayer/Connection/NotifyToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public class NotifyToken : INotifyToken, INotifyCallBack

public void OnDelivered()
{
if (_notified) throw new InvalidOperationException("this token as already been notified");
if (_notified) throw new NotifyTokenException("this token as already been notified");
_notified = true;

Delivered?.Invoke();
}

public void OnLost()
{
if (_notified) throw new InvalidOperationException("this token as already been notified");
if (_notified) throw new NotifyTokenException("this token as already been notified");
_notified = true;

Lost?.Invoke();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override void SendUnreliable(byte[] packet, int offset, int length)

if (length + 1 > _maxPacketSize)
{
throw new ArgumentException($"Message is bigger than MTU, size:{length} but max Unreliable message size is {_maxPacketSize - 1}");
throw new MessageSizeException($"Message is bigger than MTU, size:{length} but max Unreliable message size is {_maxPacketSize - 1}");
}

_unreliableBatch.AddMessage(packet, offset, length);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace Mirage.SocketLayer
{
/// <summary>
/// Base Exception by all errors from using SocketLayer
/// </summary>
public class SocketLayerException : Exception
{
public SocketLayerException(string message) : base(message)
{
}
}

public class BufferFullException : SocketLayerException
{
public BufferFullException(string message) : base(message)
{
}
}

public class MessageSizeException : SocketLayerException
{
public MessageSizeException(string message) : base(message)
{
}
}

public class NoConnectionException : SocketLayerException
{
public NoConnectionException(string message) : base(message)
{
}
}

public class NotifyTokenException : SocketLayerException
{
public NotifyTokenException(string message) : base(message)
{
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Assets/Mirage/Runtime/SocketLayer/RingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ public long DistanceToRead(uint from)
///
/// </summary>
/// <param name="item"></param>
/// <returns>sequance of written item</returns>
/// <returns>sequence of written item</returns>
public uint Enqueue(T item)
{
_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}"); }
var distance = Sequencer.Distance(_write, _read);
if (distance == -1)
throw new BufferFullException($"Buffer is full, write:{_write} read:{_read}");

_buffer[_write] = item;
var sequence = _write;
Expand Down

0 comments on commit c76b88a

Please sign in to comment.