Server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. DeleteCriticalSection(&cs);
  103. }
  104. // nicht constant
  105. void FactoryCraftServer::run()
  106. {
  107. runningThreads++;
  108. while (sslServer->isConnected())
  109. {
  110. SSLSKlient* klient = sslServer->getKlient();
  111. if (!klient) continue;
  112. Framework::getThreadRegister()->cleanUpClosedThreads();
  113. FCKlient* clHandle = new FCKlient(
  114. klient, dynamic_cast<FactoryCraftServer*>(getThis()));
  115. EnterCriticalSection(&cs);
  116. klients->add(clHandle);
  117. LeaveCriticalSection(&cs);
  118. clHandle->start();
  119. }
  120. runningThreads--;
  121. }
  122. void FactoryCraftServer::close()
  123. {
  124. Game::INSTANCE->save();
  125. sslServer->trenne();
  126. server->trenne();
  127. EnterCriticalSection(&cs);
  128. for (int i = 0; i < klients->getEintragAnzahl(); i++)
  129. klients->z(i)->absturz();
  130. LeaveCriticalSection(&cs);
  131. }
  132. bool FactoryCraftServer::removeKlient(FCKlient* zKlient)
  133. {
  134. bool gefunden = 0;
  135. getThis();
  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. release();
  148. return gefunden;
  149. }
  150. bool FactoryCraftServer::hatClients() const
  151. {
  152. return klients->hat(0);
  153. }
  154. int FactoryCraftServer::getUnencryptedPort() const
  155. {
  156. return server->getPort();
  157. }
  158. // Inhalt der LSKlient aus LoginServer.h
  159. // Konstruktor
  160. FCKlient::FCKlient(SSLSKlient* klient, FactoryCraftServer* ls)
  161. : Thread()
  162. {
  163. this->klient = klient;
  164. background = 0;
  165. foreground = 0;
  166. this->ls = ls;
  167. zGameClient = 0;
  168. backgroundReader = 0;
  169. foregroundReader = 0;
  170. backgroundWriter = 0;
  171. foregroundWriter = 0;
  172. authKey = randomSecret();
  173. }
  174. // Destruktor
  175. FCKlient::~FCKlient()
  176. {
  177. if (zGameClient)
  178. {
  179. zGameClient->logout();
  180. zGameClient = (GameClient*)zGameClient->release();
  181. }
  182. if (background) background->release();
  183. if (foreground) foreground->release();
  184. delete backgroundReader;
  185. delete foregroundReader;
  186. delete backgroundWriter;
  187. delete foregroundWriter;
  188. klient->release();
  189. ls->release();
  190. }
  191. // nicht constant
  192. void FCKlient::setForegroundClient(SKlient* foreground)
  193. {
  194. std::cout << "foreground client connected\n";
  195. this->foreground = foreground;
  196. foregroundReader = new NetworkReader(foreground);
  197. foregroundWriter = new NetworkWriter(foreground);
  198. if (foreground && background)
  199. zGameClient
  200. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()), name);
  201. foregroundRunning = 1;
  202. new AsynchronCall([this]() {
  203. while (this->foreground->waitForNextMessage())
  204. {
  205. if (zGameClient) zGameClient->addMessage(foregroundReader);
  206. if (!zGameClient) Sleep(100);
  207. }
  208. cs.lock();
  209. foregroundRunning = 0;
  210. if (!backgroundRunning)
  211. {
  212. cs.unlock();
  213. if (zGameClient)
  214. {
  215. zGameClient->logout();
  216. zGameClient = (GameClient*)zGameClient->release();
  217. }
  218. ls->removeKlient(this);
  219. }
  220. else
  221. {
  222. cs.unlock();
  223. }
  224. });
  225. }
  226. void FCKlient::setBackgroundClient(SKlient* background)
  227. {
  228. std::cout << "background client connected\n";
  229. this->background = background;
  230. backgroundReader = new NetworkReader(background);
  231. backgroundWriter = new NetworkWriter(background);
  232. if (foreground && background)
  233. zGameClient
  234. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()),
  235. 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. }