Server.cpp 12 KB

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