Server.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. {
  67. klient->sende("\1", 1);
  68. client->setBackgroundClient(klient);
  69. }
  70. else
  71. {
  72. klient->sende("\1", 1);
  73. client->setForegroundClient(klient);
  74. }
  75. found = 1;
  76. break;
  77. }
  78. }
  79. LeaveCriticalSection(&cs);
  80. if (!found)
  81. {
  82. klient->sende("\0", 1);
  83. std::cout << "client failed to pass through authorisation\n";
  84. klient->release();
  85. }
  86. }
  87. runningThreads--;
  88. });
  89. InitializeCriticalSection(&cs);
  90. }
  91. // Destruktor
  92. FactoryCraftServer::~FactoryCraftServer()
  93. {
  94. sslServer->trenne();
  95. server->trenne();
  96. while (runningThreads > 0)
  97. Sleep(100);
  98. sslServer->release();
  99. server->release();
  100. if (klients) klients->release();
  101. ini->release();
  102. Game::INSTANCE->requestStop();
  103. Game::INSTANCE->release();
  104. DeleteCriticalSection(&cs);
  105. }
  106. // nicht constant
  107. void FactoryCraftServer::run()
  108. {
  109. runningThreads++;
  110. while (sslServer->isConnected())
  111. {
  112. SSLSKlient* klient = sslServer->getKlient();
  113. if (!klient) continue;
  114. Framework::getThreadRegister()->cleanUpClosedThreads();
  115. FCKlient* clHandle = new FCKlient(
  116. klient, dynamic_cast<FactoryCraftServer*>(getThis()));
  117. EnterCriticalSection(&cs);
  118. klients->add(clHandle);
  119. LeaveCriticalSection(&cs);
  120. clHandle->start();
  121. }
  122. runningThreads--;
  123. }
  124. void FactoryCraftServer::close()
  125. {
  126. sslServer->trenne();
  127. EnterCriticalSection(&cs);
  128. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  129. klients->z(i)->absturz();
  130. Game::INSTANCE->save();
  131. LeaveCriticalSection(&cs);
  132. }
  133. bool FactoryCraftServer::absturzKlient(int accountId)
  134. {
  135. bool gefunden = 0;
  136. EnterCriticalSection(&cs);
  137. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  138. {
  139. if (klients->z(i) && klients->z(i)->getAccountId() == accountId)
  140. {
  141. klients->z(i)->absturz();
  142. klients->remove(i);
  143. gefunden = 1;
  144. break;
  145. }
  146. }
  147. LeaveCriticalSection(&cs);
  148. return gefunden;
  149. }
  150. bool FactoryCraftServer::removeKlient(FCKlient* zKlient)
  151. {
  152. bool gefunden = 0;
  153. EnterCriticalSection(&cs);
  154. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  155. {
  156. if (klients->z(i) == zKlient)
  157. {
  158. klients->remove(i);
  159. gefunden = 1;
  160. break;
  161. }
  162. }
  163. LeaveCriticalSection(&cs);
  164. return gefunden;
  165. }
  166. bool FactoryCraftServer::hatClients() const
  167. {
  168. return klients->hat(0);
  169. }
  170. int FactoryCraftServer::getUnencryptedPort() const
  171. {
  172. return server->getPort();
  173. }
  174. // Inhalt der LSKlient aus LoginServer.h
  175. // Konstruktor
  176. FCKlient::FCKlient(SSLSKlient* klient, FactoryCraftServer* ls)
  177. : Thread()
  178. {
  179. this->klient = klient;
  180. background = 0;
  181. foreground = 0;
  182. accountId = 0;
  183. this->ls = ls;
  184. zGameClient = 0;
  185. backgroundReader = 0;
  186. foregroundReader = 0;
  187. backgroundWriter = 0;
  188. foregroundWriter = 0;
  189. authKey = randomKey(authKeyLen);
  190. }
  191. // Destruktor
  192. FCKlient::~FCKlient()
  193. {
  194. if (zGameClient)
  195. {
  196. zGameClient->logout();
  197. zGameClient = (GameClient*)zGameClient->release();
  198. }
  199. if (background) background->release();
  200. if (foreground) foreground->release();
  201. delete backgroundReader;
  202. delete foregroundReader;
  203. delete backgroundWriter;
  204. delete foregroundWriter;
  205. klient->release();
  206. ls->release();
  207. delete[] authKey;
  208. }
  209. // nicht constant
  210. void FCKlient::setForegroundClient(SKlient* foreground)
  211. {
  212. std::cout << "foreground client connected\n";
  213. this->foreground = foreground;
  214. foregroundReader = new NetworkReader(foreground);
  215. foregroundWriter = new NetworkWriter(foreground);
  216. if (foreground && background)
  217. zGameClient
  218. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()),
  219. Framework::Text((int)accountId));
  220. foregroundRunning = 1;
  221. new AsynchronCall([this]() {
  222. while (this->foreground->waitForNextMessage())
  223. {
  224. if (zGameClient) zGameClient->addMessage(foregroundReader);
  225. if (!zGameClient) Sleep(100);
  226. }
  227. cs.lock();
  228. foregroundRunning = 0;
  229. if (!backgroundRunning)
  230. {
  231. cs.unlock();
  232. if (zGameClient)
  233. {
  234. zGameClient->logout();
  235. zGameClient = (GameClient*)zGameClient->release();
  236. }
  237. ls->removeKlient(this);
  238. }
  239. else
  240. {
  241. cs.unlock();
  242. }
  243. });
  244. }
  245. void FCKlient::setBackgroundClient(SKlient* background)
  246. {
  247. std::cout << "background client connected\n";
  248. this->background = background;
  249. backgroundReader = new NetworkReader(background);
  250. backgroundWriter = new NetworkWriter(background);
  251. if (foreground && background)
  252. zGameClient
  253. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()),
  254. Framework::Text((int)accountId));
  255. backgroundRunning = 1;
  256. new AsynchronCall([this]() {
  257. while (this->background->waitForNextMessage())
  258. {
  259. if (zGameClient) zGameClient->addMessage(backgroundReader);
  260. if (!zGameClient) Sleep(100);
  261. }
  262. cs.lock();
  263. backgroundRunning = 0;
  264. if (!foregroundRunning)
  265. {
  266. cs.unlock();
  267. if (zGameClient)
  268. {
  269. zGameClient->logout();
  270. zGameClient = (GameClient*)zGameClient->release();
  271. }
  272. ls->removeKlient(this);
  273. }
  274. else
  275. {
  276. cs.unlock();
  277. }
  278. });
  279. }
  280. void FCKlient::absturz()
  281. {
  282. klient->trenne();
  283. if (background) background->trenne();
  284. if (foreground) foreground->trenne();
  285. warteAufThread(10000);
  286. ende();
  287. }
  288. void FCKlient::thread()
  289. {
  290. bool identified = 0;
  291. while (1)
  292. {
  293. char c = 0;
  294. if (!klient->getNachricht(&c, 1))
  295. break;
  296. else
  297. {
  298. bool br = 0;
  299. switch (c)
  300. {
  301. case 1: // Klient identifikation
  302. {
  303. char len;
  304. klient->getNachricht(&len, 1);
  305. char* name = new char[len + 1];
  306. klient->getNachricht(name, len);
  307. name[(int)len] = 0;
  308. unsigned short sLen;
  309. klient->getNachricht((char*)&sLen, 2);
  310. char* secret = new char[sLen + 1];
  311. klient->getNachricht(secret, sLen);
  312. secret[sLen] = 0;
  313. if (!Game::INSTANCE->checkPlayer(name, secret))
  314. {
  315. klient->sende("\0", 1);
  316. delete[] name;
  317. delete[] secret;
  318. break;
  319. }
  320. if (!Game::INSTANCE->existsPlayer(name))
  321. {
  322. Text secret = Game::INSTANCE->createPlayer(name);
  323. klient->sende("\2", 1);
  324. short len = (short)secret.getLength();
  325. klient->sende((char*)&len, 2);
  326. klient->sende(secret.getText(), len);
  327. }
  328. else
  329. {
  330. klient->sende("\1", 1);
  331. identified = 1;
  332. }
  333. short keyLen = (short)authKeyLen;
  334. klient->sende((char*)&keyLen, 2);
  335. klient->sende(authKey, authKeyLen);
  336. delete[] name;
  337. delete[] secret;
  338. break;
  339. }
  340. case 2: // Verbindungsende
  341. br = 1;
  342. if (zGameClient)
  343. {
  344. zGameClient->logout();
  345. zGameClient = (GameClient*)zGameClient->release();
  346. }
  347. klient->sende("\1", 1);
  348. break;
  349. case 3: // ping
  350. klient->sende("\1", 1);
  351. break;
  352. case 4: // check player name valid
  353. {
  354. klient->sende("\1", 1);
  355. char len;
  356. klient->getNachricht(&len, 1);
  357. char* name = new char[len + 1];
  358. klient->getNachricht(name, len);
  359. name[(int)len] = 0;
  360. short sLen;
  361. klient->getNachricht((char*)&sLen, 2);
  362. char* secret = new char[sLen + 1];
  363. klient->getNachricht(secret, sLen);
  364. secret[sLen] = 0;
  365. char res = 0;
  366. if (Game::INSTANCE->checkPlayer(name, secret)) res = 1;
  367. klient->sende(&res, 1);
  368. delete[] name;
  369. delete[] secret;
  370. break;
  371. }
  372. default:
  373. br = 1;
  374. break;
  375. }
  376. if (br) break;
  377. }
  378. }
  379. if (!identified)
  380. {
  381. ls->removeKlient(this);
  382. }
  383. }
  384. int FCKlient::getAccountId() const // gibt die KlientId zurück
  385. {
  386. return accountId;
  387. }
  388. NetworkWriter* FCKlient::zBackgroundWriter() const
  389. {
  390. return backgroundWriter;
  391. }
  392. NetworkWriter* FCKlient::zForegroundWriter() const
  393. {
  394. return foregroundWriter;
  395. }
  396. bool FCKlient::matchAuthKey(char* key, int len) const
  397. {
  398. if (foreground && background) return 0;
  399. if (len != authKeyLen) return 0;
  400. for (int i = 0; i < len; i++)
  401. {
  402. if (key[i] != authKey[i]) return 0;
  403. }
  404. return 1;
  405. }