Server.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "Server.h"
  2. #include <iostream>
  3. #include <Klient.h>
  4. #include <Globals.h>
  5. #include <HttpRequest.h>
  6. #include <JSON.h>
  7. // Inhalt der LoginServer Klasse aus LoginServer.h
  8. // Konstruktor
  9. FactoryCraftServer::FactoryCraftServer( InitDatei* zIni )
  10. : ReferenceCounter()
  11. {
  12. Network::Start( 100 );
  13. klients = new RCArray< FCKlient >();
  14. empfangen = 0;
  15. gesendet = 0;
  16. ini = dynamic_cast<InitDatei*>(zIni->getThis());
  17. id = *zIni->zWert( "ServerId" );
  18. server = new SSLServer();
  19. server->setPrivateKeyPassword( zIni->zWert( "SSLPasswort" )->getText() );
  20. server->setCertificateFile( zIni->zWert( "SSLCert" )->getText() );
  21. std::cout << "using cert file " << zIni->zWert( "SSLCert" )->getText() << "\n";
  22. server->setPrivateKeyFile( zIni->zWert( "SSLKey" )->getText() );
  23. std::cout << "using private key " << zIni->zWert( "SSLKey" )->getText() << "\n";
  24. game = new Game( zIni->zWert( "World" )->getText(), zIni->zWert( "SaveDir" )->getText() );
  25. InitializeCriticalSection( &cs );
  26. }
  27. // Destruktor
  28. FactoryCraftServer::~FactoryCraftServer()
  29. {
  30. server->trenne();
  31. server->release();
  32. if( klients )
  33. klients->release();
  34. ini->release();
  35. game->requestStop();
  36. game->release();
  37. DeleteCriticalSection( &cs );
  38. }
  39. // nicht constant
  40. void FactoryCraftServer::run()
  41. {
  42. if( !server->verbinde( (unsigned short)TextZuInt( ini->zWert( "SSLPort" )->getText(), 10 ), 10 ) )
  43. std::cout << "Der Server konnte nicht gestartet werden.\n";
  44. else
  45. std::cout << "Server Port: " << ini->zWert( "SSLPort" )->getText() << "\n";
  46. while( server->isConnected() )
  47. {
  48. SSLSKlient* klient = server->getKlient();
  49. if( !klient )
  50. continue;
  51. Framework::getThreadRegister()->cleanUpClosedThreads();
  52. FCKlient* clHandle = new FCKlient( klient, dynamic_cast<FactoryCraftServer*>(getThis()) );
  53. EnterCriticalSection( &cs );
  54. klients->add( clHandle );
  55. LeaveCriticalSection( &cs );
  56. clHandle->start();
  57. }
  58. }
  59. void FactoryCraftServer::close()
  60. {
  61. server->trenne();
  62. EnterCriticalSection( &cs );
  63. for( int i = 0; i < klients->getEintragAnzahl(); i++ )
  64. klients->z( i )->absturz();
  65. klients = (RCArray< FCKlient > *)klients->release();
  66. game->save();
  67. LeaveCriticalSection( &cs );
  68. }
  69. bool FactoryCraftServer::absturzKlient( int accountId )
  70. {
  71. bool gefunden = 0;
  72. EnterCriticalSection( &cs );
  73. for( int i = 0; i < klients->getEintragAnzahl(); i++ )
  74. {
  75. if( klients->z( i ) && klients->z( i )->getAccountId() == accountId )
  76. {
  77. klients->z( i )->absturz();
  78. klients->remove( i );
  79. gefunden = 1;
  80. break;
  81. }
  82. }
  83. LeaveCriticalSection( &cs );
  84. return gefunden;
  85. }
  86. bool FactoryCraftServer::removeKlient( FCKlient* zKlient )
  87. {
  88. bool gefunden = 0;
  89. EnterCriticalSection( &cs );
  90. for( int i = 0; i < klients->getEintragAnzahl(); i++ )
  91. {
  92. if( klients->z( i ) == zKlient )
  93. {
  94. klients->remove( i );
  95. gefunden = 1;
  96. break;
  97. }
  98. }
  99. LeaveCriticalSection( &cs );
  100. return gefunden;
  101. }
  102. void FactoryCraftServer::addGesendet( int bytes )
  103. {
  104. gesendet += bytes;
  105. }
  106. void FactoryCraftServer::addEmpfangen( int bytes )
  107. {
  108. empfangen += bytes;
  109. }
  110. bool FactoryCraftServer::hatClients() const
  111. {
  112. return klients->hat( 0 );
  113. }
  114. Game* FactoryCraftServer::zGame() const
  115. {
  116. return game;
  117. }
  118. // Inhalt der LSKlient aus LoginServer.h
  119. // Konstruktor
  120. FCKlient::FCKlient( SSLSKlient* klient, FactoryCraftServer* ls )
  121. : Thread()
  122. {
  123. this->klient = klient;
  124. accountId = 0;
  125. this->ls = ls;
  126. reader = new NetworkReader( klient );
  127. writer = new NetworkWriter( klient );
  128. }
  129. // Destruktor
  130. FCKlient::~FCKlient()
  131. {
  132. if( zGameClient )
  133. {
  134. zGameClient->logout();
  135. zGameClient = (GameClient*)zGameClient->release();
  136. }
  137. delete reader;
  138. delete writer;
  139. klient->release();
  140. ls->release();
  141. }
  142. // nicht constant
  143. void FCKlient::absturz()
  144. {
  145. ende();
  146. klient->trenne();
  147. }
  148. void FCKlient::thread()
  149. {
  150. while( 1 )
  151. {
  152. char c = 0;
  153. if( !klient->getNachricht( &c, 1 ) )
  154. break;
  155. else
  156. {
  157. bool br = 0;
  158. switch( c )
  159. {
  160. case 1: // Klient identifikation
  161. {
  162. int accountId = 0;
  163. klient->getNachricht( (char*)&accountId, 4 );
  164. unsigned char secretLength = 0;
  165. klient->getNachricht( (char*)&secretLength, 1 );
  166. char* secret = new char[ secretLength + 1 ];
  167. klient->getNachricht( secret, (int)secretLength );
  168. secret[ secretLength ] = 0;
  169. Text data = "{\"account_id\":";
  170. data += accountId;
  171. data += ", \"secret\": \"";
  172. data += secret;
  173. data += "\"}";
  174. bool ok = false;
  175. HTTP::Answer* answer = HTTP::PostRequest( "/game_client/api/verify_client.php", "koljastrohm-games.com", data, "application/json", 443, true ).execute();
  176. if( answer->getStatusCode() == 200 )
  177. {
  178. JSON::JSONObject obj( answer->getData() );
  179. if( obj.hasValue( "verified" ) )
  180. {
  181. JSON::JSONValue* value = obj.getValue( "verified" );
  182. if( value->getType() == JSON::JSONType::BOOLEAN )
  183. {
  184. if( ((JSON::JSONBool*)value)->getBool() )
  185. {
  186. this->accountId = accountId;
  187. if( zGameClient )
  188. {
  189. zGameClient->logout();
  190. zGameClient = (GameClient*)zGameClient->release();
  191. }
  192. klient->sende( "\1", 1 );
  193. zGameClient = ls->zGame()->addPlayer( dynamic_cast<FCKlient*>(getThis()), Text( accountId ) );
  194. ok = true;
  195. }
  196. }
  197. value->release();
  198. }
  199. }
  200. answer->release();
  201. delete[]secret;
  202. if( !ok )
  203. klient->sende( "\0", 1 );
  204. break;
  205. }
  206. case 2: // Verbindungsende
  207. br = 1;
  208. if( zGameClient )
  209. {
  210. zGameClient->logout();
  211. zGameClient = (GameClient*)zGameClient->release();
  212. }
  213. klient->sende( "\1", 1 );
  214. break;
  215. default:
  216. if( zGameClient )
  217. zGameClient->addMessage( reader );
  218. break;
  219. }
  220. if( br )
  221. break;
  222. ls->addEmpfangen( klient->getDownloadBytes( 1 ) );
  223. ls->addGesendet( klient->getUploadBytes( 1 ) );
  224. }
  225. }
  226. ls->addEmpfangen( klient->getDownloadBytes( 1 ) );
  227. ls->addGesendet( klient->getUploadBytes( 1 ) );
  228. ls->removeKlient( this ); // delete this
  229. }
  230. int FCKlient::getAccountId() const // gibt die KlientId zurück
  231. {
  232. return accountId;
  233. }
  234. SSLSKlient* FCKlient::zClient() const
  235. {
  236. return klient;
  237. }
  238. NetworkWriter* FCKlient::zWriter() const
  239. {
  240. return writer;
  241. }