From f458a0b65de5db40ecb57d5831117305fc4dd876 Mon Sep 17 00:00:00 2001 From: Treeki Date: Tue, 21 Jan 2014 01:28:58 +0100 Subject: make a bunch more things private/protected where appropriate --- core.h | 55 +++++++++++++++++++++++++++++----------------- ircserver.cpp | 2 +- mobileclient.cpp | 67 +++++++++++++++++++++++++++----------------------------- 3 files changed, 68 insertions(+), 56 deletions(-) diff --git a/core.h b/core.h index 8e2d317..4037f25 100644 --- a/core.h +++ b/core.h @@ -29,12 +29,16 @@ #define SERVE_VIA_TLS false -struct NetCore; -struct Bouncer; +class NetCore; +class Bouncer; -struct SocketRWCommon { +class SocketRWCommon { +public: static bool setSocketNonBlocking(int fd); // Move me! + friend class NetCore; + +protected: NetCore *netCore; Buffer inputBuf, outputBuf; @@ -52,16 +56,19 @@ struct SocketRWCommon { gnutls_session_t tls; bool tlsActive; +public: SocketRWCommon(NetCore *_netCore); virtual ~SocketRWCommon(); - bool tryTLSHandshake(); virtual void close(); +private: + bool tryTLSHandshake(); + void readAction(); void writeAction(); bool hasTlsPendingData() const; -private: + virtual void processReadBuffer() = 0; }; @@ -85,7 +92,9 @@ struct Packet { Buffer data; }; -struct Client : SocketRWCommon { +class Client : private SocketRWCommon { + friend class NetCore; +private: enum AuthState { AS_LOGIN_WAIT = 0, AS_AUTHED = 1 @@ -98,15 +107,19 @@ struct Client : SocketRWCommon { std::list packetCache; int nextPacketID, lastReceivedPacketID; +public: Client(NetCore *_netCore); ~Client(); - void startService(int _sock, bool withTls); + bool isAuthed() const { return (authState == AS_AUTHED); } + void close(); void sendPacket(Packet::Type type, const Buffer &data, bool allowUnauthed = false); private: + void startService(int _sock, bool withTls); + int readBufPosition; void processReadBuffer(); @@ -118,14 +131,13 @@ private: void clearCachedPackets(int maxID); // Events! -public: - virtual void sessionEndEvent() = 0; -private: virtual void sessionStartEvent() = 0; + virtual void sessionEndEvent() = 0; virtual void packetReceivedEvent(Packet::Type type, Buffer &pkt) = 0; }; -struct MobileClient : Client { +class MobileClient : public Client { +public: Bouncer *bouncer; MobileClient(Bouncer *_bouncer); @@ -138,12 +150,15 @@ private: void handleDebugCommand(char *line, int size); }; -struct Server : SocketRWCommon { +class Server : private SocketRWCommon { + friend class NetCore; + int port; bool useTls; int dnsQueryId; +public: Server(NetCore *_netCore); ~Server(); @@ -151,18 +166,16 @@ protected: void connect(const char *hostname, int _port, bool _useTls); public: - void tryConnectPhase(); - void connectionSuccessful(); - void sendLine(const char *line); // protect me! void close(); private: + void tryConnectPhase(); + void connectionSuccessful(); void processReadBuffer(); -public: - virtual void connectedEvent() = 0; // PRIVATE ME private: + virtual void connectedEvent() = 0; virtual void disconnectedEvent() = 0; virtual void lineReceivedEvent(char *line, int size) = 0; }; @@ -177,8 +190,9 @@ struct IRCNetworkConfig { bool useTls; }; -struct IRCServer : Server { +class IRCServer : public Server { Bouncer *bouncer; +public: IRCNetworkConfig config; IRCServer(Bouncer *_bouncer); @@ -193,7 +207,8 @@ private: }; -struct NetCore { +class NetCore { +public: NetCore(); Client *clients[CLIENT_LIMIT]; @@ -216,7 +231,7 @@ protected: int findServerID(Server *server) const; }; -struct Bouncer : NetCore { +class Bouncer : public NetCore { private: virtual Client *constructClient(); }; diff --git a/ircserver.cpp b/ircserver.cpp index 8ccdbb9..830c82b 100644 --- a/ircserver.cpp +++ b/ircserver.cpp @@ -32,6 +32,6 @@ void IRCServer::lineReceivedEvent(char *line, int size) { Buffer pkt; pkt.writeStr(line, size); for (int i = 0; i < bouncer->clientCount; i++) - if (bouncer->clients[i]->authState == Client::AS_AUTHED) + if (bouncer->clients[i]->isAuthed()) bouncer->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); } diff --git a/mobileclient.cpp b/mobileclient.cpp index c6be33a..bbfac73 100644 --- a/mobileclient.cpp +++ b/mobileclient.cpp @@ -17,52 +17,49 @@ void MobileClient::packetReceivedEvent(Packet::Type type, Buffer &pkt) { handleDebugCommand(cmd, strlen(cmd)); } else { - printf("[fd=%d] Unrecognised packet for MobileClient: type %d, size %d\n", - sock, type, pkt.size()); + printf("[MobileClient:%p] Unrecognised packet for MobileClient: type %d, size %d\n", + this, 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); + if (strncmp(line, "all ", 4) == 0) { + Buffer pkt; + pkt.writeStr(&line[4]); + for (int i = 0; i < bouncer->clientCount; i++) + bouncer->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); - } else if (strcmp(line, "quit") == 0) { - netCore->quitFlag = true; - } else if (strncmp(&line[1], "ddsrv ", 6) == 0) { - IRCServer *srv = new IRCServer(bouncer); - strcpy(srv->config.hostname, &line[7]); - srv->config.useTls = (line[0] == 's'); - srv->config.port = (line[0] == 's') ? 1191 : 6667; - strcpy(srv->config.nickname, "Ninjifox"); - strcpy(srv->config.username, "boop"); - strcpy(srv->config.realname, "boop"); - strcpy(srv->config.password, ""); - bouncer->registerServer(srv); + } else if (strcmp(line, "quit") == 0) { + bouncer->quitFlag = true; + } else if (strncmp(&line[1], "ddsrv ", 6) == 0) { + IRCServer *srv = new IRCServer(bouncer); + strcpy(srv->config.hostname, &line[7]); + srv->config.useTls = (line[0] == 's'); + srv->config.port = (line[0] == 's') ? 1191 : 6667; + strcpy(srv->config.nickname, "Ninjifox"); + strcpy(srv->config.username, "boop"); + strcpy(srv->config.realname, "boop"); + strcpy(srv->config.password, ""); + bouncer->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); + Buffer pkt; + pkt.writeStr("Your wish is my command!"); + for (int i = 0; i < bouncer->clientCount; i++) + bouncer->clients[i]->sendPacket(Packet::B2C_STATUS, pkt); - } else if (strncmp(line, "srvpw", 5) == 0) { - int sid = line[5] - '0'; + } else if (strncmp(line, "srvpw", 5) == 0) { + int sid = line[5] - '0'; // ugly hack, fuck casting, will fix later - strcpy(((IRCServer*)netCore->servers[sid])->config.password, &line[7]); + strcpy(((IRCServer*)bouncer->servers[sid])->config.password, &line[7]); - } else if (strncmp(line, "connsrv", 7) == 0) { - int sid = line[7] - '0'; + } else if (strncmp(line, "connsrv", 7) == 0) { + int sid = line[7] - '0'; // ugly hack, fuck casting, will fix later - ((IRCServer*)netCore->servers[sid])->connect(); - } else if (line[0] >= '0' && line[0] <= '9') { - int sid = line[0] - '0'; - netCore->servers[sid]->sendLine(&line[1]); - } - } else { + ((IRCServer*)bouncer->servers[sid])->connect(); + } else if (line[0] >= '0' && line[0] <= '9') { + int sid = line[0] - '0'; + bouncer->servers[sid]->sendLine(&line[1]); } } -- cgit v1.2.3