Skip to content

Commit

Permalink
Replace NetTuple with ValueTuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Mar 22, 2024
1 parent 82d82fb commit e5cd7cf
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Lidgren.Network/NetConnection.Handshake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal void SendConnect(double now)
om.Write((float)now);

WriteLocalHail(om);

m_peer.SendLibrary(om, m_remoteEndPoint);

m_connectRequested = false;
Expand Down Expand Up @@ -171,7 +171,7 @@ internal void SendConnectResponse(double now, bool onLibraryThread)
if (onLibraryThread)
m_peer.SendLibrary(om, m_remoteEndPoint);
else
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(m_remoteEndPoint, om));
m_peer.m_unsentUnconnectedMessages.Enqueue((m_remoteEndPoint, om));

m_lastHandshakeSendTime = now;
m_handshakeAttempts++;
Expand All @@ -193,7 +193,7 @@ internal void SendDisconnect(string? reason, bool onLibraryThread)
if (onLibraryThread)
m_peer.SendLibrary(om, m_remoteEndPoint);
else
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(m_remoteEndPoint, om));
m_peer.m_unsentUnconnectedMessages.Enqueue((m_remoteEndPoint, om));
}

private void WriteLocalHail(NetOutgoingMessage om)
Expand Down Expand Up @@ -473,7 +473,7 @@ private bool ValidateHandshakeData(int ptr, int payloadLength, out byte[]? hail)
}
return true;
}

/// <summary>
/// Disconnect from the remote peer
/// </summary>
Expand Down
16 changes: 8 additions & 8 deletions Lidgren.Network/NetConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public partial class NetConnection
internal NetReceiverChannelBase?[] m_receiveChannels;
internal NetOutgoingMessage? m_localHailMessage;
internal long m_remoteUniqueIdentifier;
internal NetQueue<NetTuple<NetMessageType, int>> m_queuedOutgoingAcks;
internal NetQueue<NetTuple<NetMessageType, int>> m_queuedIncomingAcks;
internal NetQueue<(NetMessageType, int)> m_queuedOutgoingAcks;
internal NetQueue<(NetMessageType, int)> m_queuedIncomingAcks;
private int m_sendBufferWritePtr;
private int m_sendBufferNumMessages;
private object? m_tag;
Expand Down Expand Up @@ -93,8 +93,8 @@ internal NetConnection(NetPeer peer, NetEndPoint remoteEndPoint)
m_remoteEndPoint = remoteEndPoint;
m_sendChannels = new NetSenderChannelBase[NetConstants.NumTotalChannels];
m_receiveChannels = new NetReceiverChannelBase[NetConstants.NumTotalChannels];
m_queuedOutgoingAcks = new NetQueue<NetTuple<NetMessageType, int>>(4);
m_queuedIncomingAcks = new NetQueue<NetTuple<NetMessageType, int>>(4);
m_queuedOutgoingAcks = new NetQueue<(NetMessageType, int)>(4);
m_queuedIncomingAcks = new NetQueue<(NetMessageType, int)>(4);
m_statistics = new NetConnectionStatistics(this);
m_averageRoundtripTime = -1.0f;
m_currentMTU = m_peerConfiguration.MTUForEndPoint(m_remoteEndPoint);
Expand Down Expand Up @@ -217,7 +217,7 @@ internal void Heartbeat(double now, uint frameCounter)
// write acks
for (int i = 0; i < acks; i++)
{
m_queuedOutgoingAcks.TryDequeue(out NetTuple<NetMessageType, int> tuple);
m_queuedOutgoingAcks.TryDequeue(out (NetMessageType, int) tuple);

//m_peer.LogVerbose("Sending ack for " + tuple.Item1 + "#" + tuple.Item2);

Expand All @@ -240,7 +240,7 @@ internal void Heartbeat(double now, uint frameCounter)
//
// Parse incoming acks (may trigger resends)
//
while (m_queuedIncomingAcks.TryDequeue(out NetTuple<NetMessageType, int> incAck))
while (m_queuedIncomingAcks.TryDequeue(out (NetMessageType, int) incAck))
{
//m_peer.LogVerbose("Received ack for " + acktp + "#" + seqNr);
NetSenderChannelBase? chan = m_sendChannels[(int)incAck.Item1 - 1];
Expand Down Expand Up @@ -445,7 +445,7 @@ internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLeng
seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8);

// need to enqueue this and handle it in the netconnection heartbeat; so be able to send resends together with normal sends
m_queuedIncomingAcks.Enqueue(new NetTuple<NetMessageType, int>(acktp, seqNr));
m_queuedIncomingAcks.Enqueue((acktp, seqNr));
}
break;
case NetMessageType.Ping:
Expand Down Expand Up @@ -532,7 +532,7 @@ private NetReceiverChannelBase CreateReceiverChannel(NetMessageType tp)

internal void QueueAck(NetMessageType tp, int sequenceNumber)
{
m_queuedOutgoingAcks.Enqueue(new NetTuple<NetMessageType, int>(tp, sequenceNumber));
m_queuedOutgoingAcks.Enqueue((tp, sequenceNumber));
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions Lidgren.Network/NetNatIntroduction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Introduce(
um.Write(hostExternal);
um.Write(token);
Interlocked.Increment(ref um.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(clientExternal, um));
m_unsentUnconnectedMessages.Enqueue((clientExternal, um));

// send message to host
um = CreateMessage(10 + token.Length + 1);
Expand All @@ -41,7 +41,7 @@ public void Introduce(
um.Write(clientExternal);
um.Write(token);
Interlocked.Increment(ref um.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(hostExternal, um));
m_unsentUnconnectedMessages.Enqueue((hostExternal, um));
}

/// <summary>
Expand Down Expand Up @@ -73,7 +73,7 @@ internal void HandleNatIntroduction(int ptr)
punch.Write(hostByte);
punch.Write(token);
Interlocked.Increment(ref punch.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(remoteInternal, punch));
m_unsentUnconnectedMessages.Enqueue((remoteInternal, punch));
LogDebug("NAT punch sent to " + remoteInternal);

// send external punch
Expand All @@ -82,7 +82,7 @@ internal void HandleNatIntroduction(int ptr)
punch.Write(hostByte);
punch.Write(token);
Interlocked.Increment(ref punch.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(remoteExternal, punch));
m_unsentUnconnectedMessages.Enqueue((remoteExternal, punch));
LogDebug("NAT punch sent to " + remoteExternal);

}
Expand All @@ -105,7 +105,7 @@ private void HandleNatPunch(int ptr, NetEndPoint senderEndPoint)
confirmResponse.Write(HostByte);
confirmResponse.Write(token);
Interlocked.Increment(ref confirmResponse.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(senderEndPoint, confirmResponse));
m_unsentUnconnectedMessages.Enqueue((senderEndPoint, confirmResponse));
}
else
{
Expand All @@ -116,7 +116,7 @@ private void HandleNatPunch(int ptr, NetEndPoint senderEndPoint)
confirmRequest.Write(ClientByte);
confirmRequest.Write(token);
Interlocked.Increment(ref confirmRequest.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(senderEndPoint, confirmRequest));
m_unsentUnconnectedMessages.Enqueue((senderEndPoint, confirmRequest));
}
}

Expand All @@ -133,7 +133,7 @@ private void HandleNatPunchConfirmRequest(int ptr, NetEndPoint senderEndPoint)
confirmResponse.Write(isFromClient ? HostByte : ClientByte);
confirmResponse.Write(token);
Interlocked.Increment(ref confirmResponse.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(senderEndPoint, confirmResponse));
m_unsentUnconnectedMessages.Enqueue((senderEndPoint, confirmResponse));
}

private void HandleNatPunchConfirmed(int ptr, NetEndPoint senderEndPoint)
Expand Down
6 changes: 3 additions & 3 deletions Lidgren.Network/NetPeer.Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void DiscoverLocalPeers(int serverPort)
if (broadcastAddress == null)
throw new NetException("Unable to determine broadcast address.");

m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(new NetEndPoint(broadcastAddress, serverPort), um));
m_unsentUnconnectedMessages.Enqueue((new NetEndPoint(broadcastAddress, serverPort), um));
}

/// <summary>
Expand All @@ -46,7 +46,7 @@ public void DiscoverKnownPeer(NetEndPoint endPoint)
NetOutgoingMessage om = CreateMessage(0);
om.m_messageType = NetMessageType.Discovery;
om.m_recyclingCount = 1;
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(endPoint, om));
m_unsentUnconnectedMessages.Enqueue((endPoint, om));
}

/// <summary>
Expand All @@ -68,7 +68,7 @@ public void SendDiscoveryResponse(NetOutgoingMessage msg, NetEndPoint recipient)

msg.m_messageType = NetMessageType.DiscoveryResponse;
Interlocked.Increment(ref msg.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(recipient, msg));
m_unsentUnconnectedMessages.Enqueue((recipient, msg));
}
}
}
10 changes: 5 additions & 5 deletions Lidgren.Network/NetPeer.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class NetPeer

internal readonly NetPeerConfiguration m_configuration;
private readonly NetQueue<NetIncomingMessage> m_releasedIncomingMessages;
internal readonly NetQueue<NetTuple<NetEndPoint, NetOutgoingMessage>> m_unsentUnconnectedMessages;
internal readonly NetQueue<(NetEndPoint, NetOutgoingMessage)> m_unsentUnconnectedMessages;

internal Dictionary<NetEndPoint, NetConnection> m_handshakes;

Expand All @@ -41,7 +41,7 @@ public partial class NetPeer
internal bool m_executeFlushSendQueue;

private AutoResetEvent? m_messageReceivedEvent;
private List<NetTuple<SynchronizationContext, SendOrPostCallback>>? m_receiveCallbacks;
private List<(SynchronizationContext, SendOrPostCallback)>? m_receiveCallbacks;

internal Action? m_onShutdown;

Expand All @@ -60,8 +60,8 @@ public void RegisterReceivedCallback(SendOrPostCallback callback, Synchronizatio
if (syncContext == null)
throw new NetException("Need a SynchronizationContext to register callback on correct thread!");
if (m_receiveCallbacks == null)
m_receiveCallbacks = new List<NetTuple<SynchronizationContext, SendOrPostCallback>>();
m_receiveCallbacks.Add(new NetTuple<SynchronizationContext, SendOrPostCallback>(syncContext, callback));
m_receiveCallbacks = new List<(SynchronizationContext, SendOrPostCallback)>();
m_receiveCallbacks.Add((syncContext, callback));
}

/// <summary>
Expand Down Expand Up @@ -383,7 +383,7 @@ private void Heartbeat()
m_executeFlushSendQueue = false;

// send unsent unconnected messages
while (m_unsentUnconnectedMessages.TryDequeue(out NetTuple<NetEndPoint, NetOutgoingMessage> unsent))
while (m_unsentUnconnectedMessages.TryDequeue(out (NetEndPoint, NetOutgoingMessage) unsent))
{
NetOutgoingMessage om = unsent.Item2;

Expand Down
6 changes: 3 additions & 3 deletions Lidgren.Network/NetPeer.Send.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void SendUnconnectedMessage(NetOutgoingMessage msg, string host, int port
msg.m_messageType = NetMessageType.Unconnected;

Interlocked.Increment(ref msg.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(new NetEndPoint(adr, port), msg));
m_unsentUnconnectedMessages.Enqueue((new NetEndPoint(adr, port), msg));
}

/// <summary>
Expand All @@ -195,7 +195,7 @@ public void SendUnconnectedMessage(NetOutgoingMessage msg, NetEndPoint recipient
msg.m_isSent = true;

Interlocked.Increment(ref msg.m_recyclingCount);
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(recipient, msg));
m_unsentUnconnectedMessages.Enqueue((recipient, msg));
}

/// <summary>
Expand Down Expand Up @@ -227,7 +227,7 @@ public void SendUnconnectedMessage(NetOutgoingMessage msg, IList<NetEndPoint> re

Interlocked.Add(ref msg.m_recyclingCount, recipients.Count);
foreach (NetEndPoint ep in recipients)
m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(ep, msg));
m_unsentUnconnectedMessages.Enqueue((ep, msg));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Lidgren.Network/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public NetPeer(NetPeerConfiguration config)
m_configuration = config;
m_statistics = new NetPeerStatistics(this);
m_releasedIncomingMessages = new NetQueue<NetIncomingMessage>(4);
m_unsentUnconnectedMessages = new NetQueue<NetTuple<NetEndPoint, NetOutgoingMessage>>(2);
m_unsentUnconnectedMessages = new NetQueue<(NetEndPoint, NetOutgoingMessage)>(2);
m_connections = new List<NetConnection>();
m_connectionLookup = new Dictionary<NetSocketAddress, NetConnection>();
m_handshakes = new Dictionary<NetEndPoint, NetConnection>();
Expand Down
19 changes: 0 additions & 19 deletions Lidgren.Network/NetTuple.cs

This file was deleted.

1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Fixed MTU expansion not setting internal state correctly and spamming network packets.
- Fixed `NetPeerConfiguration.ExpandMTUFailAttempts` not being respected completely.
- The new `NetPeerMetrics` allows exporting metrics about the library to `System.Diagnostics.Metrics`.
- Internal code cleanup.

## 0.2.7

Expand Down

0 comments on commit e5cd7cf

Please sign in to comment.