Server.cpp 8.5 KB

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