sockscape/server/Websocket/Stack.cs

66 lines
1.7 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 CircleScape.Websocket {
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 Mutex ClientsMutex = new Mutex();
private bool RunWithNoClients = false;
private bool Running = true;
private bool _finished = false;
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;
}
public bool AddClient(Connection client) {
if(!ClientsMutex.WaitOne(5000))
return false;
Clients.Add(client);
ClientsMutex.ReleaseMutex();
return true;
}
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)) {
for(var i = Count - 1; i >= 0 && Running; ++i) {
PoolRef.OnConnectionParse(Clients[i]);
}
}
2017-04-21 21:04:03 +00:00
Finished = true;
PoolRef.InvalidateThread(this);
2017-04-21 21:04:03 +00:00
}
}
}