From e5cd7cf139b1b5b07c1ae1b006cbdc645389df22 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Fri, 22 Mar 2024 22:41:06 +0100 Subject: [PATCH] Replace NetTuple with ValueTuple. --- Lidgren.Network/NetConnection.Handshake.cs | 8 ++++---- Lidgren.Network/NetConnection.cs | 16 ++++++++-------- Lidgren.Network/NetNatIntroduction.cs | 14 +++++++------- Lidgren.Network/NetPeer.Discovery.cs | 6 +++--- Lidgren.Network/NetPeer.Internal.cs | 10 +++++----- Lidgren.Network/NetPeer.Send.cs | 6 +++--- Lidgren.Network/NetPeer.cs | 2 +- Lidgren.Network/NetTuple.cs | 19 ------------------- RELEASE-NOTES.md | 1 + 9 files changed, 32 insertions(+), 50 deletions(-) delete mode 100644 Lidgren.Network/NetTuple.cs diff --git a/Lidgren.Network/NetConnection.Handshake.cs b/Lidgren.Network/NetConnection.Handshake.cs index c1fd1255..a1dd00d2 100644 --- a/Lidgren.Network/NetConnection.Handshake.cs +++ b/Lidgren.Network/NetConnection.Handshake.cs @@ -143,7 +143,7 @@ internal void SendConnect(double now) om.Write((float)now); WriteLocalHail(om); - + m_peer.SendLibrary(om, m_remoteEndPoint); m_connectRequested = false; @@ -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(m_remoteEndPoint, om)); + m_peer.m_unsentUnconnectedMessages.Enqueue((m_remoteEndPoint, om)); m_lastHandshakeSendTime = now; m_handshakeAttempts++; @@ -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(m_remoteEndPoint, om)); + m_peer.m_unsentUnconnectedMessages.Enqueue((m_remoteEndPoint, om)); } private void WriteLocalHail(NetOutgoingMessage om) @@ -473,7 +473,7 @@ private bool ValidateHandshakeData(int ptr, int payloadLength, out byte[]? hail) } return true; } - + /// /// Disconnect from the remote peer /// diff --git a/Lidgren.Network/NetConnection.cs b/Lidgren.Network/NetConnection.cs index 422df289..d35c7965 100644 --- a/Lidgren.Network/NetConnection.cs +++ b/Lidgren.Network/NetConnection.cs @@ -28,8 +28,8 @@ public partial class NetConnection internal NetReceiverChannelBase?[] m_receiveChannels; internal NetOutgoingMessage? m_localHailMessage; internal long m_remoteUniqueIdentifier; - internal NetQueue> m_queuedOutgoingAcks; - internal NetQueue> 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; @@ -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>(4); - m_queuedIncomingAcks = new NetQueue>(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); @@ -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 tuple); + m_queuedOutgoingAcks.TryDequeue(out (NetMessageType, int) tuple); //m_peer.LogVerbose("Sending ack for " + tuple.Item1 + "#" + tuple.Item2); @@ -240,7 +240,7 @@ internal void Heartbeat(double now, uint frameCounter) // // Parse incoming acks (may trigger resends) // - while (m_queuedIncomingAcks.TryDequeue(out NetTuple 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]; @@ -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(acktp, seqNr)); + m_queuedIncomingAcks.Enqueue((acktp, seqNr)); } break; case NetMessageType.Ping: @@ -532,7 +532,7 @@ private NetReceiverChannelBase CreateReceiverChannel(NetMessageType tp) internal void QueueAck(NetMessageType tp, int sequenceNumber) { - m_queuedOutgoingAcks.Enqueue(new NetTuple(tp, sequenceNumber)); + m_queuedOutgoingAcks.Enqueue((tp, sequenceNumber)); } /// diff --git a/Lidgren.Network/NetNatIntroduction.cs b/Lidgren.Network/NetNatIntroduction.cs index 1df7e429..1074509c 100644 --- a/Lidgren.Network/NetNatIntroduction.cs +++ b/Lidgren.Network/NetNatIntroduction.cs @@ -31,7 +31,7 @@ public void Introduce( um.Write(hostExternal); um.Write(token); Interlocked.Increment(ref um.m_recyclingCount); - m_unsentUnconnectedMessages.Enqueue(new NetTuple(clientExternal, um)); + m_unsentUnconnectedMessages.Enqueue((clientExternal, um)); // send message to host um = CreateMessage(10 + token.Length + 1); @@ -41,7 +41,7 @@ public void Introduce( um.Write(clientExternal); um.Write(token); Interlocked.Increment(ref um.m_recyclingCount); - m_unsentUnconnectedMessages.Enqueue(new NetTuple(hostExternal, um)); + m_unsentUnconnectedMessages.Enqueue((hostExternal, um)); } /// @@ -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(remoteInternal, punch)); + m_unsentUnconnectedMessages.Enqueue((remoteInternal, punch)); LogDebug("NAT punch sent to " + remoteInternal); // send external punch @@ -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(remoteExternal, punch)); + m_unsentUnconnectedMessages.Enqueue((remoteExternal, punch)); LogDebug("NAT punch sent to " + remoteExternal); } @@ -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(senderEndPoint, confirmResponse)); + m_unsentUnconnectedMessages.Enqueue((senderEndPoint, confirmResponse)); } else { @@ -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(senderEndPoint, confirmRequest)); + m_unsentUnconnectedMessages.Enqueue((senderEndPoint, confirmRequest)); } } @@ -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(senderEndPoint, confirmResponse)); + m_unsentUnconnectedMessages.Enqueue((senderEndPoint, confirmResponse)); } private void HandleNatPunchConfirmed(int ptr, NetEndPoint senderEndPoint) diff --git a/Lidgren.Network/NetPeer.Discovery.cs b/Lidgren.Network/NetPeer.Discovery.cs index a38e11e7..cc4be7b6 100644 --- a/Lidgren.Network/NetPeer.Discovery.cs +++ b/Lidgren.Network/NetPeer.Discovery.cs @@ -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(new NetEndPoint(broadcastAddress, serverPort), um)); + m_unsentUnconnectedMessages.Enqueue((new NetEndPoint(broadcastAddress, serverPort), um)); } /// @@ -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(endPoint, om)); + m_unsentUnconnectedMessages.Enqueue((endPoint, om)); } /// @@ -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(recipient, msg)); + m_unsentUnconnectedMessages.Enqueue((recipient, msg)); } } } diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs index e939b2ce..6857d77f 100644 --- a/Lidgren.Network/NetPeer.Internal.cs +++ b/Lidgren.Network/NetPeer.Internal.cs @@ -32,7 +32,7 @@ public partial class NetPeer internal readonly NetPeerConfiguration m_configuration; private readonly NetQueue m_releasedIncomingMessages; - internal readonly NetQueue> m_unsentUnconnectedMessages; + internal readonly NetQueue<(NetEndPoint, NetOutgoingMessage)> m_unsentUnconnectedMessages; internal Dictionary m_handshakes; @@ -41,7 +41,7 @@ public partial class NetPeer internal bool m_executeFlushSendQueue; private AutoResetEvent? m_messageReceivedEvent; - private List>? m_receiveCallbacks; + private List<(SynchronizationContext, SendOrPostCallback)>? m_receiveCallbacks; internal Action? m_onShutdown; @@ -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>(); - m_receiveCallbacks.Add(new NetTuple(syncContext, callback)); + m_receiveCallbacks = new List<(SynchronizationContext, SendOrPostCallback)>(); + m_receiveCallbacks.Add((syncContext, callback)); } /// @@ -383,7 +383,7 @@ private void Heartbeat() m_executeFlushSendQueue = false; // send unsent unconnected messages - while (m_unsentUnconnectedMessages.TryDequeue(out NetTuple unsent)) + while (m_unsentUnconnectedMessages.TryDequeue(out (NetEndPoint, NetOutgoingMessage) unsent)) { NetOutgoingMessage om = unsent.Item2; diff --git a/Lidgren.Network/NetPeer.Send.cs b/Lidgren.Network/NetPeer.Send.cs index e805b0dd..3750667c 100644 --- a/Lidgren.Network/NetPeer.Send.cs +++ b/Lidgren.Network/NetPeer.Send.cs @@ -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(new NetEndPoint(adr, port), msg)); + m_unsentUnconnectedMessages.Enqueue((new NetEndPoint(adr, port), msg)); } /// @@ -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(recipient, msg)); + m_unsentUnconnectedMessages.Enqueue((recipient, msg)); } /// @@ -227,7 +227,7 @@ public void SendUnconnectedMessage(NetOutgoingMessage msg, IList re Interlocked.Add(ref msg.m_recyclingCount, recipients.Count); foreach (NetEndPoint ep in recipients) - m_unsentUnconnectedMessages.Enqueue(new NetTuple(ep, msg)); + m_unsentUnconnectedMessages.Enqueue((ep, msg)); } /// diff --git a/Lidgren.Network/NetPeer.cs b/Lidgren.Network/NetPeer.cs index 8f8e8337..0255500e 100644 --- a/Lidgren.Network/NetPeer.cs +++ b/Lidgren.Network/NetPeer.cs @@ -118,7 +118,7 @@ public NetPeer(NetPeerConfiguration config) m_configuration = config; m_statistics = new NetPeerStatistics(this); m_releasedIncomingMessages = new NetQueue(4); - m_unsentUnconnectedMessages = new NetQueue>(2); + m_unsentUnconnectedMessages = new NetQueue<(NetEndPoint, NetOutgoingMessage)>(2); m_connections = new List(); m_connectionLookup = new Dictionary(); m_handshakes = new Dictionary(); diff --git a/Lidgren.Network/NetTuple.cs b/Lidgren.Network/NetTuple.cs deleted file mode 100644 index 40ff4232..00000000 --- a/Lidgren.Network/NetTuple.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Lidgren.Network -{ - // replace with BCL 4.0 Tuple<> when appropriate - internal struct NetTuple - { - public A Item1; - public B Item2; - - public NetTuple(A item1, B item2) - { - Item1 = item1; - Item2 = item2; - } - } -} diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 46d573d5..2d8d8dca 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -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