Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentFire committed Nov 18, 2023
1 parent dc8eb58 commit 61eeb0c
Show file tree
Hide file tree
Showing 29 changed files with 155 additions and 175 deletions.
4 changes: 2 additions & 2 deletions Lidgren.Network/Lidgren.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<AssemblyName>SpaceWizards.Lidgren.Network</AssemblyName>
<IncludeSymbols>true</IncludeSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>

<!-- Uncomment the following line to not get statistics in RELEASE mode -->
<DefineConstants>$(DefineConstants);USE_RELEASE_STATISTICS</DefineConstants>
Expand Down
7 changes: 3 additions & 4 deletions Lidgren.Network/NetBigInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private static int GetByteLength(

private NetBigInteger()
{
m_magnitude = null!;
}

private NetBigInteger(
Expand Down Expand Up @@ -1131,10 +1132,8 @@ public NetBigInteger ModPow(

yVal = new int[m.m_magnitude.Length];

if (zVal == null || yAccum == null)
{
throw new InvalidOperationException("both zVal and yAccum should not be null");
}
NetException.ThrowIfNull(zVal);
NetException.ThrowIfNull(yAccum);

//
// from LSW to MSW
Expand Down
36 changes: 18 additions & 18 deletions Lidgren.Network/NetBuffer.Peek.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public partial class NetBuffer
public bool PeekBoolean()
{
NetException.Assert(m_bitLength - m_readPosition >= 1, c_readOverflowError);
byte retval = NetBitWriter.ReadByte(BufferData, 1, m_readPosition);
byte retval = NetBitWriter.ReadByte(Data, 1, m_readPosition);
return (retval > 0 ? true : false);
}

Expand All @@ -52,7 +52,7 @@ public bool PeekBoolean()
public byte PeekByte()
{
NetException.Assert(m_bitLength - m_readPosition >= 8, c_readOverflowError);
byte retval = NetBitWriter.ReadByte(BufferData, 8, m_readPosition);
byte retval = NetBitWriter.ReadByte(Data, 8, m_readPosition);
return retval;
}

Expand All @@ -63,7 +63,7 @@ public byte PeekByte()
public sbyte PeekSByte()
{
NetException.Assert(m_bitLength - m_readPosition >= 8, c_readOverflowError);
byte retval = NetBitWriter.ReadByte(BufferData, 8, m_readPosition);
byte retval = NetBitWriter.ReadByte(Data, 8, m_readPosition);
return (sbyte)retval;
}

Expand All @@ -72,7 +72,7 @@ public sbyte PeekSByte()
/// </summary>
public byte PeekByte(int numberOfBits)
{
byte retval = NetBitWriter.ReadByte(BufferData, numberOfBits, m_readPosition);
byte retval = NetBitWriter.ReadByte(Data, numberOfBits, m_readPosition);
return retval;
}

Expand All @@ -86,7 +86,7 @@ public Span<byte> PeekBytes(Span<byte> into)
{
NetException.Assert(m_bitLength - m_readPosition >= (into.Length * 8), c_readOverflowError);

NetBitWriter.ReadBytes(BufferData, m_readPosition, into);
NetBitWriter.ReadBytes(Data, m_readPosition, into);

return into;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public void PeekBytes(byte[] into, int offset, int numberOfBytes)
public short PeekInt16()
{
NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt16(BufferData, 16, m_readPosition);
uint retval = NetBitWriter.ReadUInt16(Data, 16, m_readPosition);
return (short)retval;
}

Expand All @@ -130,7 +130,7 @@ public short PeekInt16()
public ushort PeekUInt16()
{
NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt16(BufferData, 16, m_readPosition);
uint retval = NetBitWriter.ReadUInt16(Data, 16, m_readPosition);
return (ushort)retval;
}

Expand All @@ -143,7 +143,7 @@ public ushort PeekUInt16()
public int PeekInt32()
{
NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt32(BufferData, 32, m_readPosition);
uint retval = NetBitWriter.ReadUInt32(Data, 32, m_readPosition);
return (int)retval;
}

Expand All @@ -155,7 +155,7 @@ public int PeekInt32(int numberOfBits)
NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "ReadInt() can only read between 1 and 32 bits");
NetException.Assert(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError);

uint retval = NetBitWriter.ReadUInt32(BufferData, numberOfBits, m_readPosition);
uint retval = NetBitWriter.ReadUInt32(Data, numberOfBits, m_readPosition);

if (numberOfBits == 32)
return (int)retval;
Expand All @@ -180,7 +180,7 @@ public int PeekInt32(int numberOfBits)
public uint PeekUInt32()
{
NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt32(BufferData, 32, m_readPosition);
uint retval = NetBitWriter.ReadUInt32(Data, 32, m_readPosition);
return retval;
}

Expand All @@ -193,7 +193,7 @@ public uint PeekUInt32(int numberOfBits)
NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "ReadUInt() can only read between 1 and 32 bits");
//NetException.Assert(m_bitLength - m_readBitPtr >= numberOfBits, "tried to read past buffer size");

uint retval = NetBitWriter.ReadUInt32(BufferData, numberOfBits, m_readPosition);
uint retval = NetBitWriter.ReadUInt32(Data, numberOfBits, m_readPosition);
return retval;
}

Expand All @@ -208,8 +208,8 @@ public ulong PeekUInt64()
{
NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);

ulong low = NetBitWriter.ReadUInt32(BufferData, 32, m_readPosition);
ulong high = NetBitWriter.ReadUInt32(BufferData, 32, m_readPosition + 32);
ulong low = NetBitWriter.ReadUInt32(Data, 32, m_readPosition);
ulong high = NetBitWriter.ReadUInt32(Data, 32, m_readPosition + 32);

ulong retval = low + (high << 32);

Expand Down Expand Up @@ -242,12 +242,12 @@ public ulong PeekUInt64(int numberOfBits)
ulong retval;
if (numberOfBits <= 32)
{
retval = (ulong)NetBitWriter.ReadUInt32(BufferData, numberOfBits, m_readPosition);
retval = (ulong)NetBitWriter.ReadUInt32(Data, numberOfBits, m_readPosition);
}
else
{
retval = NetBitWriter.ReadUInt32(BufferData, 32, m_readPosition);
retval |= (ulong)NetBitWriter.ReadUInt32(BufferData, numberOfBits - 32, m_readPosition + 32) << 32;
retval = NetBitWriter.ReadUInt32(Data, 32, m_readPosition);
retval |= (ulong)NetBitWriter.ReadUInt32(Data, numberOfBits - 32, m_readPosition + 32) << 32;
}
return retval;
}
Expand Down Expand Up @@ -299,7 +299,7 @@ public float PeekSingle()

if ((m_readPosition & 7) == 0) // read directly
{
float retval = BitConverter.ToSingle(BufferData, m_readPosition >> 3);
float retval = BitConverter.ToSingle(Data, m_readPosition >> 3);
return retval;
}

Expand All @@ -317,7 +317,7 @@ public double PeekDouble()
if ((m_readPosition & 7) == 0) // read directly
{
// read directly
double retval = BitConverter.ToDouble(BufferData, m_readPosition >> 3);
double retval = BitConverter.ToDouble(Data, m_readPosition >> 3);
return retval;
}

Expand Down
14 changes: 7 additions & 7 deletions Lidgren.Network/NetBuffer.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Span<byte> ReadBytes(Span<byte> into)
{
NetException.Assert(m_bitLength - m_readPosition + 7 >= (into.Length * 8), c_readOverflowError);

NetBitWriter.ReadBytes(m_data, m_readPosition, into);
NetBitWriter.ReadBytes(Data, m_readPosition, into);
m_readPosition += (8 * into.Length);
return into;
}
Expand All @@ -111,7 +111,7 @@ public bool ReadBytes(int numberOfBytes, [MaybeNullWhen(false)] out byte[] resul
}

result = new byte[numberOfBytes];
NetBitWriter.ReadBytes(BufferData, numberOfBytes, m_readPosition, result, 0);
NetBitWriter.ReadBytes(Data, numberOfBytes, m_readPosition, result, 0);
m_readPosition += (8 * numberOfBytes);
return true;
}
Expand All @@ -126,7 +126,7 @@ public bool TryReadBytes(Span<byte> into)
return false;
}

NetBitWriter.ReadBytes(m_data, m_readPosition, into);
NetBitWriter.ReadBytes(Data, m_readPosition, into);
m_readPosition += (8 * into.Length);
return true;
}
Expand All @@ -142,7 +142,7 @@ public void ReadBytes(byte[] into, int offset, int numberOfBytes)
NetException.Assert(m_bitLength - m_readPosition + 7 >= (numberOfBytes * 8), c_readOverflowError);
NetException.Assert(offset + numberOfBytes <= into.Length);

NetBitWriter.ReadBytes(m_data, numberOfBytes, m_readPosition, into, offset);
NetBitWriter.ReadBytes(Data, numberOfBytes, m_readPosition, into, offset);
m_readPosition += (8 * numberOfBytes);
return;
}
Expand All @@ -160,7 +160,7 @@ public void ReadBits(Span<byte> into, int numberOfBits)
int numberOfWholeBytes = numberOfBits / 8;
int extraBits = numberOfBits - (numberOfWholeBytes * 8);

NetBitWriter.ReadBytes(m_data, m_readPosition, into.Slice(0, numberOfWholeBytes));
NetBitWriter.ReadBytes(Data, m_readPosition, into.Slice(0, numberOfWholeBytes));
m_readPosition += (8 * numberOfWholeBytes);

if (extraBits > 0)
Expand Down Expand Up @@ -598,7 +598,7 @@ public string ReadString()
/// <summary>
/// Reads a string written using Write(string) and returns true for success
/// </summary>
public bool ReadString(out string result)
public bool ReadString([MaybeNullWhen(false)] out string result)
{
if (ReadVariableUInt32(out uint byteLen) == false)
{
Expand Down Expand Up @@ -645,7 +645,7 @@ public bool ReadString(out string result)
return false;
}

if (ReadBytes((int)byteLen, out byte[] bytes) == false)
if (ReadBytes((int)byteLen, out byte[]? bytes) == false)
{
result = string.Empty;
return false;
Expand Down
4 changes: 2 additions & 2 deletions Lidgren.Network/NetBuffer.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public int WriteRangedInteger(long min, long max, long value)
/// <summary>
/// Write a string
/// </summary>
public void Write(string source)
public void Write(string? source)
{
if (string.IsNullOrEmpty(source))
{
Expand Down Expand Up @@ -640,7 +640,7 @@ public void Write(NetBuffer buffer)
{
EnsureBufferSize(m_bitLength + (buffer.LengthBytes * 8));

Write(buffer.m_data, 0, buffer.LengthBytes);
Write(buffer.Data, 0, buffer.LengthBytes);

// did we write excessive bits?
int bitsInLastByte = (buffer.m_bitLength & 7);
Expand Down
16 changes: 9 additions & 7 deletions Lidgren.Network/NetBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ public partial class NetBuffer
private static readonly Dictionary<Type, MethodInfo> s_readMethods;
private static readonly Dictionary<Type, MethodInfo> s_writeMethods;

internal byte[] BufferData => m_data ?? throw new InvalidOperationException($"{nameof(m_data)} is null");

internal byte[]? m_data;
internal int m_bitLength;
internal int m_readPosition;

/// <summary>
/// Gets or sets the internal data buffer
/// Gets or sets the internal data buffer.
/// </summary>
public byte[]? Data
/// <remarks>
/// Throws an <see cref="NetException"/> if internal <see cref="m_data"/> field is null or if set a null value.
/// </remarks>
/// <exception cref="NetException"/>
public byte[] Data
{
get { return m_data; }
set { m_data = value; }
get { return NetException.ThrowIfNull(m_data); }
set { m_data = NetException.ThrowIfNull(value); }
}

/// <summary>
Expand Down Expand Up @@ -71,7 +73,7 @@ public int PositionInBytes
{
get { return (int)(m_readPosition / 8); }
}

static NetBuffer()
{
s_readMethods = new Dictionary<Type, MethodInfo>();
Expand Down
6 changes: 4 additions & 2 deletions Lidgren.Network/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ public override NetConnection Connect(NetEndPoint remoteEndPoint, NetOutgoingMes
{
if (m_connections.Count > 0)
{
throw new InvalidOperationException("Connect attempt failed; Already connected");
LogWarning("Connect attempt failed; Already connected");
return null!;
}
}

lock (m_handshakes)
{
if (m_handshakes.Count > 0)
{
throw new InvalidOperationException("Connect attempt failed; Handshake already in progress");
LogWarning("Connect attempt failed; Handshake already in progress");
return null!;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Lidgren.Network/NetConnection.Handshake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ internal void ReceivedHandshake(double now, NetMessageType tp, int ptr, int payl
appMsg.m_senderConnection = this;
appMsg.m_senderEndPoint = this.m_remoteEndPoint;
if (m_remoteHailMessage != null)
appMsg.Write(m_remoteHailMessage.BufferData, 0, m_remoteHailMessage.LengthBytes);
appMsg.Write(m_remoteHailMessage.Data, 0, m_remoteHailMessage.LengthBytes);
SetStatus(NetConnectionStatus.RespondedAwaitingApproval, "Awaiting approval");
m_peer.ReleaseMessage(appMsg);
return;
Expand Down
2 changes: 1 addition & 1 deletion Lidgren.Network/NetConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ internal void ResetTimeout(double now)
m_timeoutDeadline = now + m_peerConfiguration.m_connectionTimeout;
}

internal void SetStatus(NetConnectionStatus status, string reason)
internal void SetStatus(NetConnectionStatus status, string? reason)
{
// user or library thread

Expand Down
23 changes: 22 additions & 1 deletion Lidgren.Network/NetException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Lidgren.Network
{
Expand Down Expand Up @@ -70,5 +71,25 @@ public static void Assert(bool isOk)
if (!isOk)
throw new NetException();
}

/// <summary>Returns the passed <paramref name="argument"/>, but throws an <see cref="NetException"/> if the <paramref name="argument"/> is null.</summary>
/// <param name="argument">The reference type argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ThrowIfNull<T>(T argument, string? paramName = null)
{
return argument;
}
#else
#if NET5_0_OR_GREATER
public static T ThrowIfNull<T>([NotNull] T? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) where T : class
#else
public static T ThrowIfNull<T>([NotNull] T? argument, string? paramName = null) where T : class
#endif
{
return argument ?? throw new NetException($"{paramName ?? "argument"} is null");
}
}
#endif
}
4 changes: 2 additions & 2 deletions Lidgren.Network/NetOutgoingMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal int Encode(byte[] intoBuffer, int ptr, int sequenceNumber)
int byteLen = NetUtility.BytesToHoldBits(m_bitLength);
if (byteLen > 0)
{
Buffer.BlockCopy(BufferData, 0, intoBuffer, ptr, byteLen);
Buffer.BlockCopy(Data, 0, intoBuffer, ptr, byteLen);
ptr += byteLen;
}
}
Expand All @@ -102,7 +102,7 @@ internal int Encode(byte[] intoBuffer, int ptr, int sequenceNumber)
int byteLen = NetUtility.BytesToHoldBits(m_bitLength);
if (byteLen > 0)
{
Buffer.BlockCopy(BufferData, (int)(m_fragmentChunkNumber * m_fragmentChunkByteSize), intoBuffer, ptr, byteLen);
Buffer.BlockCopy(Data, (int)(m_fragmentChunkNumber * m_fragmentChunkByteSize), intoBuffer, ptr, byteLen);
ptr += byteLen;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Lidgren.Network/NetPeer.Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void DiscoverLocalPeers(int serverPort)
um.m_messageType = NetMessageType.Discovery;
Interlocked.Increment(ref um.m_recyclingCount);

m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(new NetEndPoint(NetUtility.GetBroadcastAddress() ?? throw new InvalidOperationException(), serverPort), um));
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(new NetEndPoint(NetException.ThrowIfNull(NetUtility.GetBroadcastAddress()), serverPort), um));
}

/// <summary>
Expand Down
Loading

0 comments on commit 61eeb0c

Please sign in to comment.