From 0c114045d4a0b16db8c00e710708d7d20a54bf6a Mon Sep 17 00:00:00 2001 From: Treeki Date: Mon, 20 Jan 2014 09:53:20 +0100 Subject: add server management functions to NetCore --- core.cpp | 34 ++++++++++++++++++++++++++++------ core.h | 6 ++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/core.cpp b/core.cpp index fc54013..b403cae 100644 --- a/core.cpp +++ b/core.cpp @@ -314,12 +314,11 @@ void Client::handleCommand(char *line, int size) { } else if (strncmp(line, "resolve ", 8) == 0) { DNS::makeQuery(&line[8]); } else if (strncmp(&line[1], "ddsrv ", 6) == 0) { - int srvID = netCore->serverCount; - netCore->servers[srvID] = new Server(netCore); - strcpy(netCore->servers[srvID]->ircHostname, &line[7]); - netCore->servers[srvID]->ircPort = 1191; - netCore->servers[srvID]->ircUseTls = (line[0] == 's'); - netCore->serverCount++; + Server *srv = new Server(netCore); + strcpy(srv->ircHostname, &line[7]); + srv->ircPort = 1191; + srv->ircUseTls = (line[0] == 's'); + netCore->registerServer(srv); Buffer pkt; pkt.writeStr("Your wish is my command!"); @@ -742,6 +741,29 @@ Client *NetCore::findClientWithSessionKey(uint8_t *key) const { return 0; } +int NetCore::registerServer(Server *server) { + if (serverCount >= SERVER_LIMIT) + return -1; + + int id = serverCount++; + servers[id] = server; + return id; +} +void NetCore::deregisterServer(int id) { + Server *server = servers[id]; + server->close(); + delete server; + + serverCount--; + servers[id] = servers[serverCount]; +} +int NetCore::findServerID(Server *server) const { + for (int i = 0; i < SERVER_LIMIT; i++) + if (servers[i] == server) + return i; + return -1; +} + int NetCore::execute() { // prepare the listen socket int listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); diff --git a/core.h b/core.h index e259d7a..ef1ef0d 100644 --- a/core.h +++ b/core.h @@ -136,6 +136,12 @@ struct NetCore { Client *findClientWithSessionKey(uint8_t *key) const; private: virtual Client *constructClient() = 0; + +public: + int registerServer(Server *server); // THIS FUNCTION WILL BE PROTECTED LATER +protected: + void deregisterServer(int id); + int findServerID(Server *server) const; }; struct Bouncer : NetCore { -- cgit v1.2.3