Server.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. LeaveCriticalSection( &cs );
  67. }
  68. bool FactoryCraftServer::absturzKlient( int accountId )
  69. {
  70. bool gefunden = 0;
  71. EnterCriticalSection( &cs );
  72. for( int i = 0; i < klientAnzahl; i++ )
  73. {
  74. if( klients->z( i ) && klients->z( i )->getAccountId() == accountId )
  75. {
  76. klients->z( i )->absturz();
  77. klients->remove( i );
  78. klientAnzahl--;
  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 < klientAnzahl; i++ )
  91. {
  92. if( klients->z( i ) == zKlient )
  93. {
  94. klients->remove( i );
  95. klientAnzahl--;
  96. gefunden = 1;
  97. break;
  98. }
  99. }
  100. LeaveCriticalSection( &cs );
  101. return gefunden;
  102. }
  103. void FactoryCraftServer::addGesendet( int bytes )
  104. {
  105. gesendet += bytes;
  106. }
  107. void FactoryCraftServer::addEmpfangen( int bytes )
  108. {
  109. empfangen += bytes;
  110. }
  111. bool FactoryCraftServer::hatClients() const
  112. {
  113. return klientAnzahl > 0;
  114. }
  115. Game *FactoryCraftServer::zGame() const
  116. {
  117. return game;
  118. }
  119. // Inhalt der LSKlient aus LoginServer.h
  120. // Konstruktor
  121. FCKlient::FCKlient( SSLSKlient *klient, FactoryCraftServer *ls )
  122. : Thread()
  123. {
  124. this->klient = klient;
  125. accountId = 0;
  126. this->ls = ls;
  127. reader = new NetworkReader( klient );
  128. writer = new NetworkWriter( klient );
  129. }
  130. // Destruktor
  131. FCKlient::~FCKlient()
  132. {
  133. if( zGameClient )
  134. {
  135. zGameClient->logout();
  136. zGameClient = (GameClient *)zGameClient->release();
  137. }
  138. delete reader;
  139. delete writer;
  140. klient->release();
  141. ls->release();
  142. }
  143. // nicht constant
  144. void FCKlient::absturz()
  145. {
  146. ende();
  147. klient->trenne();
  148. }
  149. void FCKlient::thread()
  150. {
  151. while( 1 )
  152. {
  153. char c = 0;
  154. if( !klient->getNachricht( &c, 1 ) )
  155. break;
  156. else
  157. {
  158. bool br = 0;
  159. switch( c )
  160. {
  161. case 1: // Klient identifikation
  162. {
  163. int accountId = 0;
  164. klient->getNachricht( (char *)&accountId, 4 );
  165. unsigned char secretLength = 0;
  166. klient->getNachricht( (char *)&secretLength, 1 );
  167. char *secret = new char[ secretLength + 1 ];
  168. klient->getNachricht( secret, (int)secretLength );
  169. secret[ secretLength ] = 0;
  170. Text data = "{\"account_id\":";
  171. data += accountId;
  172. data += ", \"secret\": \"";
  173. data += secret;
  174. data += "\"}";
  175. bool ok = false;
  176. HTTP::Answer *answer = HTTP::PostRequest( "/game_client/api/verify_client.php", "koljastrohm-games.com", data, "application/json", 443, true ).execute();
  177. if( answer->getStatusCode() == 200 )
  178. {
  179. JSON::JSONObject obj( answer->getData() );
  180. if( obj.hasValue( "verified" ) )
  181. {
  182. JSON::JSONValue *value = obj.getValue( "verified" );
  183. if( value->getType() == JSON::BOOLEAN )
  184. {
  185. if( ( (JSON::JSONBool *)value )->getBool() )
  186. {
  187. this->accountId = accountId;
  188. if( zGameClient )
  189. {
  190. zGameClient->logout();
  191. zGameClient = (GameClient *)zGameClient->release();
  192. }
  193. zGameClient = ls->zGame()->addPlayer( dynamic_cast<FCKlient *>( getThis() ), Text( accountId ) );
  194. klient->sende( "\1", 1 );
  195. ok = true;
  196. }
  197. }
  198. value->release();
  199. }
  200. }
  201. answer->release();
  202. delete[]secret;
  203. if( !ok )
  204. klient->sende( "\0", 1 );
  205. break;
  206. }
  207. case 2: // Verbindungsende
  208. br = 1;
  209. if( zGameClient )
  210. {
  211. zGameClient->logout();
  212. zGameClient = (GameClient *)zGameClient->release();
  213. }
  214. klient->sende( "\1", 1 );
  215. break;
  216. default:
  217. if( zGameClient )
  218. zGameClient->addMessage( reader );
  219. break;
  220. }
  221. if( br )
  222. break;
  223. ls->addEmpfangen( klient->getDownloadBytes( 1 ) );
  224. ls->addGesendet( klient->getUploadBytes( 1 ) );
  225. }
  226. }
  227. ls->addEmpfangen( klient->getDownloadBytes( 1 ) );
  228. ls->addGesendet( klient->getUploadBytes( 1 ) );
  229. ls->removeKlient( this ); // delete this
  230. }
  231. int FCKlient::getAccountId() const // gibt die KlientId zurück
  232. {
  233. return accountId;
  234. }
  235. SSLSKlient *FCKlient::zClient() const
  236. {
  237. return klient;
  238. }
  239. NetworkWriter *FCKlient::zWriter() const
  240. {
  241. return writer;
  242. }