Server.cpp 12 KB

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