From 2d9a1a1afa6cc99860e458ceacbe5c04dfa1312d Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:29:40 +0300 Subject: [PATCH 1/7] Fix copy-paste error in NetConnectionStatistics. --- Lidgren.Network/NetConnectionStatistics.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lidgren.Network/NetConnectionStatistics.cs b/Lidgren.Network/NetConnectionStatistics.cs index 1affbc2..c0756f7 100644 --- a/Lidgren.Network/NetConnectionStatistics.cs +++ b/Lidgren.Network/NetConnectionStatistics.cs @@ -171,7 +171,7 @@ namespace Lidgren.Network if (m_resentMessagesDueToDelay > 0) bdr.AppendLine("Resent messages (delay): " + m_resentMessagesDueToDelay); - if (m_resentMessagesDueToDelay > 0) + if (m_resentMessagesDueToHole > 0) bdr.AppendLine("Resent messages (holes): " + m_resentMessagesDueToHole); int numUnsent = 0; From d8ff2c9d31736c3146a1edb97383b9f4ddb4c55f Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:32:37 +0300 Subject: [PATCH 2/7] Fix possible NullArgumentException throw by using a null return. --- Lidgren.Network/NetUtility.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lidgren.Network/NetUtility.cs b/Lidgren.Network/NetUtility.cs index a8bafd2..a81ae09 100644 --- a/Lidgren.Network/NetUtility.cs +++ b/Lidgren.Network/NetUtility.cs @@ -72,7 +72,7 @@ namespace Lidgren.Network public static NetEndPoint Resolve(string ipOrHost, int port) { var adr = Resolve(ipOrHost); - return new NetEndPoint(adr, port); + return adr == null ? null : new NetEndPoint(adr, port); } private static IPAddress s_broadcastAddress; From 30b58c20986991a1b02503c8473378f37f2b49f6 Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:36:17 +0300 Subject: [PATCH 3/7] Fix possible leak in NetUPnP.SOAPRequest. --- Lidgren.Network/NetUPnP.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Lidgren.Network/NetUPnP.cs b/Lidgren.Network/NetUPnP.cs index b983a66..891d685 100644 --- a/Lidgren.Network/NetUPnP.cs +++ b/Lidgren.Network/NetUPnP.cs @@ -263,11 +263,12 @@ namespace Lidgren.Network r.ContentType = "text/xml; charset=\"utf-8\""; r.ContentLength = b.Length; r.GetRequestStream().Write(b, 0, b.Length); - XmlDocument resp = new XmlDocument(); - WebResponse wres = r.GetResponse(); - Stream ress = wres.GetResponseStream(); - resp.Load(ress); - return resp; + using (WebResponse wres = r.GetResponse()) { + XmlDocument resp = new XmlDocument(); + Stream ress = wres.GetResponseStream(); + resp.Load(ress); + return resp; + } } } } \ No newline at end of file From afbe41bb6d35ec5d2e97398bbbd71cbd5ad5b76f Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:40:34 +0300 Subject: [PATCH 4/7] Fix possible leak in NetUPnP.ExtractServiceUrl. --- Lidgren.Network/NetUPnP.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lidgren.Network/NetUPnP.cs b/Lidgren.Network/NetUPnP.cs index 891d685..217602f 100644 --- a/Lidgren.Network/NetUPnP.cs +++ b/Lidgren.Network/NetUPnP.cs @@ -97,7 +97,9 @@ namespace Lidgren.Network { #endif XmlDocument desc = new XmlDocument(); - desc.Load(WebRequest.Create(resp).GetResponse().GetResponseStream()); + using (var response = WebRequest.Create(resp).GetResponse()) + desc.Load(response.GetResponseStream()); + XmlNamespaceManager nsMgr = new XmlNamespaceManager(desc.NameTable); nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0"); XmlNode typen = desc.SelectSingleNode("//tns:device/tns:deviceType/text()", nsMgr); From 0f9ca1d106520db6ec15fbea8bf03574c5dd2ce6 Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:48:39 +0300 Subject: [PATCH 5/7] Fix data race in Capacity/Count of NetQueue. --- Lidgren.Network/NetQueue.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Lidgren.Network/NetQueue.cs b/Lidgren.Network/NetQueue.cs index 475bc37..25a015b 100644 --- a/Lidgren.Network/NetQueue.cs +++ b/Lidgren.Network/NetQueue.cs @@ -56,12 +56,29 @@ namespace Lidgren.Network /// /// Gets the number of items in the queue /// - public int Count { get { return m_size; } } + public int Count { + get + { + m_lock.EnterReadLock(); + int count = m_size; + m_lock.ExitReadLock(); + return count; + } + } /// /// Gets the current capacity for the queue /// - public int Capacity { get { return m_items.Length; } } + public int Capacity + { + get + { + m_lock.EnterReadLock(); + int capacity = m_items.Length; + m_lock.ExitReadLock(); + return capacity; + } + } /// /// NetQueue constructor From 2d1f9a4b914936bafb66fc610fbe88c33d9dec2e Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:52:23 +0300 Subject: [PATCH 6/7] Fix data race in NetPeerStatistics.BytesInRecyclePool --- Lidgren.Network/NetPeerStatistics.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lidgren.Network/NetPeerStatistics.cs b/Lidgren.Network/NetPeerStatistics.cs index 841bf0f..093bd5c 100644 --- a/Lidgren.Network/NetPeerStatistics.cs +++ b/Lidgren.Network/NetPeerStatistics.cs @@ -105,7 +105,14 @@ namespace Lidgren.Network /// /// Gets the number of bytes in the recycled pool /// - public int BytesInRecyclePool { get { return m_peer.m_storagePoolBytes; } } + public int BytesInRecyclePool + { + get + { + lock (m_peer.m_storagePool) + return m_peer.m_storagePoolBytes; + } + } #if !USE_RELEASE_STATISTICS [Conditional("DEBUG")] From 15a9a97ffb439bce0b177f3741666e614d929789 Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Sat, 5 Sep 2015 10:56:17 +0300 Subject: [PATCH 7/7] Refactor NetPeer.Connect code to throw on non-resolvable address. --- Lidgren.Network/NetPeer.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Lidgren.Network/NetPeer.cs b/Lidgren.Network/NetPeer.cs index 8590e8e..3e59e2b 100644 --- a/Lidgren.Network/NetPeer.cs +++ b/Lidgren.Network/NetPeer.cs @@ -244,12 +244,20 @@ namespace Lidgren.Network Recycle(msg); } + static NetEndPoint GetNetEndPoint(string host, int port) + { + IPAddress address = NetUtility.Resolve(host); + if (address == null) + throw new NetException("Could not resolve host"); + return new NetEndPoint(address, port); + } + /// /// Create a connection to a remote endpoint /// public NetConnection Connect(string host, int port) { - return Connect(new NetEndPoint(NetUtility.Resolve(host), port), null); + return Connect(GetNetEndPoint(host, port), null); } /// @@ -257,7 +265,7 @@ namespace Lidgren.Network /// public NetConnection Connect(string host, int port, NetOutgoingMessage hailMessage) { - return Connect(new NetEndPoint(NetUtility.Resolve(host), port), hailMessage); + return Connect(GetNetEndPoint(host, port), hailMessage); } ///