123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- #include "MainClient.h"
- #include <Klient.h>
- #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;
- }
|