123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #include "Server.h"
- #ifndef WIN32
- #include <string.h>
- #endif
- #include <Key.h>
- using namespace Network;
- Server::Server()
- {
- sock = 0;
- memset( &addresse, 0, sizeof( addresse ) );
- addresse.sin_family = AF_INET;
- addresse.sin_addr.s_addr = ADDR_ANY;
- ref = 1;
- klients = 0;
- }
- Server::~Server()
- {
- }
- bool Server::verbinde( unsigned short port, int warteschlangenLen )
- {
- sock = socket( AF_INET, SOCK_STREAM, 0 );
- addresse.sin_port = htons( port );
- if( bind( sock, ( struct sockaddr* )&addresse, sizeof( addresse ) ) == -1 )
- return 0;
- if( listen( sock, warteschlangenLen ) == -1 )
- return 0;
- return 1;
- }
- SKlient *Server::getKlient()
- {
- if( !sock )
- return 0;
- sockaddr_in client;
- int len = sizeof( addresse );
- #ifdef WIN32
- SOCKET cls = accept( sock, (sockaddr*)&client, &len );
- if( cls == INVALID_SOCKET )
- return 0;
- #else
- SOCKET cls = accept( sock, (sockaddr*)&client, (socklen_t*)&len );
- if( !cls )
- return 0;
- #endif
- client.sin_port = addresse.sin_port;
- klients++;
- return new SKlient( client, cls );
- }
- int Server::getKlients( bool reset )
- {
- int ret = klients;
- if( reset )
- klients = 0;
- return ret;
- }
- bool Server::trenne()
- {
- if( !sock )
- return 1;
- if( closesocket( sock ) < 0 )
- return 0;
- sock = 0;
- return 1;
- }
- unsigned short Server::getPort() const
- {
- return htons( addresse.sin_port );
- }
- Server *Server::getThis()
- {
- ref++;
- return this;
- }
- Server *Server::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
- SKlient::SKlient( sockaddr_in addresse, SOCKET sock )
- {
- clientAddr = addresse;
- this->sock = sock;
- ref = 1;
- downStreamBytes = 0;
- upStreamBytes = 0;
- sendeKey = 0;
- empfangKey = 0;
- }
- SKlient::~SKlient()
- {
- trenne();
- if( sendeKey )
- sendeKey->release();
- if( empfangKey )
- empfangKey->release();
- }
- void SKlient::setSendeKeyZ( Encryption::Key *key )
- {
- if( sendeKey )
- sendeKey->release();
- sendeKey = key;
- }
- void SKlient::setEmpfangKeyZ( Encryption::Key *key )
- {
- if( empfangKey )
- empfangKey->release();
- empfangKey = key;
- }
- void SKlient::setSendeKey( char *key, int len )
- {
- if( !sendeKey )
- sendeKey = new Encryption::Key();
- sendeKey->setKey( key, len );
- }
- void SKlient::setEmpfangKey( char *key, int len )
- {
- if( !empfangKey )
- empfangKey = new Encryption::Key();
- empfangKey->setKey( key, len );
- }
- bool SKlient::sende( const char *nachricht, int len )
- {
- if( !sock )
- return 0;
- int ll = 0;
- while( len > 0 )
- {
- #ifdef WIN32
- int l = send( sock, nachricht + ll, len, 0 );
- #else
- int l = (int)send( sock, nachricht + ll, len, MSG_NOSIGNAL );
- #endif
- if( l < 0 )
- return 0;
- len -= l;
- ll += l;
- }
- upStreamBytes += ll;
- return 1;
- }
- bool SKlient::getNachricht( char *nachricht, int len )
- {
- if( !sock )
- return 0;
- int ll = 0;
- while( len > 0 )
- {
- int l = (int)recv( sock, nachricht + ll, len, MSG_WAITALL );
- if( l < 0 )
- return 0;
- len -= l;
- ll += l;
- }
- downStreamBytes += ll;
- return 1;
- }
- bool SKlient::sendeEncrypted( const char *nachricht, int len )
- {
- if( !sendeKey )
- return sende( nachricht, len );
- Encryption::Bytes *n = new Encryption::Bytes( nachricht, len );
- sendeKey->codieren( n->getThis() ); int ll = 0;
- while( len > 0 )
- {
- #ifdef WIN32
- int l = send( sock, n->getBytes() + ll, len, 0 );
- #else
- int l = (int)send( sock, n->getBytes() + ll, len, MSG_NOSIGNAL );
- #endif
- if( l < 0 )
- {
- n->release();
- return 0;
- }
- len -= l;
- ll += l;
- }
- upStreamBytes += ll;
- n->release();
- return 1;
- }
- bool SKlient::getNachrichtEncrypted( char *nachricht, int len )
- {
- if( !empfangKey )
- return getNachricht( nachricht, len );
- int ll = 0;
- while( len > 0 )
- {
- int l = (int)recv( sock, nachricht + ll, len, MSG_WAITALL );
- if( l < 0 )
- return 0;
- len -= l;
- ll += l;
- }
- Encryption::Bytes *n = new Encryption::Bytes();
- n->setBytesZ( nachricht, ll );
- empfangKey->decodieren( n );
- downStreamBytes += ll;
- return 1;
- }
- int SKlient::getDownloadBytes( bool reset )
- {
- int ret = downStreamBytes;
- if( reset )
- downStreamBytes = 0;
- return ret;
- }
- int SKlient::getUploadBytes( bool reset )
- {
- int ret = upStreamBytes;
- if( reset )
- upStreamBytes = 0;
- return ret;
- }
- bool SKlient::trenne()
- {
- if( !sock )
- return 0;
- if( closesocket( sock ) < 0 )
- return 0;
- sock = 0;
- return 1;
- }
- unsigned short SKlient::getPort() const
- {
- return htons( clientAddr.sin_port );
- }
- const char *SKlient::getIp() const
- {
- return inet_ntoa( clientAddr.sin_addr );
- }
- SKlient *SKlient::getThis()
- {
- ref++;
- return this;
- }
- SKlient *SKlient::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
|