Server.cpp 11 KB

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