sockscape/server/Libraries/Kneesocks/Stack.cs

65 lines
1.8 KiB
C#
Raw Normal View History

2017-04-21 21:04:03 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Kneesocks {
public class Stack<T> where T : Connection {
private Pool<T> PoolRef = null;
2017-04-21 21:04:03 +00:00
private List<Connection> Clients = new List<Connection>();
private bool RunWithNoClients = false;
private bool Running = true;
2017-04-21 21:04:03 +00:00
public Stack(Pool<T> poolRef, Connection initialConnection = null) {
PoolRef = poolRef;
2017-04-21 21:04:03 +00:00
if(initialConnection != null)
Clients.Add(initialConnection);
}
public Stack(Pool<T> poolRef, bool runWithNoClients, Connection initialConnection = null)
: this(poolRef, initialConnection)
2017-04-21 21:04:03 +00:00
{
RunWithNoClients = runWithNoClients;
}
2017-05-05 21:05:52 +00:00
public void AddClient(Connection client) {
lock(Clients) {
Clients.Add(client);
}
2017-04-21 21:04:03 +00:00
}
public int Count {
2017-04-21 21:04:03 +00:00
get {
return Clients.Count;
}
}
public void StopThread() {
Running = false;
}
public bool Finished { get; private set; }
2017-04-21 21:04:03 +00:00
// USED FOR THREADING -- DO NOT CALL
public void ManageStack() {
while(Running && (Count > 0 || RunWithNoClients)) {
2017-05-08 21:06:17 +00:00
for(var i = Count - 1; i >= 0 && Running; --i) {
2017-05-04 12:21:27 +00:00
var client = Clients[i];
if(!client.Disconnected)
client.Parse();
else {
lock(Clients) {
Clients.RemoveAt(i);
}
}
}
}
2017-04-21 21:04:03 +00:00
Finished = true;
PoolRef.InvalidateThread(this);
2017-04-21 21:04:03 +00:00
}
}
}