ESClient.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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( 5000 );
  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. if( k->verbinde( port, ip ) )
  52. {
  53. k->sende( "\0", 1 ); // Verschlüsselung Aktivieren
  54. if( k->sendeEncrypted( "\1", 1 ) )
  55. {
  56. k->sendeEncrypted( (char*)&klientId, 4 );
  57. char serverReturn = 0;
  58. k->getNachrichtEncrypted( &serverReturn, 1 );
  59. if( serverReturn == 3 )
  60. {
  61. char byte = 0;
  62. k->getNachrichtEncrypted( &byte, 1 );
  63. char *f = new char[ byte + 1 ];
  64. f[ byte ] = 0;
  65. k->getNachrichtEncrypted( f, byte );
  66. std::cerr << "error while identifyin client Erhaltung Server returned: " << f << "\n";
  67. delete[]f;
  68. k->sendeEncrypted( "\3", 1 );
  69. k->getNachrichtEncrypted( &serverReturn, 1 );
  70. k->trenne();
  71. return 0;
  72. }
  73. k->setSendeKey( (char*)key, keyLen );
  74. k->setEmpfangKey( (char*)key, keyLen );
  75. start();
  76. }
  77. else
  78. {
  79. std::cerr << "network error while sending to Erhaltung Server\n";
  80. k = k->release();
  81. return 0;
  82. }
  83. }
  84. else
  85. {
  86. std::cerr << "network error while connecting to Erhaltung Server\n";
  87. k = k->release();
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. void abmelden()
  93. {
  94. abmel = 1;
  95. trenn = 1;
  96. }
  97. void trennen()
  98. {
  99. trenn = 1;
  100. }
  101. virtual void thread()
  102. {
  103. while( 1 )
  104. {
  105. char n = 0;
  106. k->getNachrichtEncrypted( &n, 1 );
  107. if( n != 1 )
  108. {
  109. Sleep( 250 );
  110. continue;
  111. }
  112. if( trenn )
  113. {
  114. if( abmel )
  115. {
  116. k->sendeEncrypted( "\1", 1 );
  117. k->getNachrichtEncrypted( &n, 1 );
  118. }
  119. k->trenne();
  120. run = 0;
  121. return;
  122. }
  123. else
  124. k->sendeEncrypted( "\0", 1 );
  125. }
  126. }
  127. // Reference Counting
  128. ESClient *getThis()
  129. {
  130. ref++;
  131. return this;
  132. }
  133. ESClient *release()
  134. {
  135. if( !--ref )
  136. delete this;
  137. return 0;
  138. }
  139. };