i knew it was a lock
ross
This commit is contained in:
parent
e8bf127297
commit
643ac0bba6
4 changed files with 44 additions and 30 deletions
|
@ -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<string, string> Headers =
|
||||
new Dictionary<string, string>(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();
|
||||
|
||||
|
|
|
@ -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<ThreadContext> Threads
|
||||
= new List<ThreadContext>();
|
||||
|
||||
private UInt64 InternalCounter = 0;
|
||||
private Dictionary<UInt64, Connection> Connections
|
||||
= new Dictionary<UInt64, Connection>();
|
||||
|
||||
private List<ThreadContext> InvalidThreads
|
||||
= new List<ThreadContext>();
|
||||
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<T> 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)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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) { }
|
||||
}
|
||||
|
|
|
@ -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) { }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue