Server.cpp 11 KB

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