Server.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // Inhalt der LSKlient aus LoginServer.h
  116. // Konstruktor
  117. FCKlient::FCKlient( SSLSKlient *klient, FactoryCraftServer *ls )
  118. : Thread()
  119. {
  120. this->klient = klient;
  121. accountId = 0;
  122. this->ls = ls;
  123. reader = new NetworkReader( klient );
  124. }
  125. // Destruktor
  126. FCKlient::~FCKlient()
  127. {
  128. if( zGameClient )
  129. {
  130. zGameClient->logout();
  131. zGameClient = (GameClient *)zGameClient->release();
  132. }
  133. delete reader;
  134. klient->release();
  135. ls->release();
  136. }
  137. // nicht constant
  138. void FCKlient::absturz()
  139. {
  140. ende();
  141. klient->trenne();
  142. }
  143. void FCKlient::thread()
  144. {
  145. while( 1 )
  146. {
  147. char c = 0;
  148. if( !klient->getNachricht( &c, 1 ) )
  149. break;
  150. else
  151. {
  152. bool br = 0;
  153. switch( c )
  154. {
  155. case 1: // Klient identifikation
  156. {
  157. int accountId = 0;
  158. klient->getNachricht( (char *)&accountId, 4 );
  159. unsigned char secretLength = 0;
  160. klient->getNachricht( (char *)&secretLength, 1 );
  161. char *secret = new char[ secretLength + 1 ];
  162. klient->getNachricht( secret, (int)secretLength );
  163. secret[ secretLength ] = 0;
  164. Text data = "{\"account_id\":";
  165. data += accountId;
  166. data += ", \"secret\": \"";
  167. data += secret;
  168. data += "\"}";
  169. bool ok = false;
  170. HTTP::Answer *answer = HTTP::PostRequest( "/game_client/api/verify_client.php", "koljastrohm-games.com", data, "application/json", 443, true ).execute();
  171. if( answer->getStatusCode() == 200 )
  172. {
  173. JSON::JSONObject obj( answer->getData() );
  174. if( obj.hasValue( "verified" ) )
  175. {
  176. JSON::JSONValue *value = obj.getValue( "verified" );
  177. if( value->getType() == JSON::BOOLEAN )
  178. {
  179. if( ( (JSON::JSONBool *)value )->getBool() )
  180. {
  181. this->accountId = accountId;
  182. if( zGameClient )
  183. {
  184. zGameClient->logout();
  185. zGameClient = (GameClient *)zGameClient->release();
  186. }
  187. zGameClient = ls->zGame()->addPlayer( dynamic_cast<FCKlient *>( getThis() ), Text( accountId ) );
  188. klient->sende( "\1", 1 );
  189. ok = true;
  190. }
  191. }
  192. value->release();
  193. }
  194. }
  195. answer->release();
  196. delete[]secret;
  197. if( !ok )
  198. klient->sende( "\0", 1 );
  199. break;
  200. }
  201. case 2: // Verbindungsende
  202. br = 1;
  203. if( zGameClient )
  204. {
  205. zGameClient->logout();
  206. zGameClient = (GameClient *)zGameClient->release();
  207. }
  208. klient->sende( "\1", 1 );
  209. break;
  210. default:
  211. if( zGameClient )
  212. zGameClient->addMessage( reader );
  213. break;
  214. }
  215. if( br )
  216. break;
  217. ls->addEmpfangen( klient->getDownloadBytes( 1 ) );
  218. ls->addGesendet( klient->getUploadBytes( 1 ) );
  219. }
  220. }
  221. ls->addEmpfangen( klient->getDownloadBytes( 1 ) );
  222. ls->addGesendet( klient->getUploadBytes( 1 ) );
  223. ls->removeKlient( this ); // delete this
  224. }
  225. int FCKlient::getAccountId() const // gibt die KlientId zurück
  226. {
  227. return accountId;
  228. }
  229. SSLSKlient *FCKlient::zClient() const
  230. {
  231. return klient;
  232. }