login server moved to main thread

This commit is contained in:
CPunch 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() { void CNServer::start() {
std::cout << "Starting server at *:" << port << std::endl; std::cout << "Starting server at *:" << port << std::endl;
// listen to new connections, add to connection list // listen to new connections, add to connection list
while (true) { while (active) {
std::lock_guard<std::mutex> lock(activeCrit);
// listen for a new connection // listen for a new connection
SOCKET newConnection = accept(sock, (struct sockaddr *)&(address), (socklen_t*)&(addressSize)); SOCKET newConnection = accept(sock, (struct sockaddr *)&(address), (socklen_t*)&(addressSize));
if (!SOCKETINVALID(newConnection)) { 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 void CNServer::killConnection(CNSocket* cns) {} // stubbed lol

View File

@ -39,6 +39,12 @@
#include <list> #include <list>
#include <queue> #include <queue>
#ifdef __MINGW32__
#include "mingw/mingw.mutex.h"
#else
#include <mutex>
#endif
/* /*
Packets format (sent from the client): Packets format (sent from the client):
[4 bytes] - size of packet including the 4 byte packet type [4 bytes] - size of packet including the 4 byte packet type
@ -125,6 +131,9 @@ protected:
struct sockaddr_in address; struct sockaddr_in address;
void init(); void init();
bool active = true;
std::mutex activeCrit;
public: public:
PacketHandler pHandler; PacketHandler pHandler;
@ -132,6 +141,7 @@ public:
CNServer(uint16_t p); CNServer(uint16_t p);
void start(); void start();
void kill();
virtual void killConnection(CNSocket* cns); virtual void killConnection(CNSocket* cns);
}; };

View File

@ -12,14 +12,8 @@
#endif #endif
#include <string> #include <string>
void startLogin(uint16_t port) { void startShard(CNShardServer* server) {
CNLoginServer server(port); server->start();
server.start();
}
void startShard(uint16_t port) {
CNShardServer server(port);
server.start();
} }
int main() { int main() {
@ -36,9 +30,15 @@ int main() {
ChatManager::init(); ChatManager::init();
std::cout << "[INFO] Starting Server Threads..." << std::endl; std::cout << "[INFO] Starting Server Threads..." << std::endl;
std::thread loginThread(startLogin, settings::LOGINPORT); CNShardServer loginServer(settings::LOGINPORT);
std::thread shardThread(startShard, settings::SHARDPORT); CNLoginServer shardServer(settings::SHARDPORT);
getchar(); // blocks until input
std::thread shardThread(startShard, (CNShardServer*)&shardServer);
loginServer.start();
shardServer.kill();
shardThread.join();
#ifdef _WIN32 #ifdef _WIN32
WSACleanup(); WSACleanup();