Server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. new AsynchronCall([this]() {
  209. while (this->foreground->waitForNextMessage())
  210. {
  211. if (zGameClient) zGameClient->addMessage(foregroundReader);
  212. if (!zGameClient) Sleep(100);
  213. }
  214. zGameClient->logout();
  215. ls->removeKlient(this);
  216. });
  217. }
  218. void FCKlient::setBackgroundClient(SKlient* background)
  219. {
  220. this->background = background;
  221. backgroundReader = new NetworkReader(background);
  222. backgroundWriter = new NetworkWriter(background);
  223. if (foreground && background)
  224. zGameClient
  225. = Game::INSTANCE->addPlayer(dynamic_cast<FCKlient*>(getThis()),
  226. Framework::Text((int)accountId));
  227. new AsynchronCall([this]() {
  228. while (this->background->waitForNextMessage())
  229. {
  230. if (zGameClient) zGameClient->addMessage(backgroundReader);
  231. if (!zGameClient) Sleep(100);
  232. }
  233. zGameClient->logout();
  234. ls->removeKlient(this);
  235. });
  236. }
  237. void FCKlient::absturz()
  238. {
  239. ende();
  240. klient->trenne();
  241. }
  242. void FCKlient::thread()
  243. {
  244. while (1)
  245. {
  246. char c = 0;
  247. if (!klient->getNachricht(&c, 1))
  248. break;
  249. else
  250. {
  251. bool br = 0;
  252. switch (c)
  253. {
  254. case 1: // Klient identifikation
  255. {
  256. char len;
  257. klient->getNachricht(&len, 1);
  258. char* name = new char[len + 1];
  259. klient->getNachricht(name, len);
  260. name[(int)len] = 0;
  261. unsigned short sLen;
  262. klient->getNachricht((char*)&sLen, 2);
  263. char* secret = new char[sLen + 1];
  264. klient->getNachricht(secret, sLen);
  265. secret[sLen] = 0;
  266. if (!Game::INSTANCE->checkPlayer(name, secret))
  267. {
  268. klient->sende("\0", 1);
  269. delete[] name;
  270. delete[] secret;
  271. break;
  272. }
  273. if (!Game::INSTANCE->existsPlayer(name))
  274. {
  275. Text secret = Game::INSTANCE->createPlayer(name);
  276. klient->sende("\2", 1);
  277. short len = (short)secret.getLength();
  278. klient->sende((char*)&len, 2);
  279. klient->sende(secret.getText(), len);
  280. }
  281. else
  282. {
  283. klient->sende("\1", 1);
  284. }
  285. klient->sende((char*)&authKeyLen, 4);
  286. klient->sende(authKey, authKeyLen);
  287. delete[] name;
  288. delete[] secret;
  289. break;
  290. }
  291. case 2: // Verbindungsende
  292. br = 1;
  293. if (zGameClient)
  294. {
  295. zGameClient->logout();
  296. zGameClient = (GameClient*)zGameClient->release();
  297. }
  298. klient->sende("\1", 1);
  299. break;
  300. case 3: // ping
  301. klient->sende("\1", 1);
  302. break;
  303. case 4: // check player name valid
  304. {
  305. klient->sende("\1", 1);
  306. char len;
  307. klient->getNachricht(&len, 1);
  308. char* name = new char[len + 1];
  309. klient->getNachricht(name, len);
  310. name[(int)len] = 0;
  311. short sLen;
  312. klient->getNachricht((char*)&sLen, 2);
  313. char* secret = new char[sLen + 1];
  314. klient->getNachricht(secret, sLen);
  315. secret[sLen] = 0;
  316. char res = 0;
  317. if (Game::INSTANCE->checkPlayer(name, secret)) res = 1;
  318. klient->sende(&res, 1);
  319. delete[] name;
  320. delete[] secret;
  321. break;
  322. }
  323. default:
  324. br = 1;
  325. break;
  326. }
  327. if (br) break;
  328. }
  329. }
  330. }
  331. int FCKlient::getAccountId() const // gibt die KlientId zurück
  332. {
  333. return accountId;
  334. }
  335. NetworkWriter* FCKlient::zBackgroundWriter() const
  336. {
  337. return backgroundWriter;
  338. }
  339. NetworkWriter* FCKlient::zForegroundWriter() const
  340. {
  341. return foregroundWriter;
  342. }
  343. bool FCKlient::matchAuthKey(char* key, int len) const
  344. {
  345. if (foreground && background) return 0;
  346. if (len != authKeyLen) return 0;
  347. for (int i = 0; i < len; i++)
  348. {
  349. if (key[i] != authKey[i]) return 0;
  350. }
  351. return 1;
  352. }