PSClient.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #pragma once
  2. #include <Klient.h>
  3. #include <Text.h>
  4. #include "Keys.h"
  5. #include <iostream>
  6. #include <KSGTDatei.h>
  7. #include <Datei.h>
  8. class PSClient
  9. {
  10. private:
  11. int id;
  12. Network::Klient *klient;
  13. char *schlüssel;
  14. unsigned char schlüsselLän;
  15. int port;
  16. char *ip;
  17. int ref;
  18. public:
  19. // Konstruktor
  20. PSClient( int klientId, unsigned short port, char *ip, char *key, unsigned char keyLen )
  21. {
  22. klient = 0;
  23. id = klientId;
  24. this->port = port;
  25. this->ip = new char[ textLength( ip ) + 1 ];
  26. memcpy( this->ip, ip, textLength( ip ) + 1 );
  27. schlüsselLän = keyLen;
  28. schlüssel = new char[ keyLen ];
  29. memcpy( schlüssel, key, keyLen );
  30. ref = 1;
  31. }
  32. // Destruktor
  33. ~PSClient()
  34. {
  35. delete[] schlüssel;
  36. if( klient )
  37. trenne( 1 );
  38. delete[] ip;
  39. if( klient )
  40. klient->release();
  41. }
  42. // nicht constant
  43. bool verbinde()
  44. {
  45. if( klient )
  46. return 1;
  47. klient = new Network::Klient();
  48. int l = 0;
  49. char *key;
  50. Keys::getServerKey( &key, l, Keys::PATCH, Keys::SENDEN );
  51. klient->setSendeKey( key, l );
  52. delete[] key;
  53. Keys::getServerKey( &key, l, Keys::PATCH, Keys::EMPFANGEN );
  54. klient->setEmpfangKey( key, l );
  55. delete[] key;
  56. if( klient->verbinde( port, ip ) )
  57. {
  58. klient->sende( "\0", 1 ); // Verschlüsselung Aktivieren
  59. if( klient->sendeEncrypted( "\1", 1 ) )
  60. {
  61. klient->sendeEncrypted( (char*)&id, 4 );
  62. klient->sendeEncrypted( "\2", 1 ); // 64 Bit Client
  63. char serverReturn = 0;
  64. klient->getNachrichtEncrypted( &serverReturn, 1 );
  65. if( serverReturn == 3 )
  66. {
  67. char byte = 0;
  68. klient->getNachrichtEncrypted( &byte, 1 );
  69. char *f = new char[ byte + 1 ];
  70. f[ byte ] = 0;
  71. klient->getNachrichtEncrypted( f, byte );
  72. std::cerr << "error while identifyin client Patch Server returned: " << f << "\n";
  73. delete[]f;
  74. klient->sendeEncrypted( "\3", 1 );
  75. klient->getNachrichtEncrypted( &serverReturn, 1 );
  76. klient->trenne();
  77. return 0;
  78. }
  79. klient->setSendeKey( (char*)schlüssel, schlüsselLän );
  80. klient->setEmpfangKey( (char*)schlüssel, schlüsselLän );
  81. }
  82. else
  83. {
  84. std::cerr << "network error while sending to Patch Server\n";
  85. klient = klient->release();
  86. return 0;
  87. }
  88. }
  89. else
  90. {
  91. std::cerr << "network error while connecting to Patch Server\n";
  92. klient = klient->release();
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. KSGTDatei *getDateiGruppenListe()
  98. {
  99. verbinde();
  100. if( !klient )
  101. return 0;
  102. klient->sendeEncrypted( "\xC", 1 );
  103. char ret = 0;
  104. klient->getNachrichtEncrypted( &ret, 1 );
  105. if( ret == 3 )
  106. { // error
  107. klient->getNachrichtEncrypted( &ret, 1 );
  108. char *msg = new char[ ret + 1 ];
  109. msg[ ret ] = 0;
  110. if( ret )
  111. klient->getNachrichtEncrypted( msg, ret );
  112. std::cerr << "error while getDateiGruppenListe Patch Server returned: " << msg << "\n";
  113. delete[] msg;
  114. return 0;
  115. }
  116. KSGTDatei *dgtb = new KSGTDatei();
  117. int id = 0;
  118. klient->getNachrichtEncrypted( (char*)&id, 4 );
  119. while( id )
  120. {
  121. RCArray< Text > *zeile = new RCArray< Text >();
  122. Text *idT = new Text();
  123. idT->append( id );
  124. zeile->set( idT, 0 );
  125. int län = 0;
  126. klient->getNachrichtEncrypted( (char*)&län, 4 );
  127. char *pfad = new char[ län + 1 ];
  128. pfad[ län ] = 0;
  129. if( län )
  130. klient->getNachrichtEncrypted( pfad, län );
  131. zeile->set( new Text( pfad ), 1 );
  132. delete[] pfad;
  133. int vs = 0;
  134. klient->getNachrichtEncrypted( (char*)&vs, 4 );
  135. Text *vsT = new Text();
  136. vsT->append( vs );
  137. zeile->set( vsT, 2 );
  138. dgtb->addZeile( 3, zeile );
  139. zeile->release();
  140. klient->getNachrichtEncrypted( (char*)&id, 4 );
  141. }
  142. klient->sendeEncrypted( "\1", 1 );
  143. return dgtb;
  144. }
  145. KSGTDatei *getDateiListe( int gruppe )
  146. {
  147. verbinde();
  148. if( !klient )
  149. return 0;
  150. klient->sendeEncrypted( "\x9", 1 );
  151. char ret = 0;
  152. klient->getNachrichtEncrypted( &ret, 1 );
  153. if( ret == 1 )
  154. {
  155. klient->sendeEncrypted( (char*)&gruppe, 4 );
  156. klient->getNachrichtEncrypted( &ret, 1 );
  157. }
  158. if( ret == 3 )
  159. { // error
  160. klient->getNachrichtEncrypted( &ret, 1 );
  161. char *msg = new char[ ret + 1 ];
  162. msg[ ret ] = 0;
  163. if( ret )
  164. klient->getNachrichtEncrypted( msg, ret );
  165. std::cerr << "error while getDateiListe Patch Server returned: " << msg << "\n";
  166. delete[] msg;
  167. return 0;
  168. }
  169. KSGTDatei *dtb = new KSGTDatei();
  170. int län = 0;
  171. klient->getNachrichtEncrypted( (char*)&län, 4 );
  172. while( län )
  173. {
  174. RCArray< Text > *zeile = new RCArray< Text >();
  175. zeile->set( new Text( "1" ), 0 );
  176. char *pfad = new char[ län + 1 ];
  177. pfad[ län ] = 0;
  178. klient->getNachrichtEncrypted( pfad, län );
  179. zeile->set( new Text( pfad ), 1 );
  180. delete[] pfad;
  181. int vs = 0;
  182. klient->getNachrichtEncrypted( (char*)&vs, 4 );
  183. Text *vsT = new Text();
  184. vsT->append( vs );
  185. zeile->set( vsT, 2 );
  186. dtb->addZeile( 3, zeile );
  187. zeile->release();
  188. klient->getNachrichtEncrypted( (char*)&län, 4 );
  189. }
  190. klient->getNachrichtEncrypted( (char*)&län, 4 );
  191. while( län )
  192. {
  193. RCArray< Text > *zeile = new RCArray< Text >();
  194. zeile->set( new Text( "0" ), 0 );
  195. char *pfad = new char[ län + 1 ];
  196. pfad[ län ] = 0;
  197. klient->getNachrichtEncrypted( pfad, län );
  198. zeile->set( new Text( pfad ), 1 );
  199. delete[] pfad;
  200. dtb->addZeile( 2, zeile );
  201. zeile->release();
  202. klient->getNachrichtEncrypted( (char*)&län, 4 );
  203. }
  204. return dtb;
  205. }
  206. __int64 getDateiGröße( int gruppe, const char *pfad )
  207. {
  208. verbinde();
  209. if( !klient )
  210. return 0;
  211. klient->sendeEncrypted( "\xA", 1 );
  212. char ret = 0;
  213. klient->getNachrichtEncrypted( &ret, 1 );
  214. if( ret == 1 )
  215. {
  216. klient->sendeEncrypted( (char*)&gruppe, 4 );
  217. klient->getNachrichtEncrypted( &ret, 1 );
  218. }
  219. if( ret == 3 )
  220. { // error
  221. klient->getNachrichtEncrypted( &ret, 1 );
  222. char *msg = new char[ ret + 1 ];
  223. msg[ ret ] = 0;
  224. if( ret )
  225. klient->getNachrichtEncrypted( msg, ret );
  226. std::cerr << "error while getDateiGröße Patch Server returned: " << msg << "\n";
  227. delete[] msg;
  228. return 0;
  229. }
  230. if( ret == 2 )
  231. { // verweis auf anderen Server
  232. klient->getNachrichtEncrypted( (char*)&port, 4 );
  233. int län = 0;
  234. klient->getNachrichtEncrypted( (char*)&län, 4 );
  235. delete[] ip;
  236. ip = new char[ län + 1 ];
  237. ip[ län ] = 0;
  238. klient->getNachrichtEncrypted( ip, län );
  239. if( !trenne( 0 ) )
  240. {
  241. delete[] ip;
  242. return 0;
  243. }
  244. if( !verbinde() )
  245. {
  246. delete[] ip;
  247. return 0;
  248. }
  249. delete[] ip;
  250. return getDateiGröße( gruppe, pfad );
  251. }
  252. int län = textLength( pfad );
  253. klient->sendeEncrypted( (char*)&län, 4 );
  254. klient->sendeEncrypted( pfad, län );
  255. __int64 größe = 0;
  256. klient->getNachrichtEncrypted( (char*)&größe, 8 );
  257. return größe;
  258. }
  259. bool downloadDatei( int gruppe, __int64 *start, const char *pfad, const char *zielPfad )
  260. {
  261. verbinde();
  262. if( !klient )
  263. return 0;
  264. Datei *zielD = new Datei();
  265. zielD->setDatei( zielPfad );
  266. if( !zielD->existiert() )
  267. zielD->erstellen();
  268. bool ok = 0;
  269. if( !( *start ) ) // datei neu erstellen
  270. ok = zielD->open( Datei::Style::schreiben );
  271. else // datei weiterschreiben
  272. ok = zielD->open( Datei::Style::schreiben | Datei::Style::lesen );
  273. if( !ok )
  274. {
  275. zielD->release();
  276. return 0;
  277. }
  278. zielD->setSPosition( *start, 0 );
  279. klient->sendeEncrypted( "\xB", 1 );
  280. char ret = 0;
  281. klient->getNachrichtEncrypted( &ret, 1 );
  282. if( ret == 1 )
  283. {
  284. klient->sendeEncrypted( (char*)&gruppe, 4 );
  285. klient->getNachrichtEncrypted( &ret, 1 );
  286. }
  287. if( ret == 3 )
  288. { // error
  289. klient->getNachrichtEncrypted( &ret, 1 );
  290. char *msg = new char[ ret + 1 ];
  291. msg[ ret ] = 0;
  292. if( ret )
  293. klient->getNachrichtEncrypted( msg, ret );
  294. std::cerr << "error while downloadDatei Patch Server returned: " << msg << "\n";
  295. delete[] msg;
  296. zielD->close();
  297. zielD->release();
  298. return 0;
  299. }
  300. if( ret == 2 )
  301. { // verweis auf anderen Server
  302. klient->getNachrichtEncrypted( (char*)&port, 4 );
  303. int län = 0;
  304. klient->getNachrichtEncrypted( (char*)&län, 4 );
  305. delete[] ip;
  306. ip = new char[ län + 1 ];
  307. ip[ län ] = 0;
  308. klient->getNachrichtEncrypted( ip, län );
  309. if( !trenne( 0 ) )
  310. {
  311. delete[] ip;
  312. zielD->close();
  313. zielD->release();
  314. return 0;
  315. }
  316. if( !verbinde() )
  317. {
  318. delete[] ip;
  319. zielD->close();
  320. zielD->release();
  321. return 0;
  322. }
  323. delete[] ip;
  324. zielD->close();
  325. zielD->release();
  326. return downloadDatei( gruppe, start, pfad, zielPfad );
  327. }
  328. int län = textLength( pfad );
  329. klient->sendeEncrypted( (char*)&län, 4 );
  330. klient->sendeEncrypted( pfad, län );
  331. klient->sendeEncrypted( (char*)start, 8 );
  332. char *bytes = new char[ 2048 ];
  333. unsigned short byteLän = 0;
  334. klient->getNachricht( (char*)&byteLän, 2 );
  335. if( byteLän )
  336. {
  337. int bps = 0;
  338. double sec = 0;
  339. while( 1 )
  340. {
  341. klient->getNachricht( bytes, byteLän );
  342. zielD->schreibe( bytes, byteLän );
  343. ( *start ) += byteLän;
  344. bps += byteLän;
  345. if( byteLän == 2048 )
  346. {
  347. klient->getNachricht( (char*)&byteLän, 2 ); // sendelänge empfangen
  348. if( !byteLän )
  349. break;
  350. }
  351. else
  352. break;
  353. }
  354. }
  355. delete[] bytes;
  356. klient->sendeEncrypted( "\1", 1 );
  357. zielD->close();
  358. zielD->release();
  359. return 1;
  360. }
  361. bool trenne( bool abmelden )
  362. {
  363. verbinde();
  364. if( !klient )
  365. return 1;
  366. if( abmelden )
  367. {
  368. klient->sendeEncrypted( "\4", 1 );
  369. char ret = 0;
  370. klient->getNachrichtEncrypted( &ret, 1 );
  371. if( ret == 3 )
  372. { // error
  373. klient->getNachrichtEncrypted( &ret, 1 );
  374. char *msg = new char[ ret + 1 ];
  375. msg[ ret ] = 0;
  376. if( ret )
  377. klient->getNachrichtEncrypted( msg, ret );
  378. std::cerr << "error while unregister Client Patch Server returned: " << msg << "\n";
  379. delete[] msg;
  380. }
  381. }
  382. klient->sendeEncrypted( "\3", 1 );
  383. char ret = 0;
  384. klient->getNachrichtEncrypted( &ret, 1 );
  385. if( ret == 3 )
  386. { // error
  387. klient->getNachrichtEncrypted( &ret, 1 );
  388. char *msg = new char[ ret + 1 ];
  389. msg[ ret ] = 0;
  390. if( ret )
  391. klient->getNachrichtEncrypted( msg, ret );
  392. std::cerr << "error while trenne Patch Server returned: " << msg << "\n";
  393. delete[] msg;
  394. }
  395. klient->trenne();
  396. klient = klient->release();
  397. return 1;
  398. }
  399. // Reference Counting
  400. PSClient *getThis()
  401. {
  402. ref++;
  403. return this;
  404. }
  405. PSClient *release()
  406. {
  407. ref--;
  408. if( !ref )
  409. delete this;
  410. return 0;
  411. }
  412. };