Klient.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "Klient.h"
  2. #ifndef WIN32
  3. #include <string.h>
  4. #endif
  5. #include <Key.h>
  6. #include <Datei.h>
  7. using namespace Network;
  8. // inhalt der Klient Klasse aus Klient.h
  9. // Konstruktor
  10. Klient::Klient()
  11. {
  12. memset( &server, 0, sizeof( server ) );
  13. server.sin_family = AF_INET;
  14. ref = 1;
  15. downStreamBytes = 0;
  16. upStreamBytes = 0;
  17. sock = 0;
  18. sendeKey = 0;
  19. empfangKey = 0;
  20. }
  21. // Destruktoe
  22. Klient::~Klient()
  23. {
  24. if( sock )
  25. trenne();
  26. if( sendeKey )
  27. sendeKey->release();
  28. if( empfangKey )
  29. empfangKey->release();
  30. }
  31. // nicht constant
  32. void Klient::setSendeKeyZ( Encryption::Key *key ) // Setzt den Key fürs Senden
  33. {
  34. if( sendeKey )
  35. sendeKey->release();
  36. sendeKey = key;
  37. }
  38. void Klient::setEmpfangKeyZ( Encryption::Key *key ) // Setzt den Key fürs Empfangen
  39. {
  40. if( empfangKey )
  41. empfangKey->release();
  42. empfangKey = key;
  43. }
  44. void Klient::setSendeKey( char *key, int len ) // Setzt den Key fürs Senden
  45. {
  46. if( !sendeKey )
  47. sendeKey = new Encryption::Key();
  48. sendeKey->setKey( key, len );
  49. }
  50. void Klient::setEmpfangKey( char *key, int len ) // Setzt den Key fürs Empfangen
  51. {
  52. if( !empfangKey )
  53. empfangKey = new Encryption::Key();
  54. empfangKey->setKey( key, len );
  55. }
  56. bool Klient::verbinde( unsigned short port, const char *ip ) // verbindet mit Server
  57. {
  58. if( sendeKey )
  59. sendeKey->setPos( 0 );
  60. if( empfangKey )
  61. empfangKey->setPos( 0 );
  62. if( sock )
  63. closesocket( sock );
  64. sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  65. long sIp = inet_addr( ip ); // ip addresse
  66. memcpy( (char*)&server.sin_addr, &sIp, sizeof( sIp ) );
  67. server.sin_port = htons( port ); // port
  68. if( connect( sock, ( struct sockaddr* )&server, sizeof( server ) ) < 0 ) // verbinden
  69. return 0; // Fehler
  70. return 1;
  71. }
  72. bool Klient::sende( const char *nachricht, int len ) // sendet zum Server
  73. {
  74. int ll = 0;
  75. while( len > 0 )
  76. {
  77. #ifdef WIN32
  78. int l = send( sock, nachricht + ll, len, 0 );
  79. #else
  80. int l = (int)send( sock, nachricht + ll, len, MSG_NOSIGNAL );
  81. #endif
  82. if( l < 0 )
  83. return 0; // Fehler
  84. len -= l;
  85. ll += l;
  86. }
  87. upStreamBytes += ll;
  88. return 1;
  89. }
  90. bool Klient::getNachricht( char *nachricht, int len ) // empfängt Nachricht
  91. {
  92. int ll = 0;
  93. while( len > 0 )
  94. {
  95. int l = (int)recv( sock, nachricht + ll, len, MSG_WAITALL );
  96. if( l < 0 )
  97. return 0; // Fehler
  98. len -= l;
  99. ll += l;
  100. }
  101. downStreamBytes += ll;
  102. return 1;
  103. }
  104. bool Klient::sendeEncrypted( const char *nachricht, int len ) // sendet zum Server
  105. {
  106. if( !sendeKey )
  107. return sende( nachricht, len );
  108. Encryption::Bytes *n = new Encryption::Bytes( nachricht, len );
  109. sendeKey->codieren( n->getThis() );
  110. int ll = 0;
  111. while( len > 0 )
  112. {
  113. #ifdef WIN32
  114. int l = send( sock, n->getBytes() + ll, len, 0 );
  115. #else
  116. int l = (int)send( sock, n->getBytes() + ll, len, MSG_NOSIGNAL );
  117. #endif
  118. if( l < 0 )
  119. {
  120. n->release();
  121. return 0; // Fehler
  122. }
  123. len -= l;
  124. ll += l;
  125. }
  126. upStreamBytes += ll;
  127. n->release();
  128. return 1;
  129. }
  130. bool Klient::getNachrichtEncrypted( char *nachricht, int len ) // empfängt Nachricht
  131. {
  132. if( !empfangKey )
  133. return getNachricht( nachricht, len );
  134. int ll = 0;
  135. while( len > 0 )
  136. {
  137. int l = (int)recv( sock, nachricht + ll, len, MSG_WAITALL );
  138. if( l < 0 )
  139. return 0; // Fehler
  140. len -= l;
  141. ll += l;
  142. }
  143. Encryption::Bytes *n = new Encryption::Bytes();
  144. n->setBytesZ( nachricht, ll );
  145. empfangKey->decodieren( n );
  146. downStreamBytes += ll;
  147. return 1;
  148. }
  149. int Klient::getDownloadBytes( bool reset ) // gibt die anzahl von empfangen bytes zurück
  150. {
  151. int ret = downStreamBytes;
  152. if( reset )
  153. downStreamBytes = 0;
  154. return ret;
  155. }
  156. int Klient::getUploadBytes( bool reset ) // gibt die anzahl von versendeter bytes zurück
  157. {
  158. int ret = upStreamBytes;
  159. if( reset )
  160. upStreamBytes = 0;
  161. return ret;
  162. }
  163. bool Klient::trenne() // Trennt die Verbindung zum Server
  164. {
  165. if( !sock )
  166. return 1;
  167. if( sendeKey )
  168. sendeKey->setPos( 0 );
  169. if( empfangKey )
  170. empfangKey->setPos( 0 );
  171. if( closesocket( sock ) < 0 ) // verbindung Trennen
  172. return 0; // Fehler
  173. sock = 0;
  174. return 1;
  175. }
  176. // constant
  177. bool Klient::hatNachricht( int zeit ) // Wartet eine Zeit Lang auf eine Nachricht
  178. {
  179. #ifdef WIN32
  180. fd_set set = { 1, { sock } };
  181. timeval time = { zeit / 1000, zeit };
  182. return select( 0, &set, 0, 0, &time ) == 1;
  183. #else
  184. return 1;
  185. #endif
  186. }
  187. unsigned short Klient::getServerPort() const // gibt den Port zurück
  188. {
  189. return htons( server.sin_port );
  190. }
  191. const char *Klient::getServerIp() const // gibt die Ip zurück
  192. {
  193. return inet_ntoa( server.sin_addr );
  194. }
  195. // Reference Counting
  196. Klient *Klient::getThis()
  197. {
  198. ref++;
  199. return this;
  200. }
  201. Klient *Klient::release()
  202. {
  203. ref--;
  204. if( !ref )
  205. delete this;
  206. return 0;
  207. }