2017-04-21 21:04:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace CircleScape.Websocket {
|
|
|
|
|
class Pool {
|
|
|
|
|
private const int InitialCount = 3;
|
|
|
|
|
private const int InitialSize = 3;
|
|
|
|
|
private const int SizeGrowth = 1;
|
|
|
|
|
private const int MaxSize = 10;
|
|
|
|
|
|
|
|
|
|
private int _fullThreadCount;
|
|
|
|
|
private bool updateFullThreadCount = true;
|
|
|
|
|
|
|
|
|
|
private List<ThreadContext> Threads
|
|
|
|
|
= new List<ThreadContext>();
|
|
|
|
|
private Dictionary<UInt64, Connection> Connections
|
|
|
|
|
= new Dictionary<UInt64, Connection>();
|
|
|
|
|
|
|
|
|
|
public Pool() {
|
2017-04-23 04:55:29 +00:00
|
|
|
|
for(var i = 0; i < InitialCount; ++i)
|
|
|
|
|
CreateThread();
|
2017-04-21 21:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddConnection(Connection connection) {
|
|
|
|
|
foreach(var thread in Threads) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 04:55:29 +00:00
|
|
|
|
private ThreadContext CreateThread(Connection initialConnection = null, bool runWithNoClients = false) {
|
|
|
|
|
var stack = new Stack(runWithNoClients, initialConnection);
|
2017-04-21 21:04:03 +00:00
|
|
|
|
var ctx = new ThreadContext {
|
|
|
|
|
Stack = stack,
|
|
|
|
|
Thread = new Thread(new ThreadStart(stack.ManageStack))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Threads.Add(ctx);
|
2017-04-23 04:55:29 +00:00
|
|
|
|
updateFullThreadCount = true;
|
2017-04-21 21:04:03 +00:00
|
|
|
|
return ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int FullThreadCount {
|
|
|
|
|
get {
|
|
|
|
|
if(updateFullThreadCount) {
|
|
|
|
|
_fullThreadCount = Math.Min(
|
|
|
|
|
MaxSize,
|
|
|
|
|
InitialSize + SizeGrowth * (Threads.Count - InitialCount)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _fullThreadCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ThreadContext {
|
|
|
|
|
public Thread Thread { get; set; }
|
|
|
|
|
public Stack Stack { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|