AppServer.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. #include "AppServer.h"
  2. #include "Datenbank.h"
  3. #include <Text.h>
  4. #include <iostream>
  5. #include <Klient.h>
  6. #include <Globals.h>
  7. // Inhalt der AppServer Klasse aus AppServer.h
  8. // Konstruktor
  9. AppServer::AppServer( InitDatei *zDat )
  10. : Thread()
  11. {
  12. Network::Start( 100 );
  13. std::cout << "AppS: Verbindung mit Datenbank wird hergestellt...\n";
  14. db = new AppSDatenbank( zDat );
  15. clients = 0;
  16. empfangen = 0;
  17. gesendet = 0;
  18. dat = dynamic_cast<InitDatei *>( zDat->getThis() );
  19. server = new Server();
  20. aServer = new SSLServer();
  21. aServer->setPrivateKeyPassword( zDat->zWert( "SSLPasswort" )->getText() );
  22. aServer->setPrivateKeyFile( zDat->zWert( "SSLKey" )->getText() );
  23. aServer->setCertificateFile( zDat->zWert( "SSLCert" )->getText() );
  24. std::cout << "AppS: Starten des Admin Servers...\n";
  25. if( !aServer->verbinde( (unsigned short)TextZuInt( zDat->zWert( "AppSAPort" )->getText(), 10 ), 10 ) )
  26. {
  27. std::cout << "AppS: Der Admin Server konnte nicht gestartet werden. Das Programm wird beendet.\n";
  28. exit( 1 );
  29. }
  30. end = 0;
  31. InitializeCriticalSection( &cs );
  32. if( zDat->wertExistiert( "Aktiv" ) && zDat->zWert( "Aktiv" )->istGleich( "TRUE" ) )
  33. serverStarten();
  34. }
  35. // Destruktor
  36. AppServer::~AppServer()
  37. {
  38. ende();
  39. server->trenne();
  40. server->release();
  41. aServer->trenne();
  42. aServer->release();
  43. dat->release();
  44. db->release();
  45. Network::Exit();
  46. DeleteCriticalSection( &cs );
  47. }
  48. // nicht constant
  49. void AppServer::runn()
  50. {
  51. while( !end && aServer->isConnected() )
  52. {
  53. SSLSKlient *klient;
  54. klient = aServer->getKlient();
  55. if( end && klient )
  56. {
  57. klient->trenne();
  58. klient = (SSLSKlient *)klient->release();
  59. Sleep( 1000 );
  60. return;
  61. }
  62. if( !klient )
  63. continue;
  64. AppSAKlient *clHandle = new AppSAKlient( klient, dynamic_cast<AppServer *>( getThis() ) );
  65. clHandle->start();
  66. }
  67. }
  68. void AppServer::thread()
  69. {
  70. while( server->isConnected() )
  71. {
  72. SKlient *klient;
  73. klient = server->getKlient();
  74. if( !klient )
  75. continue;
  76. Framework::getThreadRegister()->cleanUpClosedThreads();
  77. AppSKlient *clHandle = new AppSKlient( klient, dynamic_cast<AppServer *>( getThis() ) );
  78. clHandle->start();
  79. clients++;
  80. }
  81. run = 0;
  82. }
  83. void AppServer::close()
  84. {
  85. server->trenne();
  86. #ifdef WIN32
  87. warteAufThread( 1000 );
  88. #endif
  89. ende();
  90. run = 0;
  91. end = 1;
  92. Klient *klient = new Klient();
  93. klient->verbinde( aServer->getPort(), "127.0.0.1" );
  94. Sleep( 500 );
  95. aServer->trenne();
  96. klient->release();
  97. }
  98. bool AppServer::serverStarten()
  99. {
  100. if( run )
  101. return 1;
  102. bool ret = server->verbinde( (unsigned short)TextZuInt( dat->zWert( "AppSPort" )->getText(), 10 ), 10 );
  103. start();
  104. return ret;
  105. }
  106. void AppServer::serverBeenden()
  107. {
  108. if( !run )
  109. return;
  110. server->trenne();
  111. #ifdef WIN32
  112. warteAufThread( 1000 );
  113. #endif
  114. ende();
  115. server->release();
  116. server = new Server();
  117. run = 0;
  118. }
  119. void AppServer::addGesendet( int bytes )
  120. {
  121. gesendet += bytes;
  122. }
  123. void AppServer::addEmpfangen( int bytes )
  124. {
  125. empfangen += empfangen;
  126. }
  127. void AppServer::clientTrennung()
  128. {
  129. clients--;
  130. }
  131. // constant
  132. InitDatei *AppServer::zIni() const
  133. {
  134. return dat;
  135. }
  136. Server *AppServer::zServer() const
  137. {
  138. return server;
  139. }
  140. AppSDatenbank *AppServer::zDB() const
  141. {
  142. return db;
  143. }
  144. int AppServer::getClients() const
  145. {
  146. return clients;
  147. }
  148. bool AppServer::hatClients() const
  149. {
  150. return clients > 0;
  151. }
  152. // Inhalt der AppSKlient Klasse aus AppServer.h
  153. // Konstruktor
  154. AppSAKlient::AppSAKlient( SSLSKlient *klient, AppServer *appS )
  155. : Thread()
  156. {
  157. this->klient = klient;
  158. name = new Text( "" );
  159. passwort = new Text( "" );
  160. adminId = 0;
  161. this->appS = appS;
  162. }
  163. // Destruktor
  164. AppSAKlient::~AppSAKlient()
  165. {
  166. klient->trenne();
  167. klient->release();
  168. appS->release();
  169. name->release();
  170. passwort->release();
  171. }
  172. // nicht constant
  173. void AppSAKlient::thread()
  174. {
  175. while( 1 )
  176. {
  177. char c = 0;
  178. if( !klient->getNachricht( &c, 1 ) )
  179. break;
  180. else
  181. {
  182. bool br = 0;
  183. switch( c )
  184. {
  185. case 1: // Login
  186. if( 1 )
  187. {
  188. klient->sende( "\1", 1 );
  189. unsigned char nLen = 0;
  190. klient->getNachricht( (char *)&nLen, 1 );
  191. char *n = new char[ nLen + 1 ];
  192. n[ (int)nLen ] = 0;
  193. if( nLen )
  194. klient->getNachricht( n, nLen );
  195. unsigned char pLen = 0;
  196. klient->getNachricht( (char *)&pLen, 1 );
  197. char *p = new char[ pLen + 1 ];
  198. p[ (int)pLen ] = 0;
  199. if( pLen )
  200. klient->getNachricht( p, pLen );
  201. int adminId = appS->zDB()->istAdministrator( n, p );
  202. if( adminId )
  203. {
  204. klient->sende( "\1", 1 );
  205. name->setText( n );
  206. passwort->setText( p );
  207. this->adminId = adminId;
  208. }
  209. else
  210. errorZuKlient( "Falsche Kombination aus Name und Passwort." );
  211. delete[] n;
  212. delete[] p;
  213. }
  214. break;
  215. case 2: // Logout
  216. adminId = 0;
  217. name->setText( "" );
  218. passwort->setText( "" );
  219. klient->sende( "\1", 1 );
  220. break;
  221. case 3: // Trennen
  222. br = 1;
  223. klient->sende( "\1", 1 );
  224. break;
  225. case 4: // Server starten
  226. if( !adminId )
  227. errorZuKlient( "Du musst dich einloggen." );
  228. else
  229. {
  230. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSStarten ) )
  231. {
  232. if( !appS->serverStarten() )
  233. errorZuKlient( "Beim starten des Servers ist ein Fehler aufgetreten." );
  234. else
  235. klient->sende( "\1", 1 );
  236. }
  237. else
  238. errorZuKlient( "Du bist nicht berechtigt den Server zu starten." );
  239. }
  240. break;
  241. case 5: // Server beenden
  242. if( !adminId )
  243. errorZuKlient( "Du musst dich einloggen." );
  244. else
  245. {
  246. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSBeenden ) )
  247. {
  248. appS->serverBeenden();
  249. klient->sende( "\1", 1 );
  250. }
  251. else
  252. errorZuKlient( "Du bist nicht berechtigt den Server zu beenden." );
  253. }
  254. break;
  255. case 6: // Programm Schließen
  256. if( !adminId )
  257. errorZuKlient( "Du musst dich einloggen." );
  258. else
  259. {
  260. bool ok = 0;
  261. if( appS->isRunning() )
  262. {
  263. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSBeenden ) )
  264. {
  265. appS->serverBeenden();
  266. ok = 1;
  267. }
  268. else
  269. errorZuKlient( "Du bist nicht berechtigt den Server zu beenden." );
  270. }
  271. else
  272. ok = 1;
  273. if( ok && appS->hatClients() )
  274. {
  275. errorZuKlient( "Es sind noch Klients Online. Bitte versuche es später erneut." );
  276. break;
  277. }
  278. if( ok )
  279. {
  280. klient->sende( "\1", 1 );
  281. std::cout << "AppS: Der Server wird von Benutzer " << adminId << " heruntergefahren.\n";
  282. appS->close();
  283. br = 1;
  284. }
  285. }
  286. break;
  287. case 7: // Progtamm abstürzen
  288. if( !adminId )
  289. errorZuKlient( "Du musst dich einloggen." );
  290. else
  291. {
  292. bool ok = 0;
  293. if( appS->isRunning() )
  294. {
  295. if( appS->zDB()->adminHatRecht( adminId, Admin_Recht::AppSBeenden ) )
  296. {
  297. appS->serverBeenden();
  298. ok = 1;
  299. }
  300. else
  301. errorZuKlient( "Du bist nicht berechtigt den Server zu beenden." );
  302. }
  303. else
  304. ok = 1;
  305. if( ok )
  306. {
  307. klient->sende( "\1", 1 );
  308. std::cout << "AppS: Der Server wurde von Benutzer " << adminId << " terminiert.\n";
  309. appS->close();
  310. br = 1;
  311. }
  312. }
  313. break;
  314. case 8: // Status Frage
  315. if( 1 )
  316. {
  317. klient->sende( "\1", 1 );
  318. char status = (int)appS->isRunning();
  319. klient->sende( &status, 1 );
  320. }
  321. break;
  322. default:
  323. errorZuKlient( "Unbekannte Nachricht!" );
  324. br = true;
  325. break;
  326. }
  327. if( br )
  328. break;
  329. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  330. appS->addGesendet( klient->getUploadBytes( 1 ) );
  331. }
  332. }
  333. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  334. appS->addGesendet( klient->getUploadBytes( 1 ) );
  335. delete this;
  336. }
  337. void AppSAKlient::errorZuKlient( const char *nachricht ) const // sendet eine Fehlernachricht zum Klient
  338. {
  339. klient->sende( "\3", 1 );
  340. char len = (char)textLength( nachricht );
  341. klient->sende( &len, 1 );
  342. klient->sende( nachricht, len );
  343. }
  344. // Inhalt der AppSKlient Klasse aus AppServer.h
  345. // Konstruktor
  346. AppSKlient::AppSKlient( SKlient *klient, AppServer *appS )
  347. : Thread()
  348. {
  349. std::cout << "New Client aktuell:" << appS->getClients() << "\n";
  350. this->klient = klient;
  351. unsigned char key[ 20 ] = { 74, 103, 6, 115, 185, 240, 35, 53, 108, 55, 120, 253, 1, 232, 78, 254, 183, 223, 101, 9 };
  352. klient->setSendeKey( (char *)key, 20 );
  353. klient->setEmpfangKey( (char *)key, 20 );
  354. this->appS = appS;
  355. accountId = 0;
  356. tmo = new AppSKTimeOut( this );
  357. }
  358. // Destruktor
  359. AppSKlient::~AppSKlient()
  360. {
  361. tmo->stop();
  362. tmo->warteAufThread( 1000 );
  363. delete tmo;
  364. appS->clientTrennung();
  365. appS->release();
  366. klient->release();
  367. std::cout << "Client Deleted aktuell:" << appS->getClients() << "\n";
  368. }
  369. // nicht constant
  370. void AppSKlient::thread()
  371. {
  372. while( 1 )
  373. {
  374. char c = 0;
  375. if( !klient->getNachrichtEncrypted( &c, 1 ) )
  376. break;
  377. else
  378. {
  379. tmo->addConnect();
  380. bool br = 0;
  381. switch( c )
  382. {
  383. case 0: // Schlüssel Anfrage
  384. if( 1 )
  385. {
  386. Text *s = appS->zDB()->getKey();
  387. if( s )
  388. klient->sendeEncrypted( "\1", 1 );
  389. else
  390. {
  391. errorZuKlient( "Fehler beim Aufbauen der verschlüsselten Verbindung zum Server." );
  392. break;
  393. }
  394. char l = (char)s->getLength();
  395. klient->sendeEncrypted( &l, 1 );
  396. klient->sendeEncrypted( s->getText(), l );
  397. klient->setSendeKey( s->getText(), s->getLength() );
  398. klient->setEmpfangKey( s->getText(), s->getLength() );
  399. s->release();
  400. break;
  401. }
  402. case 1: // Login 5 Minigames
  403. if( 1 )
  404. {
  405. klient->sendeEncrypted( "\1", 1 );
  406. unsigned char l = 0;
  407. klient->getNachrichtEncrypted( (char *)&l, 1 );
  408. char *name = new char[ l + 1 ];
  409. name[ (int)l ] = 0;
  410. if( l )
  411. klient->getNachrichtEncrypted( name, l );
  412. klient->getNachrichtEncrypted( (char *)&l, 1 );
  413. char *passwort = new char[ l + 1 ];
  414. passwort[ (int)l ] = 0;
  415. if( l )
  416. klient->getNachrichtEncrypted( passwort, l );
  417. accountId = appS->zDB()->login( name, passwort );
  418. delete[] name;
  419. delete[] passwort;
  420. if( accountId > 0 )
  421. klient->sendeEncrypted( "\1", 1 );
  422. else
  423. klient->sende( "\0", 1 );
  424. if( accountId && appS->zDB()->istNeu( accountId ) )
  425. klient->sendeEncrypted( "\1", 1 );
  426. else if( accountId )
  427. klient->sendeEncrypted( "\0", 1 );
  428. break;
  429. }
  430. case 2: // Frage nach Welt Blöcke Score
  431. if( 1 )
  432. {
  433. klient->sendeEncrypted( "\1", 1 );
  434. int num = 0;
  435. klient->getNachrichtEncrypted( (char *)&num, 4 );
  436. Array< int > *score = new Array< int >();
  437. RCArray< Text > *namen = new RCArray< Text >();
  438. num = appS->zDB()->getWeltBloeckeScore( num, score, namen );
  439. klient->sendeEncrypted( (char *)&num, 4 );
  440. for( int i = 0; i < num; i++ )
  441. {
  442. int s = score->get( i );
  443. klient->sendeEncrypted( (char *)&s, 4 );
  444. char l = (char)namen->z( i )->getLength();
  445. klient->sendeEncrypted( &l, 1 );
  446. klient->sendeEncrypted( namen->z( i )->getText(), l );
  447. }
  448. score->release();
  449. namen->release();
  450. break;
  451. }
  452. case 3: // Frage nach Welt Mauer Score
  453. if( 1 )
  454. {
  455. klient->sendeEncrypted( "\1", 1 );
  456. int num = 0;
  457. klient->getNachrichtEncrypted( (char *)&num, 4 );
  458. Array< int > *score = new Array< int >();
  459. RCArray< Text > *namen = new RCArray< Text >();
  460. num = appS->zDB()->getWeltMauerScore( num, score, namen );
  461. klient->sendeEncrypted( (char *)&num, 4 );
  462. for( int i = 0; i < num; i++ )
  463. {
  464. int s = score->get( i );
  465. klient->sendeEncrypted( (char *)&s, 4 );
  466. char l = (char)namen->z( i )->getLength();
  467. klient->sendeEncrypted( &l, 1 );
  468. klient->sendeEncrypted( namen->z( i )->getText(), l );
  469. }
  470. score->release();
  471. namen->release();
  472. break;
  473. }
  474. case 4: // Frage nach Welt Farben Score
  475. if( 1 )
  476. {
  477. klient->sendeEncrypted( "\1", 1 );
  478. int num = 0;
  479. klient->getNachrichtEncrypted( (char *)&num, 4 );
  480. Array< int > *score = new Array< int >();
  481. RCArray< Text > *namen = new RCArray< Text >();
  482. num = appS->zDB()->getWeltFarbenScore( num, score, namen );
  483. klient->sendeEncrypted( (char *)&num, 4 );
  484. for( int i = 0; i < num; i++ )
  485. {
  486. int s = score->get( i );
  487. klient->sendeEncrypted( (char *)&s, 4 );
  488. char l = (char)namen->z( i )->getLength();
  489. klient->sendeEncrypted( &l, 1 );
  490. klient->sendeEncrypted( namen->z( i )->getText(), l );
  491. }
  492. score->release();
  493. namen->release();
  494. break;
  495. }
  496. case 5: // Frage nach Welt Fangen Score
  497. if( 1 )
  498. {
  499. klient->sendeEncrypted( "\1", 1 );
  500. int num = 0;
  501. klient->getNachrichtEncrypted( (char *)&num, 4 );
  502. Array< int > *score = new Array< int >();
  503. RCArray< Text > *namen = new RCArray< Text >();
  504. num = appS->zDB()->getWeltFangenScore( num, score, namen );
  505. klient->sendeEncrypted( (char *)&num, 4 );
  506. for( int i = 0; i < num; i++ )
  507. {
  508. int s = score->get( i );
  509. klient->sendeEncrypted( (char *)&s, 4 );
  510. char l = (char)namen->z( i )->getLength();
  511. klient->sendeEncrypted( &l, 1 );
  512. klient->sendeEncrypted( namen->z( i )->getText(), l );
  513. }
  514. score->release();
  515. namen->release();
  516. break;
  517. }
  518. case 6: // Frage nach Welt Rennen Score
  519. if( 1 )
  520. {
  521. klient->sendeEncrypted( "\1", 1 );
  522. int num = 0;
  523. klient->getNachrichtEncrypted( (char *)&num, 4 );
  524. Array< int > *score = new Array< int >();
  525. RCArray< Text > *namen = new RCArray< Text >();
  526. num = appS->zDB()->getWeltRennenScore( num, score, namen );
  527. klient->sendeEncrypted( (char *)&num, 4 );
  528. for( int i = 0; i < num; i++ )
  529. {
  530. int s = score->get( i );
  531. klient->sendeEncrypted( (char *)&s, 4 );
  532. char l = (char)namen->z( i )->getLength();
  533. klient->sendeEncrypted( &l, 1 );
  534. klient->sendeEncrypted( namen->z( i )->getText(), l );
  535. }
  536. score->release();
  537. namen->release();
  538. break;
  539. }
  540. case 7: // Frage nach Welt Gesamt Score
  541. if( 1 )
  542. {
  543. klient->sendeEncrypted( "\1", 1 );
  544. int num = 0;
  545. klient->getNachrichtEncrypted( (char *)&num, 4 );
  546. Array< int > *score = new Array< int >();
  547. RCArray< Text > *namen = new RCArray< Text >();
  548. num = appS->zDB()->getWeltGesamtScore( num, score, namen );
  549. klient->sendeEncrypted( (char *)&num, 4 );
  550. for( int i = 0; i < num; i++ )
  551. {
  552. int s = score->get( i );
  553. klient->sendeEncrypted( (char *)&s, 4 );
  554. char l = (char)namen->z( i )->getLength();
  555. klient->sendeEncrypted( &l, 1 );
  556. klient->sendeEncrypted( namen->z( i )->getText(), l );
  557. }
  558. score->release();
  559. namen->release();
  560. break;
  561. }
  562. case 8: // Frage nach Blöcke Score
  563. if( 1 )
  564. {
  565. if( accountId )
  566. klient->sendeEncrypted( "\1", 1 );
  567. else
  568. {
  569. klient->sendeEncrypted( "\0", 1 );
  570. break;
  571. }
  572. int score = appS->zDB()->getBloeckeScore( accountId );
  573. klient->sendeEncrypted( (char *)&score, 4 );
  574. break;
  575. }
  576. case 9: // Frage nach Mauer Score
  577. if( 1 )
  578. {
  579. if( accountId )
  580. klient->sendeEncrypted( "\1", 1 );
  581. else
  582. {
  583. klient->sendeEncrypted( "\0", 1 );
  584. break;
  585. }
  586. int score = appS->zDB()->getMauerScore( accountId );
  587. klient->sendeEncrypted( (char *)&score, 4 );
  588. break;
  589. }
  590. case 10: // Frage nach Farben Score
  591. if( 1 )
  592. {
  593. if( accountId )
  594. klient->sendeEncrypted( "\1", 1 );
  595. else
  596. {
  597. klient->sendeEncrypted( "\0", 1 );
  598. break;
  599. }
  600. int score = appS->zDB()->getFarbenScore( accountId );
  601. klient->sendeEncrypted( (char *)&score, 4 );
  602. break;
  603. }
  604. case 11: // Frage nach Fangen Score
  605. if( 1 )
  606. {
  607. if( accountId )
  608. klient->sendeEncrypted( "\1", 1 );
  609. else
  610. {
  611. klient->sendeEncrypted( "\0", 1 );
  612. break;
  613. }
  614. int score = appS->zDB()->getFangenScore( accountId );
  615. klient->sendeEncrypted( (char *)&score, 4 );
  616. break;
  617. }
  618. case 12: // Frage nach Rennen Score
  619. if( 1 )
  620. {
  621. if( accountId )
  622. klient->sendeEncrypted( "\1", 1 );
  623. else
  624. {
  625. klient->sendeEncrypted( "\0", 1 );
  626. break;
  627. }
  628. int score = appS->zDB()->getRennenScore( accountId );
  629. klient->sendeEncrypted( (char *)&score, 4 );
  630. break;
  631. }
  632. case 13: // Frage nach Gesamt Score
  633. if( 1 )
  634. {
  635. if( accountId )
  636. klient->sendeEncrypted( "\1", 1 );
  637. else
  638. {
  639. klient->sendeEncrypted( "\0", 1 );
  640. break;
  641. }
  642. int score = appS->zDB()->getGesamtScore( accountId );
  643. klient->sendeEncrypted( (char *)&score, 4 );
  644. break;
  645. }
  646. case 14: // Frage nach Mauer Zeit
  647. if( 1 )
  648. {
  649. if( accountId )
  650. klient->sendeEncrypted( "\1", 1 );
  651. else
  652. {
  653. klient->sendeEncrypted( "\0", 1 );
  654. break;
  655. }
  656. int level = 0;
  657. klient->getNachrichtEncrypted( (char *)&level, 4 );
  658. int sek = appS->zDB()->getMauerZeit( accountId, level );
  659. klient->sendeEncrypted( (char *)&sek, 4 );
  660. break;
  661. }
  662. case 15: // Set Blöcke Score
  663. if( 1 )
  664. {
  665. if( accountId )
  666. klient->sendeEncrypted( "\1", 1 );
  667. else
  668. {
  669. klient->sendeEncrypted( "\0", 1 );
  670. break;
  671. }
  672. int score = 0;
  673. klient->getNachrichtEncrypted( (char *)&score, 4 );
  674. appS->zDB()->setBloeckeScore( accountId, score );
  675. klient->sendeEncrypted( "\1", 1 );
  676. break;
  677. }
  678. case 16: // Set Farben Score
  679. if( 1 )
  680. {
  681. if( accountId )
  682. klient->sendeEncrypted( "\1", 1 );
  683. else
  684. {
  685. klient->sendeEncrypted( "\0", 1 );
  686. break;
  687. }
  688. int score = 0;
  689. klient->getNachrichtEncrypted( (char *)&score, 4 );
  690. appS->zDB()->setFarbenScore( accountId, score );
  691. klient->sendeEncrypted( "\1", 1 );
  692. break;
  693. }
  694. case 17: // Set Fangen Score
  695. if( 1 )
  696. {
  697. if( accountId )
  698. klient->sendeEncrypted( "\1", 1 );
  699. else
  700. {
  701. klient->sendeEncrypted( "\0", 1 );
  702. break;
  703. }
  704. int score = 0;
  705. klient->getNachrichtEncrypted( (char *)&score, 4 );
  706. appS->zDB()->setFangenScore( accountId, score );
  707. klient->sendeEncrypted( "\1", 1 );
  708. break;
  709. }
  710. case 18: // Set Rennen Score
  711. if( 1 )
  712. {
  713. if( accountId )
  714. klient->sendeEncrypted( "\1", 1 );
  715. else
  716. {
  717. klient->sendeEncrypted( "\0", 1 );
  718. break;
  719. }
  720. int score = 0;
  721. klient->getNachrichtEncrypted( (char *)&score, 4 );
  722. appS->zDB()->setRennenScore( accountId, score );
  723. klient->sendeEncrypted( "\1", 1 );
  724. break;
  725. }
  726. case 19: // Set Mauer Zeit
  727. if( 1 )
  728. {
  729. if( accountId )
  730. klient->sendeEncrypted( "\1", 1 );
  731. else
  732. {
  733. klient->sendeEncrypted( "\0", 1 );
  734. break;
  735. }
  736. int level = 0;
  737. klient->getNachrichtEncrypted( (char *)&level, 4 );
  738. int zeit = 0;
  739. klient->getNachrichtEncrypted( (char *)&zeit, 4 );
  740. appS->zDB()->setMauerZeit( accountId, level, zeit );
  741. klient->sendeEncrypted( "\1", 1 );
  742. break;
  743. }
  744. case 20: // Account erstellen
  745. if( 1 )
  746. {
  747. klient->sendeEncrypted( "\1", 1 );
  748. unsigned char len[ 4 ] = { 0, 0, 0, 0 };
  749. klient->getNachrichtEncrypted( (char *)len, 4 );
  750. char *acc_name = new char[ len[ 0 ] + 1 ];
  751. acc_name[ len[ 0 ] ] = 0;
  752. klient->getNachrichtEncrypted( acc_name, len[ 0 ] );
  753. char *acc_pass = new char[ len[ 1 ] + 1 ];
  754. acc_pass[ len[ 1 ] ] = 0;
  755. klient->getNachrichtEncrypted( acc_pass, len[ 1 ] );
  756. char *acc_geheim = new char[ len[ 2 ] + 1 ];
  757. acc_geheim[ len[ 2 ] ] = 0;
  758. klient->getNachrichtEncrypted( acc_geheim, len[ 2 ] );
  759. char *acc_mail = new char[ len[ 3 ] + 1 ];
  760. acc_mail[ len[ 3 ] ] = 0;
  761. klient->getNachrichtEncrypted( acc_mail, len[ 3 ] );
  762. unsigned short acc_geb_jahr = 0;
  763. klient->getNachrichtEncrypted( (char *)&acc_geb_jahr, 2 );
  764. char acc_geb_monat = 0;
  765. klient->getNachrichtEncrypted( &acc_geb_monat, 1 );
  766. char acc_geb_tag = 0;
  767. klient->getNachrichtEncrypted( &acc_geb_tag, 1 );
  768. int pres = appS->zDB()->pruefNeuAccount( acc_name, acc_mail );
  769. if( !pres )
  770. {
  771. Text *gebDatum = new Text( "" );
  772. gebDatum->append( (int)acc_geb_jahr );
  773. gebDatum->append( "-" );
  774. gebDatum->append( (int)acc_geb_monat );
  775. gebDatum->append( "-" );
  776. gebDatum->append( (int)acc_geb_tag );
  777. if( !appS->zDB()->neuAccount( acc_name, acc_pass, acc_geheim, acc_mail, gebDatum->getText(), appS->zIni() ) )
  778. klient->sendeEncrypted( "\0", 1 );
  779. else
  780. klient->sendeEncrypted( "\1", 1 );
  781. gebDatum->release();
  782. }
  783. else
  784. klient->sendeEncrypted( "\0", 1 );
  785. delete[]acc_name;
  786. delete[]acc_pass;
  787. delete[]acc_geheim;
  788. delete[]acc_mail;
  789. }
  790. break;
  791. case 22: // Trennen
  792. klient->sendeEncrypted( "\1", 1 );
  793. br = 1;
  794. break;
  795. case 23: // Mark Balls finish
  796. if( 1 )
  797. {
  798. klient->sendeEncrypted( "\1", 1 );
  799. int level = 0;
  800. klient->getNachrichtEncrypted( (char *)&level, 4 );
  801. int sek = 0;
  802. klient->getNachrichtEncrypted( (char *)&sek, 4 );
  803. if( accountId )
  804. appS->zDB()->markballsFinish( level, sek, accountId, 0, 0, 0, 0 );
  805. else
  806. {
  807. unsigned char l = 0;
  808. klient->getNachrichtEncrypted( (char *)&l, 1 );
  809. char *device = new char[ l + 1 ];
  810. device[ (int)l ] = 0;
  811. klient->getNachrichtEncrypted( device, l );
  812. appS->zDB()->markballsFinish( level, sek, accountId, device, 0, 0, 0 );
  813. delete[] device;
  814. }
  815. klient->sendeEncrypted( "\1", 1 );
  816. break;
  817. }
  818. case 24: // Get Mark Balls Score
  819. if( 1 )
  820. {
  821. klient->sendeEncrypted( "\1", 1 );
  822. Array< int > *level = new Array< int >();
  823. Array< int > *score = new Array< int >();
  824. RCArray< Text > *name = new RCArray< Text >();
  825. int anz = appS->zDB()->getMarkballsScore( level, score, name, 0 );
  826. klient->sendeEncrypted( (char *)&anz, 4 );
  827. for( int i = 0; i < anz; i++ )
  828. {
  829. int l = level->get( i );
  830. klient->sendeEncrypted( (char *)&l, 4 );
  831. int s = score->get( i );
  832. klient->sendeEncrypted( (char *)&s, 4 );
  833. char nl = (char)name->z( i )->getLength();
  834. klient->sendeEncrypted( &nl, 1 );
  835. if( nl )
  836. klient->sendeEncrypted( name->z( i )->getText(), nl );
  837. }
  838. level->release();
  839. score->release();
  840. name->release();
  841. break;
  842. }
  843. case 25: // Login Mark Balls
  844. if( 1 )
  845. {
  846. klient->sendeEncrypted( "\1", 1 );
  847. unsigned char l = 0;
  848. klient->getNachrichtEncrypted( (char *)&l, 1 );
  849. char *name = new char[ l + 1 ];
  850. name[ (int)l ] = 0;
  851. if( l )
  852. klient->getNachrichtEncrypted( name, l );
  853. klient->getNachrichtEncrypted( (char *)&l, 1 );
  854. char *passwort = new char[ l + 1 ];
  855. passwort[ (int)l ] = 0;
  856. if( l )
  857. klient->getNachrichtEncrypted( passwort, l );
  858. klient->getNachrichtEncrypted( (char *)&l, 1 );
  859. char *device = new char[ l + 1 ];
  860. device[ (int)l ] = 0;
  861. if( l )
  862. klient->getNachrichtEncrypted( device, l );
  863. accountId = appS->zDB()->login( name, passwort );
  864. delete[] name;
  865. delete[] passwort;
  866. if( accountId > 0 )
  867. {
  868. appS->zDB()->deviceAccount( device, accountId );
  869. klient->sendeEncrypted( "\1", 1 );
  870. }
  871. else
  872. klient->sende( "\0", 1 );
  873. delete[] device;
  874. break;
  875. }
  876. case 26: // Mark Balls finish NEW
  877. if( 1 )
  878. {
  879. klient->sendeEncrypted( "\1", 1 );
  880. int level = 0;
  881. klient->getNachrichtEncrypted( (char *)&level, 4 );
  882. int sek = 0;
  883. klient->getNachrichtEncrypted( (char *)&sek, 4 );
  884. int diff = 0;
  885. klient->getNachrichtEncrypted( (char *)&diff, 4 );
  886. int gId = 0;
  887. klient->getNachrichtEncrypted( (char *)&gId, 4 );
  888. if( accountId )
  889. {
  890. int kupfer = 0;
  891. klient->getNachrichtEncrypted( (char *)&kupfer, 4 );
  892. appS->zDB()->markballsFinish( level, sek, accountId, 0, diff, kupfer, gId );
  893. }
  894. else
  895. {
  896. unsigned char l = 0;
  897. klient->getNachrichtEncrypted( (char *)&l, 1 );
  898. char *device = new char[ l + 1 ];
  899. device[ (int)l ] = 0;
  900. klient->getNachrichtEncrypted( device, l );
  901. appS->zDB()->markballsFinish( level, sek, accountId, device, diff, 0, gId );
  902. delete[] device;
  903. }
  904. klient->sendeEncrypted( "\1", 1 );
  905. break;
  906. }
  907. case 27: // Get Mark Balls Score NEW
  908. if( 1 )
  909. {
  910. klient->sendeEncrypted( "\1", 1 );
  911. int diff = 0;
  912. klient->getNachrichtEncrypted( (char *)&diff, 4 );
  913. Array< int > *level = new Array< int >();
  914. Array< int > *score = new Array< int >();
  915. int anz = appS->zDB()->getMarkballsScore( level, score, diff );
  916. klient->sendeEncrypted( (char *)&anz, 4 );
  917. for( int i = 0; i < anz; i++ )
  918. {
  919. int l = level->get( i );
  920. klient->sendeEncrypted( (char *)&l, 4 );
  921. int s = score->get( i );
  922. klient->sendeEncrypted( (char *)&s, 4 );
  923. }
  924. level->release();
  925. score->release();
  926. break;
  927. }
  928. case 28: // Get gegenstand Liste
  929. if( 1 )
  930. {
  931. klient->sendeEncrypted( "\1", 1 );
  932. Array< int > *gId = new Array< int >();
  933. Array< int > *diff = new Array< int >();
  934. int anz = 0;
  935. if( accountId )
  936. anz = appS->zDB()->getGegenstaende( accountId, 0, gId, diff );
  937. else
  938. {
  939. unsigned char l = 0;
  940. klient->getNachrichtEncrypted( (char *)&l, 1 );
  941. char *device = new char[ l + 1 ];
  942. device[ (int)l ] = 0;
  943. klient->getNachrichtEncrypted( device, l );
  944. anz = appS->zDB()->getGegenstaende( 0, device, gId, diff );
  945. delete[] device;
  946. }
  947. klient->sendeEncrypted( (char *)&anz, 4 );
  948. for( int i = 0; i < anz; i++ )
  949. {
  950. int g = gId->get( i );
  951. klient->sendeEncrypted( (char *)&g, 4 );
  952. int d = diff->get( i );
  953. klient->sendeEncrypted( (char *)&d, 4 );
  954. }
  955. gId->release();
  956. diff->release();
  957. break;
  958. }
  959. case 29: // Get Kupfer
  960. if( 1 )
  961. {
  962. if( !accountId )
  963. klient->sendeEncrypted( "\0", 1 );
  964. else
  965. {
  966. klient->sendeEncrypted( "\1", 1 );
  967. int ret = appS->zDB()->getKupfer( accountId );
  968. klient->sendeEncrypted( (char *)&ret, 4 );
  969. }
  970. break;
  971. }
  972. case 30: // Get Markballs Fortschritt
  973. if( 1 )
  974. {
  975. klient->sendeEncrypted( "\1", 1 );
  976. Array< int > *fortschritt = new Array< int >();
  977. int anz = 0;
  978. if( accountId )
  979. anz = appS->zDB()->getMarkballsFortschritt( accountId, 0, fortschritt );
  980. else
  981. {
  982. unsigned char l = 0;
  983. klient->getNachrichtEncrypted( (char *)&l, 1 );
  984. char *device = new char[ l + 1 ];
  985. device[ (int)l ] = 0;
  986. klient->getNachrichtEncrypted( device, l );
  987. anz = appS->zDB()->getMarkballsFortschritt( 0, device, fortschritt );
  988. delete[] device;
  989. }
  990. klient->sendeEncrypted( (char *)&anz, 4 );
  991. for( int i = 0; i < anz; i++ )
  992. {
  993. int f = fortschritt->get( i );
  994. klient->sendeEncrypted( (char *)&f, 4 );
  995. }
  996. fortschritt->release();
  997. break;
  998. }
  999. case 31: // Global Login
  1000. {
  1001. klient->sendeEncrypted( "\1", 1 );
  1002. unsigned char l = 0;
  1003. klient->getNachrichtEncrypted( (char *)&l, 1 );
  1004. char *name = new char[ l + 1 ];
  1005. name[ (int)l ] = 0;
  1006. if( l )
  1007. klient->getNachrichtEncrypted( name, l );
  1008. klient->getNachrichtEncrypted( (char *)&l, 1 );
  1009. char *passwort = new char[ l + 1 ];
  1010. passwort[ (int)l ] = 0;
  1011. if( l )
  1012. klient->getNachrichtEncrypted( passwort, l );
  1013. klient->getNachrichtEncrypted( (char *)&l, 1 );
  1014. char *device = new char[ l + 1 ];
  1015. device[ (int)l ] = 0;
  1016. if( l )
  1017. klient->getNachrichtEncrypted( device, l );
  1018. accountId = appS->zDB()->login( name, passwort );
  1019. delete[] name;
  1020. delete[] passwort;
  1021. if( accountId > 0 )
  1022. klient->sendeEncrypted( "\1", 1 );
  1023. else
  1024. klient->sende( "\0", 1 );
  1025. delete[] device;
  1026. break;
  1027. }
  1028. case 35: // Add Kupfer
  1029. {
  1030. if( accountId )
  1031. {
  1032. klient->sendeEncrypted( "\1", 1 );
  1033. int kupfer = 0;
  1034. klient->getNachrichtEncrypted( (char *)&kupfer, 4 );
  1035. appS->zDB()->addKupfer( accountId, kupfer );
  1036. }
  1037. else
  1038. klient->sendeEncrypted( "\0", 1 );
  1039. break;
  1040. }
  1041. case 36: // getFreeAds
  1042. {
  1043. if( accountId )
  1044. {
  1045. if( appS->zDB()->getFreeAds( accountId ) )
  1046. klient->sendeEncrypted( "\1", 1 );
  1047. else
  1048. klient->sendeEncrypted( "\0", 1 );
  1049. }
  1050. else
  1051. klient->sendeEncrypted( "\0", 1 );
  1052. break;
  1053. }
  1054. case 37: // keppAlive
  1055. {
  1056. klient->sendeEncrypted( "\1", 1 );
  1057. break;
  1058. }
  1059. default:
  1060. klient->sendeEncrypted( "\0", 1 );
  1061. br = true;
  1062. break;
  1063. }
  1064. if( br )
  1065. break;
  1066. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  1067. appS->addGesendet( klient->getUploadBytes( 1 ) );
  1068. }
  1069. }
  1070. appS->addEmpfangen( klient->getDownloadBytes( 1 ) );
  1071. appS->addGesendet( klient->getUploadBytes( 1 ) );
  1072. delete this;
  1073. }
  1074. void AppSKlient::timeout()
  1075. {
  1076. klient->trenne();
  1077. }
  1078. void AppSKlient::errorZuKlient( const char *nachricht ) const // sendet eine Fehlernachricht zum Klient
  1079. {
  1080. klient->sendeEncrypted( "\3", 1 );
  1081. char len = (char)textLength( nachricht );
  1082. klient->sendeEncrypted( &len, 1 );
  1083. klient->sendeEncrypted( nachricht, len );
  1084. }
  1085. // Inhalt der AppSKTimeOut Klasse aus AppServer.h
  1086. // Konstruktor
  1087. AppSKTimeOut::AppSKTimeOut( AppSKlient *zK )
  1088. {
  1089. zKlient = zK;
  1090. zm = new ZeitMesser();
  1091. zm->messungStart();
  1092. st = 0;
  1093. start();
  1094. }
  1095. // Destruktor
  1096. AppSKTimeOut::~AppSKTimeOut()
  1097. {
  1098. zm->release();
  1099. }
  1100. // nicht constant
  1101. void AppSKTimeOut::thread()
  1102. {
  1103. while( !st )
  1104. {
  1105. zm->messungEnde();
  1106. if( zm->getSekunden() > 10 )
  1107. zKlient->timeout();
  1108. for( int i = 0; i < 300 && !st; i++ )
  1109. Sleep( 100 );
  1110. }
  1111. }
  1112. void AppSKTimeOut::stop()
  1113. {
  1114. st = 1;
  1115. }
  1116. void AppSKTimeOut::addConnect()
  1117. {
  1118. zm->messungStart();
  1119. }