123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #pragma once
- #include <Klient.h>
- #include <Thread.h>
- #include <iostream>
- class ESClient : public Framework::Thread
- {
- private:
- Network::Klient *k;
- int klientId;
- bool abmel;
- bool trenn;
- unsigned short port;
- char *ip;
- char *key;
- unsigned char keyLen;
- int ref;
- public:
- // Konstruktor
- ESClient( int klientId, unsigned short port, char *ip, char *key, unsigned char keyLen )
- {
- k = 0;
- this->klientId = klientId;
- this->port = port;
- this->ip = new char[ textLength( ip ) + 1 ];
- memcpy( this->ip, ip, textLength( ip ) + 1 );
- this->keyLen = keyLen;
- this->key = new char[ keyLen ];
- memcpy( this->key, key, keyLen );
- abmel = 0;
- trenn = 0;
- ref = 1;
- }
- // Destruktor
- ~ESClient()
- {
- delete[] key;
- if( k )
- abmelden();
- delete[] ip;
- warteAufThread( 10000 );
- ende();
- if( k )
- k->release();
- }
- // nicht constant
- bool verbinden()
- {
- if( k )
- return 1;
- k = new Network::Klient();
- int l = 0;
- char *key;
- Keys::getServerKey( &key, l, Keys::ERHALTUNG, Keys::SENDEN );
- k->setSendeKey( key, l );
- delete[] key;
- Keys::getServerKey( &key, l, Keys::ERHALTUNG, Keys::EMPFANGEN );
- k->setEmpfangKey( key, l );
- delete[] key;
- if( k->verbinde( port, ip ) )
- {
- k->sende( "\0", 1 ); // Verschlüsselung Aktivieren
- if( k->sendeEncrypted( "\1", 1 ) )
- {
- k->sendeEncrypted( (char*)&klientId, 4 );
- char serverReturn = 0;
- k->getNachrichtEncrypted( &serverReturn, 1 );
- if( serverReturn == 3 )
- {
- char byte = 0;
- k->getNachrichtEncrypted( &byte, 1 );
- char *f = new char[ byte + 1 ];
- f[ byte ] = 0;
- k->getNachrichtEncrypted( f, byte );
- std::cerr << "error while identifyin client Erhaltung Server returned: " << f << "\n";
- delete[]f;
- k->sendeEncrypted( "\3", 1 );
- k->getNachrichtEncrypted( &serverReturn, 1 );
- k->trenne();
- return 0;
- }
- k->setSendeKey( (char*)this->key, keyLen );
- k->setEmpfangKey( (char*)this->key, keyLen );
- start();
- }
- else
- {
- std::cerr << "network error while sending to Erhaltung Server\n";
- k = k->release();
- return 0;
- }
- }
- else
- {
- std::cerr << "network error while connecting to Erhaltung Server\n";
- k = k->release();
- return 0;
- }
- return 1;
- }
- void abmelden()
- {
- abmel = 1;
- trenn = 1;
- }
- void trennen()
- {
- trenn = 1;
- }
- virtual void thread()
- {
- while( 1 )
- {
- char n = 0;
- k->getNachrichtEncrypted( &n, 1 );
- if( n != 1 )
- {
- Sleep( 250 );
- continue;
- }
- if( trenn )
- {
- if( abmel )
- {
- k->sendeEncrypted( "\1", 1 );
- k->getNachrichtEncrypted( &n, 1 );
- }
- k->trenne();
- run = 0;
- return;
- }
- else
- k->sendeEncrypted( "\0", 1 );
- }
- }
- // Reference Counting
- ESClient *getThis()
- {
- ref++;
- return this;
- }
- ESClient *release()
- {
- if( !--ref )
- delete this;
- return 0;
- }
- };
|