From: Matthew Endsley Date: Wed, 3 Feb 2021 10:02:12 +0000 (-0800) Subject: Modify ConnectionId to key on both an endpoint and serial X-Git-Tag: 1.0.0~20^2~8^2 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=2678b17f65dbace2563840a969fa8c6c3ec571cb;p=rhonda%2Fimpostor.hazel.git Modify ConnectionId to key on both an endpoint and serial --- diff --git a/Hazel/Dtls/DtlsConnectionListener.cs b/Hazel/Dtls/DtlsConnectionListener.cs index 088a113..b7a8056 100644 --- a/Hazel/Dtls/DtlsConnectionListener.cs +++ b/Hazel/Dtls/DtlsConnectionListener.cs @@ -97,7 +97,7 @@ namespace Hazel.Dtls this.CurrentEpoch.ServerFinishedVerification = block.Slice(0, Finished.Size); this.CurrentEpoch.ExpectedClientFinishedVerification = block.Slice(Finished.Size, Finished.Size); - ResetPeer(ConnectionId.Create(0), 1); + ResetPeer(ConnectionId.Create(new IPEndPoint(0,0), 0), 1); } public void ResetPeer(ConnectionId connectionId, ulong nextExpectedSequenceNumber) @@ -147,7 +147,7 @@ namespace Hazel.Dtls private readonly ConcurrentDictionary existingPeers = new ConcurrentDictionary(); - private long connectionId_unsafe = 0; + private int connectionSerial_unsafe = 0; /// /// Create a new instance of the DTLS listener @@ -765,7 +765,7 @@ namespace Hazel.Dtls if (record.Epoch == 0 && peer.Epoch != 0) { ConnectionId oldConnectionId = peer.ConnectionId; - peer.ResetPeer(this.AllocateConnectionId(), record.SequenceNumber + 1); + peer.ResetPeer(this.AllocateConnectionId(peerAddress), record.SequenceNumber + 1); // Inform the parent layer that the existing // connection should be abandoned. @@ -1102,7 +1102,7 @@ namespace Hazel.Dtls // Allocate state for the new peer and register it PeerData peer = new PeerData(); - peer.ResetPeer(this.AllocateConnectionId(), record.SequenceNumber + 1); + peer.ResetPeer(this.AllocateConnectionId(peerAddress), record.SequenceNumber + 1); this.existingPeers[peerAddress] = peer; @@ -1232,10 +1232,10 @@ namespace Hazel.Dtls /// /// Allocate a new connection id /// - private ConnectionId AllocateConnectionId() + private ConnectionId AllocateConnectionId(IPEndPoint endPoint) { - ulong rawConnectionId = (ulong)Interlocked.Increment(ref this.connectionId_unsafe); - return ConnectionId.Create(rawConnectionId); + int rawSerialId = Interlocked.Increment(ref this.connectionSerial_unsafe); + return ConnectionId.Create(endPoint, rawSerialId); } } } diff --git a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs index 339cb5b..46a723a 100644 --- a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs +++ b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs @@ -52,30 +52,22 @@ namespace Hazel.Udp.FewerThreads public struct ConnectionId : IEquatable { - public ulong Id; + public IPEndPoint EndPoint; + public int Serial; - public static ConnectionId Create(ulong id) + public static ConnectionId Create(IPEndPoint endPoint, int serial) { - ConnectionId result = new ConnectionId(); - result.Id = id; - return result; - } - - public static ConnectionId CreateFromEndPoint(IPEndPoint endPoint) - { - if (endPoint.AddressFamily != AddressFamily.InterNetwork) - { - throw new ArgumentException("ConnectionId only supports IPv4"); - } - - ulong port = (ulong)endPoint.Port; - ulong address = (ulong)endPoint.Address.Address; - return Create((address << 32) | port); + return new ConnectionId{ + EndPoint = endPoint, + Serial = serial, + }; } public bool Equals(ConnectionId other) { - return this.Id == other.Id; + return this.Serial == other.Serial + && this.EndPoint.Equals(other.EndPoint) + ; } public override bool Equals(object obj) @@ -90,7 +82,10 @@ namespace Hazel.Udp.FewerThreads public override int GetHashCode() { - return this.Id.GetHashCode(); + ///NOTE(mendsley): We're only hashing the endpoint + /// here, as the common case will have one + /// connection per address+port tuple. + return this.EndPoint.GetHashCode(); } } @@ -235,7 +230,7 @@ namespace Hazel.Udp.FewerThreads return; } - ConnectionId connectionId = ConnectionId.CreateFromEndPoint((IPEndPoint)remoteEP); + ConnectionId connectionId = ConnectionId.Create((IPEndPoint)remoteEP, 0); this.ProcessIncomingMessageFromOtherThread(message, (IPEndPoint)remoteEP, connectionId); } }