ESClient.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. #include <Klient.h>
  3. #include <Thread.h>
  4. #include <iostream>
  5. class ESClient : public Framework::Thread
  6. {
  7. private:
  8. Network::Klient *k;
  9. int klientId;
  10. bool abmel;
  11. bool trenn;
  12. unsigned short port;
  13. char *ip;
  14. char *key;
  15. unsigned char keyLen;
  16. int ref;
  17. public:
  18. // Konstruktor
  19. ESClient( int klientId, unsigned short port, char *ip, char *key, unsigned char keyLen )
  20. {
  21. k = 0;
  22. this->klientId = klientId;
  23. this->port = port;
  24. this->ip = new char[ textLength( ip ) + 1 ];
  25. memcpy( this->ip, ip, textLength( ip ) + 1 );
  26. this->keyLen = keyLen;
  27. this->key = new char[ keyLen ];
  28. memcpy( this->key, key, keyLen );
  29. abmel = 0;
  30. trenn = 0;
  31. ref = 1;
  32. }
  33. // Destruktor
  34. ~ESClient()
  35. {
  36. delete[] key;
  37. if( k )
  38. abmelden();
  39. delete[] ip;
  40. warteAufThread( 10000 );
  41. ende();
  42. if( k )
  43. k->release();
  44. }
  45. // nicht constant
  46. bool verbinden()
  47. {
  48. if( k )
  49. return 1;
  50. k = new Network::Klient();
  51. int l = 0;
  52. char *key;
  53. Keys::getServerKey( &key, l, Keys::ERHALTUNG, Keys::SENDEN );
  54. k->setSendeKey( key, l );
  55. delete[] key;
  56. Keys::getServerKey( &key, l, Keys::ERHALTUNG, Keys::EMPFANGEN );
  57. k->setEmpfangKey( key, l );
  58. delete[] key;
  59. if( k->verbinde( port, ip ) )
  60. {
  61. k->sende( "\0", 1 ); // Verschlüsselung Aktivieren
  62. if( k->sendeEncrypted( "\1", 1 ) )
  63. {
  64. k->sendeEncrypted( (char*)&klientId, 4 );
  65. char serverReturn = 0;
  66. k->getNachrichtEncrypted( &serverReturn, 1 );
  67. if( serverReturn == 3 )
  68. {
  69. char byte = 0;
  70. k->getNachrichtEncrypted( &byte, 1 );
  71. char *f = new char[ byte + 1 ];
  72. f[ byte ] = 0;
  73. k->getNachrichtEncrypted( f, byte );
  74. std::cerr << "error while identifyin client Erhaltung Server returned: " << f << "\n";
  75. delete[]f;
  76. k->sendeEncrypted( "\3", 1 );
  77. k->getNachrichtEncrypted( &serverReturn, 1 );
  78. k->trenne();
  79. return 0;
  80. }
  81. k->setSendeKey( (char*)this->key, keyLen );
  82. k->setEmpfangKey( (char*)this->key, keyLen );
  83. start();
  84. }
  85. else
  86. {
  87. std::cerr << "network error while sending to Erhaltung Server\n";
  88. k = k->release();
  89. return 0;
  90. }
  91. }
  92. else
  93. {
  94. std::cerr << "network error while connecting to Erhaltung Server\n";
  95. k = k->release();
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. void abmelden()
  101. {
  102. abmel = 1;
  103. trenn = 1;
  104. }
  105. void trennen()
  106. {
  107. trenn = 1;
  108. }
  109. virtual void thread()
  110. {
  111. while( 1 )
  112. {
  113. char n = 0;
  114. k->getNachrichtEncrypted( &n, 1 );
  115. if( n != 1 )
  116. {
  117. Sleep( 250 );
  118. continue;
  119. }
  120. if( trenn )
  121. {
  122. if( abmel )
  123. {
  124. k->sendeEncrypted( "\1", 1 );
  125. k->getNachrichtEncrypted( &n, 1 );
  126. }
  127. k->trenne();
  128. run = 0;
  129. return;
  130. }
  131. else
  132. k->sendeEncrypted( "\0", 1 );
  133. }
  134. }
  135. // Reference Counting
  136. ESClient *getThis()
  137. {
  138. ref++;
  139. return this;
  140. }
  141. ESClient *release()
  142. {
  143. if( !--ref )
  144. delete this;
  145. return 0;
  146. }
  147. };