login server moved to main thread

This commit is contained in:
2020-08-18 19:52:02 -05:00
parent 47b76b422c
commit b2325eb308
3 changed files with 44 additions and 12 deletions

View File

@@ -234,7 +234,9 @@ CNServer::CNServer(uint16_t p): port(p) {}
void CNServer::start() {
std::cout << "Starting server at *:" << port << std::endl;
// listen to new connections, add to connection list
while (true) {
while (active) {
std::lock_guard<std::mutex> lock(activeCrit);
// listen for a new connection
SOCKET newConnection = accept(sock, (struct sockaddr *)&(address), (socklen_t*)&(addressSize));
if (!SOCKETINVALID(newConnection)) {
@@ -280,4 +282,24 @@ void CNServer::start() {
}
}
void CNServer::kill() {
std::lock_guard<std::mutex> lock(activeCrit); // the lock will be removed when the function ends
active = false;
// kill all connections
std::list<CNSocket*>::iterator i = connections.begin();
while (i != connections.end()) {
CNSocket* cSock = *i;
if (cSock->isAlive()) {
cSock->kill();
}
++i; // go to the next element
delete cSock;
}
connections.clear();
}
void CNServer::killConnection(CNSocket* cns) {} // stubbed lol