From 87e45c86ae0ff8eb16504845ec19665d656d4580 Mon Sep 17 00:00:00 2001 From: Forest Date: Tue, 23 Oct 2018 22:48:14 -0700 Subject: [PATCH] Clean up some stuff, make some stuff public for easier statistics. There's a memory leak somewhere, just gotta find it. --- Hazel/Connection.cs | 4 ++++ Hazel/ConnectionListener.cs | 2 +- Hazel/IRecyclable.cs | 2 +- Hazel/MessageReader.cs | 10 +++++----- Hazel/MessageWriter.cs | 6 +++--- Hazel/ObjectPool.cs | 4 +++- Hazel/Udp/UdpConnection.Reliable.cs | 15 +++++++++++++++ Hazel/Udp/UdpConnection.cs | 1 + Hazel/Udp/UdpConnectionListener.cs | 7 ++++++- 9 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index 74e3a22..5bb765f 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -322,6 +322,10 @@ namespace Hazel { if (disposing) { + this.DataReceived = null; + this.DataReceivedRaw = null; + this.DataSentRaw = null; + this.Disconnected = null; } } } diff --git a/Hazel/ConnectionListener.cs b/Hazel/ConnectionListener.cs index b12309e..5aaf6d7 100644 --- a/Hazel/ConnectionListener.cs +++ b/Hazel/ConnectionListener.cs @@ -112,7 +112,7 @@ namespace Hazel /// Are we disposing? protected virtual void Dispose(bool disposing) { - + this.NewConnection = null; } } } diff --git a/Hazel/IRecyclable.cs b/Hazel/IRecyclable.cs index 8310f25..3e9769e 100644 --- a/Hazel/IRecyclable.cs +++ b/Hazel/IRecyclable.cs @@ -9,7 +9,7 @@ namespace Hazel /// Interface for all items that can be returned to an object pool. /// /// - interface IRecyclable + public interface IRecyclable { /// /// Returns this object back to the object pool. diff --git a/Hazel/MessageReader.cs b/Hazel/MessageReader.cs index 7363ddd..49e2e64 100644 --- a/Hazel/MessageReader.cs +++ b/Hazel/MessageReader.cs @@ -7,7 +7,7 @@ namespace Hazel /// public class MessageReader : IRecyclable { - private static readonly ObjectPool objectPool = new ObjectPool(() => new MessageReader()); + public static readonly ObjectPool ReaderPool = new ObjectPool(() => new MessageReader()); public byte[] Buffer; public byte Tag; @@ -31,7 +31,7 @@ namespace Hazel public static MessageReader Get(MessageReader srcMsg) { - var output = objectPool.GetObject(); + var output = ReaderPool.GetObject(); output.Buffer = srcMsg.Buffer; output.Offset = srcMsg.Offset; output.Position = srcMsg.Position; @@ -42,7 +42,7 @@ namespace Hazel public static MessageReader Get(byte[] buffer) { - var output = objectPool.GetObject(); + var output = ReaderPool.GetObject(); output.Buffer = buffer; output.Offset = 0; output.Position = 0; @@ -54,7 +54,7 @@ namespace Hazel public static MessageReader Get(byte[] buffer, int offset) { - var output = objectPool.GetObject(); + var output = ReaderPool.GetObject(); output.Buffer = buffer; output.Offset = offset; output.Position = 0; @@ -83,7 +83,7 @@ namespace Hazel public void Recycle() { this.Position = this.Length = 0; - objectPool.PutObject(this); + ReaderPool.PutObject(this); } #region Read Methods diff --git a/Hazel/MessageWriter.cs b/Hazel/MessageWriter.cs index d9ad298..165c41c 100644 --- a/Hazel/MessageWriter.cs +++ b/Hazel/MessageWriter.cs @@ -9,7 +9,7 @@ namespace Hazel public class MessageWriter : IRecyclable { public static int BufferSize = 64000; - private static readonly ObjectPool objectPool = new ObjectPool(() => new MessageWriter(BufferSize)); + public static readonly ObjectPool WriterPool = new ObjectPool(() => new MessageWriter(BufferSize)); internal byte[] Buffer; public int Length; @@ -60,7 +60,7 @@ namespace Hazel /// The option specifying how the message should be sent. public static MessageWriter Get(SendOption sendOption = SendOption.None) { - var output = objectPool.GetObject(); + var output = WriterPool.GetObject(); output.Clear(sendOption); return output; @@ -123,7 +123,7 @@ namespace Hazel public void Recycle() { this.Position = this.Length = 0; - objectPool.PutObject(this); + WriterPool.PutObject(this); } #region WriteMethods diff --git a/Hazel/ObjectPool.cs b/Hazel/ObjectPool.cs index a4d8d68..c497b3b 100644 --- a/Hazel/ObjectPool.cs +++ b/Hazel/ObjectPool.cs @@ -14,8 +14,10 @@ namespace Hazel /// /// The type that is pooled. /// - sealed class ObjectPool where T : IRecyclable + public sealed class ObjectPool where T : IRecyclable { + public int Size { get { return this.pool.Count; } } + /// /// Our pool of objects /// diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index afd2a21..81663b6 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -419,5 +419,20 @@ namespace Hazel.Udp } catch (InvalidOperationException) { } } + + void DisposeReliablePackets() + { + lock (this.reliableDataPacketsSent) + { + var packets = this.reliableDataPacketsSent.Keys.ToArray(); + foreach (var kvp in this.reliableDataPacketsSent) + { + Packet pkt = kvp.Value; + pkt.Recycle(); + } + + this.reliableDataPacketsSent.Clear(); + } + } } } diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index dfa00ec..ccfde7c 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -288,6 +288,7 @@ namespace Hazel.Udp if (disposing) { DisposeKeepAliveTimer(); + DisposeReliablePackets(); } base.Dispose(disposing); diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 2fcd09d..44410d2 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -190,6 +190,8 @@ namespace Hazel.Udp /// The endpoint to send to. internal void SendData(byte[] bytes, int length, EndPoint endPoint) { + if (length > bytes.Length) return; + try { listener.BeginSendTo( @@ -259,7 +261,8 @@ namespace Hazel.Udp { lock (connections) { - foreach (var kvp in this.connections) + var connects = this.connections.ToArray(); + foreach (var kvp in connects) { if (kvp.Value.State == ConnectionState.Connected) { @@ -269,6 +272,8 @@ namespace Hazel.Udp } catch { } } + + kvp.Value.Dispose(); } connections.Clear(); -- 2.39.5