From 643ac0bba6de433a10de81c1900ba4c7f12dc270 Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Wed, 3 May 2017 16:10:50 -0500 Subject: [PATCH] i knew it was a lock ross --- server/Kneesocks/Connection.cs | 7 ++-- server/Kneesocks/Pool.cs | 58 +++++++++++++++++++------------ server/Socks/ActiveConnection.cs | 5 ++- server/Socks/PendingConnection.cs | 4 +-- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/server/Kneesocks/Connection.cs b/server/Kneesocks/Connection.cs index d2ba9e1..411e6b3 100644 --- a/server/Kneesocks/Connection.cs +++ b/server/Kneesocks/Connection.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; namespace Kneesocks { public abstract class Connection { + public UInt64 Id { get; private set; } private TcpClient Socket; private NetworkStream Stream; @@ -18,13 +19,15 @@ namespace Kneesocks { private Dictionary Headers = new Dictionary(StringComparer.OrdinalIgnoreCase); - public Connection(TcpClient sock) { + protected Connection(UInt64 id, TcpClient sock) { + Id = id; Socket = sock; Socket.ReceiveTimeout = 1; Stream = sock.GetStream(); } - public Connection(Connection conn) { + protected Connection(Connection conn) { + Id = conn.Id; Socket = conn.Socket; Stream = Socket.GetStream(); diff --git a/server/Kneesocks/Pool.cs b/server/Kneesocks/Pool.cs index 6895c76..a52087b 100644 --- a/server/Kneesocks/Pool.cs +++ b/server/Kneesocks/Pool.cs @@ -15,6 +15,7 @@ namespace Kneesocks { // a new thread is created public int SizeGrowth { get; set; } = 1; // maximum amount of connections that a single thread will be assigned + // 0 means no limit public int MaxSize { get; set; } = 10; // maximum number of threads that will be spawned // 0 means no limit @@ -30,46 +31,57 @@ namespace Kneesocks { private List Threads = new List(); + + private UInt64 InternalCounter = 0; private Dictionary Connections = new Dictionary(); - private List InvalidThreads - = new List(); - private Mutex InvalidThreadsMutex = new Mutex(); - public Pool() { for(var i = 0; i < InitialCount; ++i) CreateThread(); } - public bool AddConnection(Connection connection) { - if(InvalidThreads.Count > 0) { - foreach(var invalidThread in InvalidThreads) - Threads.RemoveAll(x => Object.ReferenceEquals(invalidThread, x)); + private void IndexConnection(UInt64 id, Connection connection) { + lock(Connections) { + if(id == 0) + id = InternalCounter++; - updateFullThreadCount = true; - InvalidThreads.RemoveAll(x => true); + Connections.Add(id, connection); } + } - foreach(var thread in Threads) { - if(thread.Stack.Count < FullThreadSize) { - thread.Stack.AddClient(connection); + public bool AddConnection(Connection connection, UInt64 id = 0) { + lock(Threads) { + foreach(var thread in Threads) { + if(thread.Stack.Count < FullThreadSize) { + thread.Stack.AddClient(connection); + return true; + } + } + + if(MaxCount == 0 || Threads.Count < MaxCount) { + CreateThread(connection); return true; } } - if(MaxCount == 0 || Threads.Count < MaxCount) { - CreateThread(connection); - return true; - } - return false; } + public void InvalidateConnection(Connection connection) { + lock(Connections) { + + } + } + public void InvalidateThread(Stack stackRef) { - var ctx = Threads.FirstOrDefault(x => Object.ReferenceEquals(x.Stack, stackRef)); - if(ctx != null) - InvalidThreads.Add(ctx); + lock(Threads) { + var ctx = Threads.FirstOrDefault(x => Object.ReferenceEquals(x.Stack, stackRef)); + if(ctx != null) { + Threads.Remove(ctx); + updateFullThreadCount = true; + } + } } private ThreadContext CreateThread(Connection initialConnection = null, bool runWithNoClients = false) { @@ -78,7 +90,7 @@ namespace Kneesocks { Stack = stack, Thread = new Thread(new ThreadStart(stack.ManageStack)) }; - + Threads.Add(ctx); updateFullThreadCount = true; return ctx; @@ -88,7 +100,7 @@ namespace Kneesocks { get { if(updateFullThreadCount) { _fullThreadCount = Math.Min( - MaxSize, + MaxSize == 0 ? int.MaxValue : MaxSize, InitialSize + SizeGrowth * (Threads.Count - InitialCount) ); } diff --git a/server/Socks/ActiveConnection.cs b/server/Socks/ActiveConnection.cs index 588f9a2..9ed4340 100644 --- a/server/Socks/ActiveConnection.cs +++ b/server/Socks/ActiveConnection.cs @@ -4,11 +4,10 @@ using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; -using Kneesocks; namespace CircleScape { - class ActiveConnection : Connection { - public ActiveConnection(TcpClient sock) : base(sock) { } + class ActiveConnection : Kneesocks.Connection { + public ActiveConnection(UInt32 id, TcpClient sock) : base(id, sock) { } public ActiveConnection(PendingConnection conn) : base(conn) { } } diff --git a/server/Socks/PendingConnection.cs b/server/Socks/PendingConnection.cs index cbac48e..99ed41b 100644 --- a/server/Socks/PendingConnection.cs +++ b/server/Socks/PendingConnection.cs @@ -6,7 +6,7 @@ using System.Text; using System.Threading.Tasks; namespace CircleScape { - class PendingConnection : ActiveConnection { - public PendingConnection(TcpClient sock) : base(sock) { } + class PendingConnection : Kneesocks.Connection { + public PendingConnection(UInt32 id, TcpClient sock) : base(id, sock) { } } }