From 52b1b88c9ce0f92235963cf1485b49df58cfbe87 Mon Sep 17 00:00:00 2001 From: Treeki Date: Mon, 20 Jan 2014 10:07:32 +0100 Subject: add MobileClient class and move some bits into it --- core.cpp | 114 +++++++++++++++++++++++++++++++++++++-------------------------- core.h | 25 ++++++++++++-- 2 files changed, 90 insertions(+), 49 deletions(-) diff --git a/core.cpp b/core.cpp index b403cae..5697e3b 100644 --- a/core.cpp +++ b/core.cpp @@ -300,43 +300,6 @@ void Client::clearCachedPackets(int maxID) { } -void Client::handleCommand(char *line, int size) { - // This is a terrible mess that will be replaced shortly - if (authState == AS_AUTHED) { - if (strncmp(line, "all ", 4) == 0) { - Buffer pkt; - pkt.writeStr(&line[4]); - for (int i = 0; i < netCore->clientCount; i++) - netCore->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); - - } else if (strcmp(line, "quit") == 0) { - netCore->quitFlag = true; - } else if (strncmp(line, "resolve ", 8) == 0) { - DNS::makeQuery(&line[8]); - } else if (strncmp(&line[1], "ddsrv ", 6) == 0) { - 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!"); - for (int i = 0; i < netCore->clientCount; i++) - netCore->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); - - } else if (strncmp(line, "connsrv", 7) == 0) { - int sid = line[7] - '0'; - netCore->servers[sid]->beginConnect(); - } else if (line[0] >= '0' && line[0] <= '9') { - int sid = line[0] - '0'; - netCore->servers[sid]->outputBuf.append(&line[1], size - 1); - netCore->servers[sid]->outputBuf.append("\r\n", 2); - } - } else { - } -} - void Client::handlePacket(Packet::Type type, char *data, int size) { Buffer pkt; pkt.useExistingBuffer(data, size); @@ -399,6 +362,8 @@ void Client::handlePacket(Packet::Type type, char *data, int size) { Buffer pkt; pkt.append((char *)sessionKey, SESSION_KEY_SIZE); sendPacket(Packet::B2C_OOB_LOGIN_SUCCESS, pkt); + + sessionStartEvent(); } } else { @@ -406,15 +371,7 @@ void Client::handlePacket(Packet::Type type, char *data, int size) { sock, type, size); } } else if (authState == AS_AUTHED) { - if (type == Packet::C2B_COMMAND) { - char cmd[2048]; - pkt.readStr(cmd, sizeof(cmd)); - handleCommand(cmd, strlen(cmd)); - - } else { - printf("[fd=%d] Unrecognised packet in AS_AUTHED authstate: type %d, size %d\n", - sock, type, size); - } + packetReceivedEvent(type, pkt); } } @@ -540,6 +497,66 @@ void Client::sendPacketOverWire(const Packet *packet) { +MobileClient::MobileClient(Bouncer *_bouncer) : Client(_bouncer) { +} + +void MobileClient::sessionStartEvent() { + printf("{Session started}\n"); +} +void MobileClient::sessionEndEvent() { + printf("{Session ended}\n"); +} +void MobileClient::packetReceivedEvent(Packet::Type type, Buffer &pkt) { + if (type == Packet::C2B_COMMAND) { + char cmd[2048]; + pkt.readStr(cmd, sizeof(cmd)); + handleDebugCommand(cmd, strlen(cmd)); + + } else { + printf("[fd=%d] Unrecognised packet for MobileClient: type %d, size %d\n", + sock, type, pkt.size()); + } +} + +void MobileClient::handleDebugCommand(char *line, int size) { + // This is a terrible mess that will be replaced shortly + if (authState == AS_AUTHED) { + if (strncmp(line, "all ", 4) == 0) { + Buffer pkt; + pkt.writeStr(&line[4]); + for (int i = 0; i < netCore->clientCount; i++) + netCore->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); + + } else if (strcmp(line, "quit") == 0) { + netCore->quitFlag = true; + } else if (strncmp(line, "resolve ", 8) == 0) { + DNS::makeQuery(&line[8]); + } else if (strncmp(&line[1], "ddsrv ", 6) == 0) { + 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!"); + for (int i = 0; i < netCore->clientCount; i++) + netCore->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); + + } else if (strncmp(line, "connsrv", 7) == 0) { + int sid = line[7] - '0'; + netCore->servers[sid]->beginConnect(); + } else if (line[0] >= '0' && line[0] <= '9') { + int sid = line[0] - '0'; + netCore->servers[sid]->outputBuf.append(&line[1], size - 1); + netCore->servers[sid]->outputBuf.append("\r\n", 2); + } + } else { + } +} + + + Server::Server(NetCore *_netCore) : SocketRWCommon(_netCore) { dnsQueryId = -1; ircUseTls = false; @@ -832,6 +849,9 @@ int NetCore::execute() { // Yep. Client *client = clients[i]; + if (client->authState == Client::AS_AUTHED) + client->sessionEndEvent(); + delete client; // If this is the last socket in the list, we can just // decrement clientCount and all will be fine. @@ -963,7 +983,7 @@ int NetCore::execute() { Client *Bouncer::constructClient() { - return new Client(this); + return new MobileClient(this); } diff --git a/core.h b/core.h index ef1ef0d..1032a06 100644 --- a/core.h +++ b/core.h @@ -15,6 +15,7 @@ #define SERVE_VIA_TLS false struct NetCore; +struct Bouncer; struct SocketRWCommon { NetCore *netCore; @@ -91,13 +92,33 @@ struct Client : SocketRWCommon { private: int readBufPosition; void processReadBuffer(); - void handlePacket(Packet::Type type, char *data, int size); - void handleCommand(char *line, int size); void generateSessionKey(); void resumeSession(Client *other, int lastReceivedByClient); + + void handlePacket(Packet::Type type, char *data, int size); void sendPacketOverWire(const Packet *packet); void clearCachedPackets(int maxID); + + // Events! +public: + virtual void sessionEndEvent() = 0; +private: + virtual void sessionStartEvent() = 0; + virtual void packetReceivedEvent(Packet::Type type, Buffer &pkt) = 0; +}; + +struct MobileClient : Client { + Bouncer *bouncer; + + MobileClient(Bouncer *_bouncer); + +private: + virtual void sessionStartEvent(); + virtual void sessionEndEvent(); + virtual void packetReceivedEvent(Packet::Type type, Buffer &pkt); + + void handleDebugCommand(char *line, int size); }; struct Server : SocketRWCommon { -- cgit v1.2.3