Server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "Server.h"
  2. #include <AsynchronCall.h>
  3. #include <Globals.h>
  4. #include <HttpRequest.h>
  5. #include <iostream>
  6. #include <JSON.h>
  7. #include <Klient.h>
  8. // Inhalt der LoginServer Klasse aus LoginServer.h
  9. // Konstruktor
  10. FactoryCraftServer::FactoryCraftServer(InitDatei* zIni)
  11. : ReferenceCounter()
  12. {
  13. Network::Start(100);
  14. runningThreads = 0;
  15. klients = new RCArray<FCKlient>();
  16. ini = dynamic_cast<InitDatei*>(zIni->getThis());
  17. id = (int)*zIni->zWert("ServerId");
  18. sslServer = new SSLServer();
  19. sslServer->setPrivateKeyPassword(zIni->zWert("SSLPasswort")->getText());
  20. sslServer->setCertificateFile(zIni->zWert("SSLCert")->getText());
  21. std::cout << "using cert file " << zIni->zWert("SSLCert")->getText()
  22. << "\n";
  23. sslServer->setPrivateKeyFile(zIni->zWert("SSLKey")->getText());
  24. std::cout << "using private key " << zIni->zWert("SSLKey")->getText()
  25. << "\n";
  26. server = new Server();
  27. std::cout << "Server Port: " << ini->zWert("Port")->getText() << "\n";
  28. if (!server->verbinde(
  29. (unsigned short)TextZuInt(ini->zWert("Port")->getText(), 10), 10))
  30. {
  31. std::cout << "Der Server konnte nicht gestartet werden.\n";
  32. exit(1);
  33. }
  34. std::cout << "SSL Server Port: " << ini->zWert("SSLPort")->getText()
  35. << "\n";
  36. if (!sslServer->verbinde(
  37. (unsigned short)TextZuInt(ini->zWert("SSLPort")->getText(), 10),
  38. 10))
  39. {
  40. std::cout << "Der SSL Server konnte nicht gestartet werden.\n";
  41. exit(2);
  42. }
  43. Game::initialize(
  44. zIni->zWert("World")->getText(), zIni->zWert("SaveDir")->getText());
  45. new Framework::AsynchronCall("Server", [this]() {
  46. runningThreads++;
  47. while (server->isConnected())
  48. {
  49. SKlient* klient = server->getKlient();
  50. if (!klient) continue;
  51. unsigned short len;
  52. klient->setEmpfangTimeout(5000);
  53. klient->getNachricht((char*)&len, 2);
  54. char* key = new char[len];
  55. klient->getNachricht((char*)key, len);
  56. bool bg;
  57. klient->getNachricht((char*)&bg, 1);
  58. klient->setEmpfangTimeout(0);
  59. bool found = 0;
  60. EnterCriticalSection(&cs);
  61. for (FCKlient* client : *klients)
  62. {
  63. if (client->matchAuthKey(key, len))
  64. {
  65. if (bg)
  66. client->setBackgroundClient(klient);
  67. else
  68. client->setForegroundClient(klient);
  69. found = 1;
  70. break;
  71. }
  72. }
  73. LeaveCriticalSection(&cs);
  74. if (!found) klient->release();
  75. }
  76. runningThreads--;
  77. });
  78. InitializeCriticalSection(&cs);
  79. }
  80. // Destruktor
  81. FactoryCraftServer::~FactoryCraftServer()
  82. {
  83. sslServer->trenne();
  84. server->trenne();
  85. while (runningThreads > 0)
  86. Sleep(100);
  87. sslServer->release();
  88. server->release();
  89. if (klients) klients->release();
  90. ini->release();
  91. Game::INSTANCE->requestStop();
  92. Game::INSTANCE->release();
  93. DeleteCriticalSection(&cs);
  94. }
  95. // nicht constant
  96. void FactoryCraftServer::run()
  97. {
  98. runningThreads++;
  99. while (sslServer->isConnected())
  100. {
  101. SSLSKlient* klient = sslServer->getKlient();
  102. if (!klient) continue;
  103. Framework::getThreadRegister()->cleanUpClosedThreads();
  104. FCKlient* clHandle = new FCKlient(
  105. klient, dynamic_cast<FactoryCraftServer*>(getThis()));
  106. EnterCriticalSection(&cs);
  107. klients->add(clHandle);
  108. LeaveCriticalSection(&cs);
  109. clHandle->start();
  110. }
  111. runningThreads--;
  112. }
  113. void FactoryCraftServer::close()
  114. {
  115. sslServer->trenne();
  116. EnterCriticalSection(&cs);
  117. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  118. klients->z(i)->absturz();
  119. klients = (RCArray<FCKlient>*)klients->release();
  120. Game::INSTANCE->save();
  121. LeaveCriticalSection(&cs);
  122. }
  123. bool FactoryCraftServer::absturzKlient(int accountId)
  124. {
  125. bool gefunden = 0;
  126. EnterCriticalSection(&cs);
  127. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  128. {
  129. if (klients->z(i) && klients->z(i)->getAccountId() == accountId)
  130. {
  131. klients->z(i)->absturz();
  132. klients->remove(i);
  133. gefunden = 1;
  134. break;
  135. }
  136. }
  137. LeaveCriticalSection(&cs);
  138. return gefunden;
  139. }
  140. bool FactoryCraftServer::removeKlient(FCKlient* zKlient)
  141. {
  142. bool gefunden = 0;
  143. EnterCriticalSection(&cs);
  144. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  145. {
  146. if (klients->z(i) == zKlient)
  147. {
  148. klients->remove(i);
  149. gefunden = 1;
  150. break;
  151. }
  152. }
  153. LeaveCriticalSection(&cs);
  154. return gefunden;
  155. }
  156. bool FactoryCraftServer::hatClients() const
  157. {
  158. return klients->hat(0);
  159. }
  160. int FactoryCraftServer::getUnencryptedPort() const
  161. {
  162. return server->getPort();
  163. }
  164. // Inhalt der LSKlient aus LoginServer.h
  165. // Konstruktor
  166. FCKlient::FCKlient(SSLSKlient* klient, FactoryCraftServer* ls)
  167. : Thread()
  168. {
  169. this->klient = klient;
  170. background = 0;
  171. foreground = 0;
  172. accountId = 0;
  173. this->ls = ls;
  174. zGameClient = 0;
  175. backgroundReader = 0;
  176. foregroundReader = 0;
  177. backgroundWriter = 0;
  178. foregroundWriter = 0;
  179. authKey = randomKey(authKeyLen);
  180. }
  181. // Destruktor
  182. FCKlient::~FCKlient()
  183. {
  184. if (zGameClient)
  185. {
  186. zGameClient->logout();
  187. zGameClient = (GameClient*)zGameClient->release();
  188. }
  189. if (background) background->release();
  190. if (foreground) foreground->release();
  191. delete backgroundReader;
  192. delete foregroundReader;
  193. delete backgroundWriter;
  194. delete foregroundWriter;
  195. klient->release();
  196. ls->release();
  197. delete[] authKey;
  198. }
  199. // nicht constant
  200. void FCKlient::setForegroundClient(SKlient* foreground)
  201. {
  202. this->foreground = foreground;
  203. foregroundReader = new NetworkReader(foreground);
  204. foregroundWriter = new NetworkWriter(foreground);
  205. if (foreground && background)
  206. zGameClient
  207. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()),
  208. Framework::Text((int)accountId));
  209. new AsynchronCall([this]() {
  210. while (this->foreground->waitForNextMessage())
  211. {
  212. if (zGameClient) zGameClient->addMessage(foregroundReader);
  213. if (!zGameClient) Sleep(100);
  214. }
  215. zGameClient->logout();
  216. ls->removeKlient(this);
  217. });
  218. }
  219. void FCKlient::setBackgroundClient(SKlient* background)
  220. {
  221. this->background = background;
  222. backgroundReader = new NetworkReader(background);
  223. backgroundWriter = new NetworkWriter(background);
  224. if (foreground && background)
  225. zGameClient
  226. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()),
  227. Framework::Text((int)accountId));
  228. new AsynchronCall([this]() {
  229. while (this->background->waitForNextMessage())
  230. {
  231. if (zGameClient) zGameClient->addMessage(backgroundReader);
  232. if (!zGameClient) Sleep(100);
  233. }
  234. zGameClient->logout();
  235. ls->removeKlient(this);
  236. });
  237. }
  238. void FCKlient::absturz()
  239. {
  240. ende();
  241. klient->trenne();
  242. }
  243. void FCKlient::thread()
  244. {
  245. while (1)
  246. {
  247. char c = 0;
  248. if (!klient->getNachricht(&c, 1))
  249. break;
  250. else
  251. {
  252. bool br = 0;
  253. switch (c)
  254. {
  255. case 1: // Klient identifikation
  256. {
  257. char len;
  258. klient->getNachricht(&len, 1);
  259. char* name = new char[len + 1];
  260. klient->getNachricht(name, len);
  261. name[(int)len] = 0;
  262. unsigned short sLen;
  263. klient->getNachricht((char*)&sLen, 2);
  264. char* secret = new char[sLen + 1];
  265. klient->getNachricht(secret, sLen);
  266. secret[sLen] = 0;
  267. if (!Game::INSTANCE->checkPlayer(name, secret))
  268. {
  269. klient->sende("\0", 1);
  270. delete[] name;
  271. delete[] secret;
  272. break;
  273. }
  274. if (!Game::INSTANCE->existsPlayer(name))
  275. {
  276. Text secret = Game::INSTANCE->createPlayer(name);
  277. klient->sende("\2", 1);
  278. short len = (short)secret.getLength();
  279. klient->sende((char*)&len, 2);
  280. klient->sende(secret.getText(), len);
  281. }
  282. else
  283. {
  284. klient->sende("\1", 1);
  285. }
  286. klient->sende((char*)&authKeyLen, 4);
  287. klient->sende(authKey, authKeyLen);
  288. delete[] name;
  289. delete[] secret;
  290. break;
  291. }
  292. case 2: // Verbindungsende
  293. br = 1;
  294. if (zGameClient)
  295. {
  296. zGameClient->logout();
  297. zGameClient = (GameClient*)zGameClient->release();
  298. }
  299. klient->sende("\1", 1);
  300. break;
  301. case 3: // ping
  302. klient->sende("\1", 1);
  303. break;
  304. case 4: // check player name valid
  305. {
  306. klient->sende("\1", 1);
  307. char len;
  308. klient->getNachricht(&len, 1);
  309. char* name = new char[len + 1];
  310. klient->getNachricht(name, len);
  311. name[(int)len] = 0;
  312. short sLen;
  313. klient->getNachricht((char*)&sLen, 2);
  314. char* secret = new char[sLen + 1];
  315. klient->getNachricht(secret, sLen);
  316. secret[sLen] = 0;
  317. char res = 0;
  318. if (Game::INSTANCE->checkPlayer(name, secret)) res = 1;
  319. klient->sende(&res, 1);
  320. delete[] name;
  321. delete[] secret;
  322. break;
  323. }
  324. default:
  325. br = 1;
  326. break;
  327. }
  328. if (br) break;
  329. }
  330. }
  331. }
  332. int FCKlient::getAccountId() const // gibt die KlientId zurück
  333. {
  334. return accountId;
  335. }
  336. NetworkWriter* FCKlient::zBackgroundWriter() const
  337. {
  338. return backgroundWriter;
  339. }
  340. NetworkWriter* FCKlient::zForegroundWriter() const
  341. {
  342. return foregroundWriter;
  343. }
  344. bool FCKlient::matchAuthKey(char* key, int len) const
  345. {
  346. if (foreground && background) return 0;
  347. if (len != authKeyLen) return 0;
  348. for (int i = 0; i < len; i++)
  349. {
  350. if (key[i] != authKey[i]) return 0;
  351. }
  352. return 1;
  353. }