Server.cpp 7.1 KB

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