#include "MainClient.h" #include #include "Keys.h" #include "ErhaltungClient.h" #include "PatchClient.h" #include "NewsClient.h" #include "RegisterServer.h" #include "LoginClient.h" #include "ShopClient.h" #include "ChatClient.h" #include "AnmeldungClient.h" #include "MinigameClient.h" #include "InformationClient.h" using namespace KSGClient; // Inhalt der MainClient Klasse // Konstruktor MainClient::MainClient() : ReferenceCounter() { port = 0; cId = 0; k = 0; key = 0; keyLen = 0; } MainClient::MainClient(const char* ip, unsigned short port, int klientId, const char* key, unsigned char keyLen) : ReferenceCounter() { this->ip = ip; this->port = port; cId = klientId; k = 0; this->keyLen = keyLen; this->key = new char[keyLen]; memcpy(this->key, key, keyLen); } // Destruktor MainClient::~MainClient() { if (k) disconnect(); delete[]key; } // Meldet den neuen Client beim Serversystem an. Durch diesen Vorgang erhält der Client eine Id und den Port und die Ip Addresse des Main Servers bool MainClient::registerSSL(const char* ip, unsigned short port) { cs.lock(); if (cId) { cs.unlock(); return 0; } Network::SSLKlient* klientSSL = new Network::SSLKlient(); int l = 0; if (!klientSSL->verbinde(port, ip)) { err = "error while trying to connect to Main SSL Server"; klientSSL->release(); cs.unlock(); return 0; } klientSSL->sende("\1", 5); char byte = 0; klientSSL->getNachricht(&byte, 1); if (byte == 3) { klientSSL->getNachricht(&byte, 1); char* message = new char[byte + 1]; message[byte] = 0; klientSSL->getNachricht(message, byte); err = "error while register new client server returned: "; err += message; delete[]message; klientSSL->sende("\3", 1); klientSSL->getNachricht(&byte, 1); klientSSL->trenne(); klientSSL->release(); cs.unlock(); return 0; } if (byte == 1) { klientSSL->getNachricht((char*)&this->port, 4); klientSSL->getNachricht((char*)&cId, 4); klientSSL->getNachricht((char*)&keyLen, 1); this->ip = ip; if (keyLen) { key = new char[keyLen]; klientSSL->getNachricht(key, keyLen); } } klientSSL->sende("\3", 1); klientSSL->getNachricht(&byte, 1); klientSSL->trenne(); klientSSL->release(); cs.unlock(); return 1; } // Verbindet den Client mit dem Server // Wird automatisch aufgerufen, falls eine Methode aufgerufen wird, die eine Verbindung erfordert. In diesem Fall wird die Verbindung auch automatisch wieder getrennt. bool MainClient::connect() { cs.lock(); if (k) { cs.unlock(); return 1; } k = new Network::Klient(); int l = 0; char* key; Keys::getServerKey(&key, l, Keys::MAIN, Keys::SENDEN); k->setSendeKey(key, l); delete[] key; Keys::getServerKey(&key, l, Keys::MAIN, Keys::EMPFANGEN); k->setEmpfangKey(key, l); delete[] key; if (!k->verbinde(port, ip)) { err = "error while trying to connect to Main Server"; k = (Network::Klient*)k->release(); cs.unlock(); return 0; } k->sende("\0", 1); // Verschlüsselung aktivieren k->sendeEncrypted("\1", 1); k->sendeEncrypted((char*)&cId, 4); char serverReturn = 0; k->getNachrichtEncrypted(&serverReturn, 1); if (serverReturn == 3) { k->getNachrichtEncrypted(&serverReturn, 1); char* message = new char[serverReturn + 1]; message[serverReturn] = 0; k->getNachrichtEncrypted(message, serverReturn); err = "error while trying to identify registered client server returned: "; err += message; delete[] message; k->sendeEncrypted("\3", 1); k->getNachrichtEncrypted(&serverReturn, 1); k->trenne(); k = (Network::Klient*)k->release(); cs.unlock(); return 0; } else { k->setSendeKey(this->key, keyLen); k->setEmpfangKey(this->key, keyLen); cs.unlock(); return 1; } } // Erzeugt einen Erhaltungs Server Client // Gibt bei misserfolg 0 zurück ErhaltungServerClient* MainClient::createErhaltungServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\x8", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new ErhaltungClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting ErhaltungServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Patch Server Client // Gibt bei misserfolg 0 zurück PatchServerClient* MainClient::createPatchServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\2", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new PatchClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting PatchServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen News Server Client // Gibt bei misserfolg 0 zurück NewsServerClient* MainClient::createNewsServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\x9", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new NewsClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting NewsServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Register Server Client // Gibt bei misserfolg 0 zurück RegisterServerClient* MainClient::createRegisterServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\1", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new RegisterClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting RegisterServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Login Server Client // Gibt bei misserfolg 0 zurück LoginServerClient* MainClient::createLoginServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\3", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new LoginClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting LoginServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Information Server Client // Gibt bei misserfolg 0 zurück InformationServerClient* MainClient::createInformationServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\4", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new InformationClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting InformationServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Chat Server Client // Gibt bei misserfolg 0 zurück ChatServerClient* MainClient::createChatServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\5", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new ChatClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting ChatServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Shop Server Client // Gibt bei misserfolg 0 zurück ShopServerClient* MainClient::createShopServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\7", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new ShopClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting ShopServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Anmeldung Server Client // Gibt bei misserfolg 0 zurück AnmeldungServerClient* MainClient::createAnmeldungServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\6", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new AnmeldungClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting AnmeldungServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Erzeugt einen Minigame Server Client // Gibt bei misserfolg 0 zurück MinigameServerClient* MainClient::createMinigameServerClient() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\6\xA", 2); char byte = 0; k->getNachrichtEncrypted(&byte, 1); if (byte == 2) { unsigned char lsIp[4]; k->getNachrichtEncrypted((char*)lsIp, 4); unsigned short lsPort = 0; k->getNachrichtEncrypted((char*)&lsPort, 2); Framework::Text ipT; ipT += (int)lsIp[0]; ipT += "."; ipT += (int)lsIp[1]; ipT += "."; ipT += (int)lsIp[2]; ipT += "."; ipT += (int)lsIp[3]; if (!connected) disconnect(); cs.unlock(); return new MinigameClient(cId, lsPort, ipT, key, keyLen); } else if (byte == 3) { k->getNachrichtEncrypted(&byte, 1); char* f = new char[byte + 1]; f[byte] = 0; k->getNachrichtEncrypted(f, byte); err = "error while requesting MinigameServer server returned: "; err += f; delete[]f; } if (!connected) disconnect(); cs.unlock(); return 0; } // Trennt die Verbindung zum Server. Muss nur aufgerufen werden, wenn vorher manuell connect aufgerufen wurde bool MainClient::disconnect() { cs.lock(); if (!k) { cs.unlock(); return 1; } char serverReturn = 0; k->sendeEncrypted("\3", 1); k->getNachrichtEncrypted(&serverReturn, 1); k->trenne(); k = (Network::Klient*)k->release(); cs.unlock(); return 1; } // Meldet den Client vom Server ab. Alle zuvor von diesem Client erzeugten Clients werden durch diesen Vorgang unbrauchbar bool MainClient::unregister() { cs.lock(); bool connected = k != 0; if (!connected) connect(); if (!k) { cs.unlock(); return 0; } k->sendeEncrypted("\7", 1); char serverReturn = 0; k->getNachrichtEncrypted(&serverReturn, 1); if (serverReturn == 3) { k->getNachrichtEncrypted(&serverReturn, 1); char* message = new char[serverReturn + 1]; message[serverReturn] = 0; k->getNachrichtEncrypted(message, serverReturn); err = "error while trying to unregistere client server returned: "; err += message; delete[] message; if (!connected) disconnect(); cs.unlock(); return 0; } disconnect(); cId = 0; delete key; key = 0; keyLen = 0; port = 0; ip = ""; cs.unlock(); return 1; } // Gibt die dem Client zugewiesene Id zurück. // sollte erst nach dem Aufruf von registerSSL verwendet werden. int MainClient::getClientId() const { return cId; } // Gibt die Ip Adresse des dem Client zugewiesenen Main Servers zurück. // sollte erst nach dem Aufruf von registerSSL verwendet werden. const char* MainClient::getServerIp() const { return ip.getText(); } // Gibt den Port des dem Client zugewiesenen Main Servers zurück. // sollte erst nach dem Aufruf von registerSSL verwendet werden. unsigned short MainClient::getServerPort() const { return port; } // Gibt den Zeiger auf den Schlüssel zurück // sollte erst nach dem Aufruf von registerSSL verwendet werden. const char* MainClient::zKey() const { return key; } // Gibt die Länge des Schlüssels zurück // sollte erst nach dem Aufruf von registerSSL verwendet werden. unsigned char MainClient::getKeyLen() const { return keyLen; } // gibt den Letzten Fehlertext zuück // sollte erst aufgerufen werden, nachdem eine andere aufgerufene Methode fehlgeschlagen ist const char* MainClient::getLetzterFehler() const { return err; }