Преглед изворни кода

reference counting improvement

Kolja Strohm пре 3 година
родитељ
комит
511b145eb0
8 измењених фајлова са 254 додато и 408 уклоњено
  1. 4 29
      Network/HttpRequest.cpp
  2. 2 10
      Network/HttpRequest.h
  3. 85 114
      Network/Klient.cpp
  4. 44 51
      Network/Klient.h
  5. 13 71
      Network/Server.cpp
  6. 62 77
      Network/Server.h
  7. 44 54
      Network/WebSocket.cpp
  8. 0 2
      Network/WebSocket.h

+ 4 - 29
Network/HttpRequest.cpp

@@ -9,13 +9,13 @@ using namespace HTTP;
 
 
 PostRequest::PostRequest( const char *path, const char *host, const char *data, const char *contentType, unsigned short port )
+    : ReferenceCounter()
 {
     this->path = path;
     this->host = host;
     this->data = data;
     this->contentType = contentType;
     this->port = port;
-    ref = 1;
 }
 
 Answer *PostRequest::execute() const
@@ -38,7 +38,8 @@ Answer *PostRequest::execute() const
     int length = -1;
     bool lastn = 0;
     Text answer;
-    do {
+    do
+    {
         char buff[ 2 ];
         buff[ 1 ] = 0;
         if( httpK.getNachricht( buff, 1 ) )
@@ -82,21 +83,9 @@ Answer *PostRequest::execute() const
     return 0;
 }
 
-PostRequest *PostRequest::getThis()
-{
-    ref++;
-    return this;
-}
-
-PostRequest *PostRequest::release()
-{
-    if( !--ref )
-        delete this;
-    return 0;
-}
-
 
 Answer::Answer( const char *answer )
+    : ReferenceCounter()
 {
     all = answer;
     TextReader reader( new Text( answer ) );
@@ -140,7 +129,6 @@ Answer::Answer( const char *answer )
     // parse body
     if( !reader.istEnde() )
         this->data = answer + reader.getLPosition();
-    ref = 1;
 }
 
 const char *Answer::getContentType() const
@@ -171,17 +159,4 @@ const char *Answer::getDate() const
 const char *Answer::getAll() const
 {
     return all;
-}
-
-Answer *Answer::getThis()
-{
-    ref++;
-    return this;
-}
-
-Answer *Answer::release()
-{
-    if( !--ref )
-        delete this;
-    return 0;
 }

+ 2 - 10
Network/HttpRequest.h

@@ -6,7 +6,7 @@ namespace Network
 {
     namespace HTTP
     {
-        class Answer
+        class Answer : public virtual Framework::ReferenceCounter
         {
         private:
             Framework::Text protocol;
@@ -17,7 +17,6 @@ namespace Network
             Framework::Text header;
             Framework::Text data;
             Framework::Text all;
-            int ref;
 
         public:
             __declspec( dllexport ) Answer( const char *answer );
@@ -28,12 +27,9 @@ namespace Network
             __declspec( dllexport ) const char *getStatusText() const;
             __declspec( dllexport ) const char *getDate() const;
             __declspec( dllexport ) const char *getAll() const;
-
-            __declspec( dllexport ) Answer *getThis();
-            __declspec( dllexport ) Answer *release();
         };
 
-        class PostRequest
+        class PostRequest : public virtual Framework::ReferenceCounter
         {
         private:
             Framework::Text path;
@@ -41,15 +37,11 @@ namespace Network
             Framework::Text contentType;
             Framework::Text data;
             unsigned short port;
-            int ref;
 
         public:
             __declspec( dllexport ) PostRequest( const char *path, const char *host, const char *data, const char *contentType, unsigned short port );
 
             __declspec( dllexport ) Answer *execute() const;
-
-            __declspec( dllexport ) PostRequest *getThis();
-            __declspec( dllexport ) PostRequest *release();
         };
     }
 }

+ 85 - 114
Network/Klient.cpp

@@ -13,79 +13,79 @@ using namespace Network;
 // inhalt der Klient Klasse aus Klient.h
 // Konstruktor 
 Klient::Klient()
+    : ReferenceCounter()
 {
-	memset( &server, 0, sizeof( server ) );
-	server.sin_family = AF_INET;
-	ref = 1;
-	downStreamBytes = 0;
-	upStreamBytes = 0;
-	sock = 0;
-	sendeKey = 0;
-	empfangKey = 0;
+    memset( &server, 0, sizeof( server ) );
+    server.sin_family = AF_INET;
+    downStreamBytes = 0;
+    upStreamBytes = 0;
+    sock = 0;
+    sendeKey = 0;
+    empfangKey = 0;
 }
 
 // Destruktoe 
 Klient::~Klient()
 {
-	if( sock )
-		trenne();
-	if( sendeKey )
-		sendeKey->release();
-	if( empfangKey )
-		empfangKey->release();
+    if( sock )
+        trenne();
+    if( sendeKey )
+        sendeKey->release();
+    if( empfangKey )
+        empfangKey->release();
 }
 
 // nicht constant 
 void Klient::setSendeKeyZ( Encryption::Key *key ) // Setzt den Key fürs Senden
 {
-	if( sendeKey )
-		sendeKey->release();
-	sendeKey = key;
+    if( sendeKey )
+        sendeKey->release();
+    sendeKey = key;
 }
 
 void Klient::setEmpfangKeyZ( Encryption::Key *key ) // Setzt den Key fürs Empfangen
 {
-	if( empfangKey )
-		empfangKey->release();
-	empfangKey = key;
+    if( empfangKey )
+        empfangKey->release();
+    empfangKey = key;
 }
 
 void Klient::setSendeKey( char *key, int len ) // Setzt den Key fürs Senden
 {
-	if( !sendeKey )
-		sendeKey = new Encryption::Key();
-	sendeKey->setKey( key, len );
+    if( !sendeKey )
+        sendeKey = new Encryption::Key();
+    sendeKey->setKey( key, len );
 }
 
 void Klient::setEmpfangKey( char *key, int len ) // Setzt den Key fürs Empfangen
 {
-	if( !empfangKey )
-		empfangKey = new Encryption::Key();
-	empfangKey->setKey( key, len );
+    if( !empfangKey )
+        empfangKey = new Encryption::Key();
+    empfangKey->setKey( key, len );
 }
 
 bool Klient::verbinde( unsigned short port, const char *ip ) // verbindet mit Server
 {
-	if( sendeKey )
-		sendeKey->setPos( 0 );
-	if( empfangKey )
-		empfangKey->setPos( 0 );
-	if( sock )
-		closesocket( sock );
-	sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
-	long sIp = inet_addr( ip ); // ip addresse
+    if( sendeKey )
+        sendeKey->setPos( 0 );
+    if( empfangKey )
+        empfangKey->setPos( 0 );
+    if( sock )
+        closesocket( sock );
+    sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
+    long sIp = inet_addr( ip ); // ip addresse
     if( sIp == INADDR_NONE )
     {
-        struct hostent* pHostInfo = gethostbyname( ip );
+        struct hostent *pHostInfo = gethostbyname( ip );
         if( pHostInfo == 0 )
             return 0;
-        sIp = *(long*)pHostInfo->h_addr_list[ 0 ];
+        sIp = *(long *)pHostInfo->h_addr_list[ 0 ];
     }
-	memcpy( (char*)&server.sin_addr, &sIp, sizeof( sIp ) );
-	server.sin_port = htons( port ); // port
-	if( connect( sock, ( struct sockaddr* )&server, sizeof( server ) ) < 0 ) // verbinden
-		return 0; // Fehler
-	return 1;
+    memcpy( (char *)&server.sin_addr, &sIp, sizeof( sIp ) );
+    server.sin_port = htons( port ); // port
+    if( connect( sock, (struct sockaddr *)&server, sizeof( server ) ) < 0 ) // verbinden
+        return 0; // Fehler
+    return 1;
 }
 
 bool Klient::sende( const char *nachricht, int len ) // sendet zum Server
@@ -103,8 +103,8 @@ bool Klient::sende( const char *nachricht, int len ) // sendet zum Server
         len -= l;
         ll += l;
     }
-	upStreamBytes += ll;
-	return 1;
+    upStreamBytes += ll;
+    return 1;
 }
 
 bool Klient::getNachricht( char *nachricht, int len ) // empfängt Nachricht
@@ -118,16 +118,16 @@ bool Klient::getNachricht( char *nachricht, int len ) // empf
         len -= l;
         ll += l;
     }
-	downStreamBytes += ll;
-	return 1;
+    downStreamBytes += ll;
+    return 1;
 }
 
 bool Klient::sendeEncrypted( const char *nachricht, int len ) // sendet zum Server
 {
-	if( !sendeKey )
-		return sende( nachricht, len );
-	Encryption::Bytes *n = new Encryption::Bytes( nachricht, len );
-	sendeKey->codieren( n->getThis() );
+    if( !sendeKey )
+        return sende( nachricht, len );
+    Encryption::Bytes *n = new Encryption::Bytes( nachricht, len );
+    sendeKey->codieren( (Framework::Encryption::Bytes *)n->getThis() );
     int ll = 0;
     while( len > 0 )
     {
@@ -144,15 +144,15 @@ bool Klient::sendeEncrypted( const char *nachricht, int len ) // sendet zum Serv
         len -= l;
         ll += l;
     }
-	upStreamBytes += ll;
-	n->release();
-	return 1;
+    upStreamBytes += ll;
+    n->release();
+    return 1;
 }
 
 bool Klient::getNachrichtEncrypted( char *nachricht, int len ) // empfängt Nachricht
 {
-	if( !empfangKey )
-		return getNachricht( nachricht, len );
+    if( !empfangKey )
+        return getNachricht( nachricht, len );
     int ll = 0;
     while( len > 0 )
     {
@@ -162,84 +162,70 @@ bool Klient::getNachrichtEncrypted( char *nachricht, int len ) // empf
         len -= l;
         ll += l;
     }
-	Encryption::Bytes *n = new Encryption::Bytes();
-	n->setBytesZ( nachricht, ll );
-	empfangKey->decodieren( n );
-	downStreamBytes += ll;
-	return 1;
+    Encryption::Bytes *n = new Encryption::Bytes();
+    n->setBytesZ( nachricht, ll );
+    empfangKey->decodieren( n );
+    downStreamBytes += ll;
+    return 1;
 }
 
 int Klient::getDownloadBytes( bool reset ) // gibt die anzahl von empfangen bytes zurück
 {
-	int ret = downStreamBytes;
-	if( reset )
-		downStreamBytes = 0;
-	return ret;
+    int ret = downStreamBytes;
+    if( reset )
+        downStreamBytes = 0;
+    return ret;
 }
 
 int Klient::getUploadBytes( bool reset ) // gibt die anzahl von versendeter bytes zurück
 {
-	int ret = upStreamBytes;
-	if( reset )
-		upStreamBytes = 0;
-	return ret;
+    int ret = upStreamBytes;
+    if( reset )
+        upStreamBytes = 0;
+    return ret;
 }
 
 bool Klient::trenne() // Trennt die Verbindung zum Server
 {
-	if( !sock )
-		return 1;
-	if( sendeKey )
-		sendeKey->setPos( 0 );
-	if( empfangKey )
-		empfangKey->setPos( 0 );
-	if( closesocket( sock ) < 0 ) // verbindung Trennen
-		return 0; // Fehler
-	sock = 0;
-	return 1;
+    if( !sock )
+        return 1;
+    if( sendeKey )
+        sendeKey->setPos( 0 );
+    if( empfangKey )
+        empfangKey->setPos( 0 );
+    if( closesocket( sock ) < 0 ) // verbindung Trennen
+        return 0; // Fehler
+    sock = 0;
+    return 1;
 }
 
 // constant
 bool Klient::hatNachricht( int zeit ) // Wartet eine Zeit Lang auf eine Nachricht
 {
 #ifdef WIN32
-	fd_set set = { 1, { sock } };
-	timeval time = { zeit / 1000, zeit };
-	return select( 0, &set, 0, 0, &time ) == 1;
+    fd_set set = { 1, { sock } };
+    timeval time = { zeit / 1000, zeit };
+    return select( 0, &set, 0, 0, &time ) == 1;
 #else
-	return 1;
+    return 1;
 #endif
 }
 
 unsigned short Klient::getServerPort() const // gibt den Port zurück
 {
-	return htons( server.sin_port );
+    return htons( server.sin_port );
 }
 
 const char *Klient::getServerIp() const // gibt die Ip zurück
 {
-	return inet_ntoa( server.sin_addr );
-}
-
-// Reference Counting 
-Klient *Klient::getThis()
-{
-	ref++;
-	return this;
-}
-
-Klient *Klient::release()
-{
-	ref--;
-	if( !ref )
-		delete this;
-	return 0;
+    return inet_ntoa( server.sin_addr );
 }
 
 
 // Inhalt der SSLKlient Klasse aus Klient.h
         // Konstruktor 
 SSLKlient::SSLKlient()
+    : ReferenceCounter()
 {
     ctx = SSL_CTX_new( SSLv23_client_method() );
     ip = 0;
@@ -249,7 +235,6 @@ SSLKlient::SSLKlient()
     downStreamBytes = 0;
     upStreamBytes = 0;
     connected = 0;
-    ref = 1;
 }
 
 // Destruktor 
@@ -346,18 +331,4 @@ unsigned short SSLKlient::getServerPort() const // gibt den Port des Servers zur
 const char *SSLKlient::getServerIp() const // gibt die Ip des Servers zurück
 {
     return ip->getText();
-}
-
-// Reference Counting 
-SSLKlient *SSLKlient::getThis()
-{
-    ref++;
-    return this;
-}
-
-SSLKlient *SSLKlient::release()
-{
-    if( !--ref )
-        delete this;
-    return 0;
-}
+}

+ 44 - 51
Network/Klient.h

@@ -2,6 +2,7 @@
 #define Klient_H
 
 #include "Network.h"
+#include <ReferenceCounter.h>
 
 #ifndef HEADER_OPENSSL_TYPES_H
 struct SSL_CTX;
@@ -11,10 +12,10 @@ struct BIO;
 
 namespace Framework
 {
-	namespace Encryption
-	{
-		class Key;
-	}
+    namespace Encryption
+    {
+        class Key;
+    }
 
     class Text;
 }
@@ -23,58 +24,53 @@ using namespace Framework;
 
 namespace Network
 {
-	class Klient; // aus dieser Datei
+    class Klient; // aus dieser Datei
 
-	class Klient : public EncryptedVerbindung
-	{
-	private:
-		SOCKET sock;
-		sockaddr_in server;
-		Encryption::Key *sendeKey;
-		Encryption::Key *empfangKey;
-		int downStreamBytes;
-		int upStreamBytes;
-		int ref;
+    class Klient : public EncryptedVerbindung, public virtual ReferenceCounter
+    {
+    private:
+        SOCKET sock;
+        sockaddr_in server;
+        Encryption::Key *sendeKey;
+        Encryption::Key *empfangKey;
+        int downStreamBytes;
+        int upStreamBytes;
 
-	public:
-		// Konstruktor 
-		__declspec( dllexport ) Klient();
-		// Destruktor 
-		__declspec( dllexport ) ~Klient();
-		// nicht constant 
-		__declspec( dllexport ) void setSendeKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Senden
-		__declspec( dllexport ) void setEmpfangKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Empfangen
-		__declspec( dllexport ) void setSendeKey( char *key, int len ); // Setzt den Schlüssel fürs Senden
-		__declspec( dllexport ) void setEmpfangKey( char *key, int len ); // Setzt den Schlüssel fürs Empfangen
-		__declspec( dllexport ) bool verbinde( unsigned short port, const char *ip ); // verbindet mit Server
-		__declspec( dllexport ) bool sende( const char *nachricht, int len ) override; // sendet zum Server
-		__declspec( dllexport ) bool getNachricht( char *nachricht, int len ) override; // empfängt Nachricht
-		__declspec( dllexport ) bool sendeEncrypted( const char *nachricht, int len ) override; // sendet zum Server
-		__declspec( dllexport ) bool getNachrichtEncrypted( char *nachricht, int len ) override; // empfängt Nachricht
-		__declspec( dllexport ) int getDownloadBytes( bool reset ); // gibt die anzahl von empfangen bytes zurück
-		__declspec( dllexport ) int getUploadBytes( bool reset ); // gibt die anzahl von versendeter bytes zurück
-		__declspec( dllexport ) bool trenne(); // Trennt die Verbindung zum Server
-		// constant 
-		__declspec( dllexport ) bool hatNachricht( int zeit ); // Wartet eine Zeit Lang auf eine Nachricht
-		__declspec( dllexport ) unsigned short getServerPort() const; // gibt den Port des Servers zurück
-		__declspec( dllexport ) const char *getServerIp() const; // gibt die Ip des Servers zurück
-		// Reference Counting 
-		__declspec( dllexport ) Klient *getThis();
-		__declspec( dllexport ) Klient *release();
-	};
+    public:
+        // Konstruktor 
+        __declspec( dllexport ) Klient();
+        // Destruktor 
+        __declspec( dllexport ) ~Klient();
+        // nicht constant 
+        __declspec( dllexport ) void setSendeKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Senden
+        __declspec( dllexport ) void setEmpfangKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Empfangen
+        __declspec( dllexport ) void setSendeKey( char *key, int len ); // Setzt den Schlüssel fürs Senden
+        __declspec( dllexport ) void setEmpfangKey( char *key, int len ); // Setzt den Schlüssel fürs Empfangen
+        __declspec( dllexport ) bool verbinde( unsigned short port, const char *ip ); // verbindet mit Server
+        __declspec( dllexport ) bool sende( const char *nachricht, int len ) override; // sendet zum Server
+        __declspec( dllexport ) bool getNachricht( char *nachricht, int len ) override; // empfängt Nachricht
+        __declspec( dllexport ) bool sendeEncrypted( const char *nachricht, int len ) override; // sendet zum Server
+        __declspec( dllexport ) bool getNachrichtEncrypted( char *nachricht, int len ) override; // empfängt Nachricht
+        __declspec( dllexport ) int getDownloadBytes( bool reset ); // gibt die anzahl von empfangen bytes zurück
+        __declspec( dllexport ) int getUploadBytes( bool reset ); // gibt die anzahl von versendeter bytes zurück
+        __declspec( dllexport ) bool trenne(); // Trennt die Verbindung zum Server
+        // constant 
+        __declspec( dllexport ) bool hatNachricht( int zeit ); // Wartet eine Zeit Lang auf eine Nachricht
+        __declspec( dllexport ) unsigned short getServerPort() const; // gibt den Port des Servers zurück
+        __declspec( dllexport ) const char *getServerIp() const; // gibt die Ip des Servers zurück
+    };
 
-    class SSLKlient : public Verbindung
+    class SSLKlient : public Verbindung, public virtual ReferenceCounter
     {
     private:
         unsigned short port;
-        Text* ip;
-        SSL_CTX* ctx;
-        SSL* ssl;
-        BIO* bio;
+        Text *ip;
+        SSL_CTX *ctx;
+        SSL *ssl;
+        BIO *bio;
         int downStreamBytes;
         int upStreamBytes;
         bool connected;
-        int ref;
 
     public:
         // Konstruktor 
@@ -87,12 +83,9 @@ namespace Network
         __declspec( dllexport ) int getDownloadBytes( bool reset ); // gibt die anzahl von empfangen bytes zurück
         __declspec( dllexport ) int getUploadBytes( bool reset ); // gibt die anzahl von versendeter bytes zurück
         __declspec( dllexport ) bool trenne(); // Trennt die Verbindung zum Server
-		// constant 
+        // constant 
         __declspec( dllexport ) unsigned short getServerPort() const; // gibt den Port des Servers zurück
         __declspec( dllexport ) const char *getServerIp() const; // gibt die Ip des Servers zurück
-		// Reference Counting 
-        __declspec( dllexport ) SSLKlient *getThis();
-        __declspec( dllexport ) SSLKlient *release();
     };
 }
 

+ 13 - 71
Network/Server.cpp

@@ -11,12 +11,12 @@ using namespace Network;
 // Inhalt der Server Klasse aus Server.h
 // Konstruktor 
 Server::Server()
+    : ReferenceCounter()
 {
     sock = 0;
     memset( &addresse, 0, sizeof( addresse ) ); // Adresse setzen
     addresse.sin_family = AF_INET;
     addresse.sin_addr.s_addr = ADDR_ANY;
-    ref = 1;
     klients = 0;
 }
 
@@ -31,7 +31,7 @@ bool Server::verbinde( unsigned short port, int warteschlangenLen ) // 
 {
     sock = socket( AF_INET, SOCK_STREAM, 0 ); // Socket erstellen
     addresse.sin_port = htons( port ); // port setzen
-    if( bind( sock, ( struct sockaddr* )&addresse, sizeof( addresse ) ) == -1 ) // socket öffnen
+    if( bind( sock, (struct sockaddr *)&addresse, sizeof( addresse ) ) == -1 ) // socket öffnen
     {
         trenne();
         return 0; // Fehler
@@ -51,14 +51,14 @@ SKlient *Server::getKlient() // nimmt Klient an
     sockaddr_in client;
     int len = sizeof( addresse );
 #ifdef WIN32
-    SOCKET cls = accept( sock, (sockaddr*)&client, &len ); // Klient empfangen
+    SOCKET cls = accept( sock, (sockaddr *)&client, &len ); // Klient empfangen
     if( cls == INVALID_SOCKET )
     {
         trenne();
         return 0;
     }
 #else
-    SOCKET cls = accept( sock, (sockaddr*)&client, (socklen_t*)&len ); // Klient empfangen
+    SOCKET cls = accept( sock, (sockaddr *)&client, (socklen_t *)&len ); // Klient empfangen
     if( !cls )
     {
         if( errno == ECONNABORTED || errno == EBADF )
@@ -100,28 +100,13 @@ bool Server::isConnected() const // giebt 1 zur
     return sock != 0;
 }
 
-// Reference Counting 
-Server *Server::getThis()
-{
-    ref++;
-    return this;
-}
-
-Server *Server::release()
-{
-    ref--;
-    if( !ref )
-        delete this;
-    return 0;
-}
-
 // Inhalt der SKlient Klasse aus Server.h
 // Konstruktor 
 SKlient::SKlient( sockaddr_in addresse, SOCKET sock )
+    : ReferenceCounter()
 {
     clientAddr = addresse;
     this->sock = sock;
-    ref = 1;
     downStreamBytes = 0;
     upStreamBytes = 0;
     sendeKey = 0;
@@ -210,7 +195,7 @@ bool SKlient::sendeEncrypted( const char *nachricht, int len ) // sendet zum Ser
     if( !sendeKey )
         return sende( nachricht, len );
     Encryption::Bytes *n = new Encryption::Bytes( nachricht, len );
-    sendeKey->codieren( n->getThis() ); int ll = 0;
+    sendeKey->codieren( (Encryption::Bytes *)n->getThis() ); int ll = 0;
     while( len > 0 )
     {
 #ifdef WIN32
@@ -288,24 +273,9 @@ const char *SKlient::getIp() const // gibt die Ip des Klients zur
     return inet_ntoa( clientAddr.sin_addr );
 }
 
-// Reference Counting 
-SKlient *SKlient::getThis()
-{
-    ref++;
-    return this;
-}
-
-SKlient *SKlient::release()
-{
-    ref--;
-    if( !ref )
-        delete this;
-    return 0;
-}
-
 int pem_passwd_cb( char *buf, int size, int rwflag, void *userdata )
 {
-    const char *passw = ( (Text*)userdata )->getText();
+    const char *passw = ( (Text *)userdata )->getText();
     memcpy( buf, passw, MIN( (unsigned int)size, strlen( passw ) + 1 ) );
     return (int)strlen( buf );
 }
@@ -313,6 +283,7 @@ int pem_passwd_cb( char *buf, int size, int rwflag, void *userdata )
 // Inhalt der SSLServer Klasse
 // Konstruktor 
 SSLServer::SSLServer()
+    : ReferenceCounter()
 {
     s = 0;
     ctx = SSL_CTX_new( SSLv23_server_method() );
@@ -322,7 +293,6 @@ SSLServer::SSLServer()
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl( INADDR_ANY );
     klients = 0;
-    ref = 1;
 }
 
 // Destruktor 
@@ -365,7 +335,7 @@ bool SSLServer::verbinde( unsigned short port, int warteschlangenLen )
         s = 0;
         return 0;
     }
-    if( bind( s, ( struct sockaddr* )&addr, sizeof( addr ) ) < 0 )
+    if( bind( s, (struct sockaddr *)&addr, sizeof( addr ) ) < 0 )
     {
         trenne();
         return 0;
@@ -386,14 +356,14 @@ SSLSKlient *SSLServer::getKlient()
     int len = sizeof( addr );
     struct sockaddr_in addr;
 #ifdef WIN32
-    SOCKET client = accept( s, ( struct sockaddr* )&addr, &len );
+    SOCKET client = accept( s, (struct sockaddr *)&addr, &len );
     if( client == INVALID_SOCKET )
     {
         trenne();
         return 0;
     }
 #else
-    SOCKET client = accept( s, (sockaddr*)&addr, (socklen_t*)&len ); // Klient empfangen
+    SOCKET client = accept( s, (sockaddr *)&addr, (socklen_t *)&len ); // Klient empfangen
     if( !client )
     {
         if( errno == ECONNABORTED || errno == EBADF )
@@ -447,31 +417,17 @@ bool SSLServer::isConnected() const
     return s != 0;
 }
 
-// Reference Counting 
-SSLServer *SSLServer::getThis()
-{
-    ref++;
-    return this;
-}
-
-SSLServer *SSLServer::release()
-{
-    if( !--ref )
-        delete this;
-    return 0;
-}
-
 
 // Inhalt der SSLSKlient Klasse
 // Konstruktor 
 SSLSKlient::SSLSKlient( sockaddr_in client, SSL *ssl, SOCKET s )
+    : ReferenceCounter()
 {
     this->s = s;
     clientAddr = client;
     this->ssl = ssl;
     downStreamBytes = 0;
     upStreamBytes = 0;
-    ref = 1;
 }
 
 // Destruktor 
@@ -555,18 +511,4 @@ unsigned short SSLSKlient::getPort() const // gibt den Port zur
 const char *SSLSKlient::getIp() const // gibt die Ip des Klients zurück
 {
     return inet_ntoa( clientAddr.sin_addr );
-}
-
-// Reference Counting 
-SSLSKlient *SSLSKlient::getThis()
-{
-    ref++;
-    return this;
-}
-
-SSLSKlient *SSLSKlient::release()
-{
-    if( !--ref )
-        delete this;
-    return 0;
-}
+}

+ 62 - 77
Network/Server.h

@@ -2,6 +2,7 @@
 #define Server_H
 
 #include "Network.h"
+#include <ReferenceCounter.h>
 
 #ifndef HEADER_OPENSSL_TYPES_H
 struct SSL_CTX;
@@ -10,10 +11,10 @@ struct SSL;
 
 namespace Framework
 {
-	namespace Encryption
-	{
-		class Key;
-	}
+    namespace Encryption
+    {
+        class Key;
+    }
     class Text;
 }
 
@@ -21,74 +22,66 @@ using namespace Framework;
 
 namespace Network
 {
-	class Server; // aus dieser Datei
-	class SKlient; // aus dieser Datei
+    class Server; // aus dieser Datei
+    class SKlient; // aus dieser Datei
 
-	class Server
-	{
-	private:
-		SOCKET sock;
-		SOCKADDR_IN addresse;
-		int klients;
-		int ref;
+    class Server : public virtual ReferenceCounter
+    {
+    private:
+        SOCKET sock;
+        SOCKADDR_IN addresse;
+        int klients;
 
-	public:
-		// Konstruktor 
-		__declspec( dllexport ) Server();
-		// Destruktor 
-		__declspec( dllexport ) ~Server();
-		// nicht constant 
-		__declspec( dllexport ) bool verbinde( unsigned short port, int warteschlangenLen ); // Öffnet das Socket
-		__declspec( dllexport ) SKlient *getKlient(); // nimmt Klient an
-		__declspec( dllexport ) int getKlients( bool reset ); // gibt die Anzahl der Klients zurück
-		__declspec( dllexport ) bool trenne(); // beendet den Server
-		// constant
-		__declspec( dllexport ) unsigned short getPort() const; // gibt den Port zurück
+    public:
+        // Konstruktor 
+        __declspec( dllexport ) Server();
+        // Destruktor 
+        __declspec( dllexport ) ~Server();
+        // nicht constant 
+        __declspec( dllexport ) bool verbinde( unsigned short port, int warteschlangenLen ); // Öffnet das Socket
+        __declspec( dllexport ) SKlient *getKlient(); // nimmt Klient an
+        __declspec( dllexport ) int getKlients( bool reset ); // gibt die Anzahl der Klients zurück
+        __declspec( dllexport ) bool trenne(); // beendet den Server
+        // constant
+        __declspec( dllexport ) unsigned short getPort() const; // gibt den Port zurück
         __declspec( dllexport ) bool isConnected() const; // giebt 1 zurück, falls der Server verbunden ist
-		// Reference Counting 
-		__declspec( dllexport ) Server *getThis();
-		__declspec( dllexport ) Server *release();
-	};
+    };
 
-	class SKlient : public EncryptedVerbindung
-	{
-	private:
-		SOCKET sock;
-		sockaddr_in clientAddr;
-		Encryption::Key *sendeKey;
-		Encryption::Key *empfangKey;
-		int downStreamBytes;
-		int upStreamBytes;
-		int ref;
+    class SKlient : public EncryptedVerbindung, public virtual ReferenceCounter
+    {
+    private:
+        SOCKET sock;
+        sockaddr_in clientAddr;
+        Encryption::Key *sendeKey;
+        Encryption::Key *empfangKey;
+        int downStreamBytes;
+        int upStreamBytes;
 
-	public:
-		// Konstruktor 
-		__declspec( dllexport ) SKlient( sockaddr_in client, SOCKET sock );
-		// Destruktor 
-		__declspec( dllexport ) ~SKlient();
-		// nicht constant 
-		__declspec( dllexport ) void setSendeKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Senden
-		__declspec( dllexport ) void setEmpfangKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Empfangen
-		__declspec( dllexport ) void setSendeKey( char *key, int len ); // Setzt den Schlüssel fürs Senden
-		__declspec( dllexport ) void setEmpfangKey( char *key, int len ); // Setzt den Schlüssel fürs Empfangen
-		__declspec( dllexport ) bool sende( const char *nachricht, int len ) override; // sendet zum Klient
-		__declspec( dllexport ) bool getNachricht( char *nachricht, int len ) override; // empfängt Nachricht von Klient
-		__declspec( dllexport ) bool sendeEncrypted( const char *nachricht, int len ) override; // sendet zum Server
-		__declspec( dllexport ) bool getNachrichtEncrypted( char *nachricht, int len ) override; // empfängt Nachricht
-		__declspec( dllexport ) int getDownloadBytes( bool reset ); // gibt die anzahl von empfangen bytes zurück
-		__declspec( dllexport ) int getUploadBytes( bool reset ); // gibt die anzahl von versendeter bytes zurück
-		__declspec( dllexport ) bool trenne(); // trennt die Verbindung zum Klient
-		// constant 
-		__declspec( dllexport ) unsigned short getPort() const; // gibt den Port zurück
-		__declspec( dllexport ) const char *getIp() const; // gibt die Ip des Klients zurück
-		// Reference Counting 
-		__declspec( dllexport ) SKlient *getThis();
-		__declspec( dllexport ) SKlient *release();
-	};
+    public:
+        // Konstruktor 
+        __declspec( dllexport ) SKlient( sockaddr_in client, SOCKET sock );
+        // Destruktor 
+        __declspec( dllexport ) ~SKlient();
+        // nicht constant 
+        __declspec( dllexport ) void setSendeKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Senden
+        __declspec( dllexport ) void setEmpfangKeyZ( Encryption::Key *key ); // Setzt den Schlüssel fürs Empfangen
+        __declspec( dllexport ) void setSendeKey( char *key, int len ); // Setzt den Schlüssel fürs Senden
+        __declspec( dllexport ) void setEmpfangKey( char *key, int len ); // Setzt den Schlüssel fürs Empfangen
+        __declspec( dllexport ) bool sende( const char *nachricht, int len ) override; // sendet zum Klient
+        __declspec( dllexport ) bool getNachricht( char *nachricht, int len ) override; // empfängt Nachricht von Klient
+        __declspec( dllexport ) bool sendeEncrypted( const char *nachricht, int len ) override; // sendet zum Server
+        __declspec( dllexport ) bool getNachrichtEncrypted( char *nachricht, int len ) override; // empfängt Nachricht
+        __declspec( dllexport ) int getDownloadBytes( bool reset ); // gibt die anzahl von empfangen bytes zurück
+        __declspec( dllexport ) int getUploadBytes( bool reset ); // gibt die anzahl von versendeter bytes zurück
+        __declspec( dllexport ) bool trenne(); // trennt die Verbindung zum Klient
+        // constant 
+        __declspec( dllexport ) unsigned short getPort() const; // gibt den Port zurück
+        __declspec( dllexport ) const char *getIp() const; // gibt die Ip des Klients zurück
+    };
 
     class SSLSKlient;
 
-    class SSLServer
+    class SSLServer : public virtual ReferenceCounter
     {
     private:
         SOCKET s;
@@ -96,7 +89,6 @@ namespace Network
         SSL_CTX *ctx;
         Text *passw;
         int klients;
-        int ref;
 
     public:
         // Konstruktor 
@@ -113,9 +105,9 @@ namespace Network
         // Öffnet das Socket
         __declspec( dllexport ) bool verbinde( unsigned short port, int warteschlangenLen );
         // nimmt Klient an
-        __declspec( dllexport ) SSLSKlient *getKlient(); 
+        __declspec( dllexport ) SSLSKlient *getKlient();
         // gibt die Anzahl der Klients zurück
-        __declspec( dllexport ) int getKlients( bool reset ); 
+        __declspec( dllexport ) int getKlients( bool reset );
         // beendet den Server
         __declspec( dllexport ) bool trenne();
         // constant
@@ -123,20 +115,16 @@ namespace Network
         __declspec( dllexport ) unsigned short getPort() const;
         // giebt 1 zurück, falls der Server verbunden ist
         __declspec( dllexport ) bool isConnected() const;
-        // Reference Counting 
-        __declspec( dllexport ) SSLServer *getThis();
-        __declspec( dllexport ) SSLServer *release();
     };
 
-    class SSLSKlient : public Verbindung
+    class SSLSKlient : public Verbindung, public virtual ReferenceCounter
     {
     private:
         SOCKET s;
-        SSL* ssl;
+        SSL *ssl;
         sockaddr_in clientAddr;
         int downStreamBytes;
         int upStreamBytes;
-        int ref;
 
     public:
         // Konstruktor 
@@ -149,12 +137,9 @@ namespace Network
         __declspec( dllexport ) int getDownloadBytes( bool reset ); // gibt die anzahl von empfangen bytes zurück
         __declspec( dllexport ) int getUploadBytes( bool reset ); // gibt die anzahl von versendeter bytes zurück
         __declspec( dllexport ) bool trenne(); // trennt die Verbindung zum Klient
-		// constant 
+        // constant 
         __declspec( dllexport ) unsigned short getPort() const; // gibt den Port zurück
         __declspec( dllexport ) const char *getIp() const; // gibt die Ip des Klients zurück
-        // Reference Counting 
-        __declspec( dllexport ) SSLSKlient *getThis();
-        __declspec( dllexport ) SSLSKlient *release();
     };
 }
 

+ 44 - 54
Network/WebSocket.cpp

@@ -16,7 +16,7 @@ __declspec( dllexport ) Frame &Frame::operator+=( const Frame &b ) // baut frame
     {
         char *data = new char[ (int)dataLength ];
         if( data )
-            memcpy( data, this->data, (int)(dataLength - b.dataLength) );
+            memcpy( data, this->data, (int)( dataLength - b.dataLength ) );
         if( b.data )
             memcpy( data + dataLength, b.data, (int)b.dataLength );
         delete[] this->data;
@@ -90,7 +90,7 @@ __declspec( dllexport ) bool WebSocketClient::connect()
         klient = new Klient();
         if( !klient->verbinde( port, host ) )
         {
-            klient = klient->release();
+            klient = (Klient *)klient->release();
             return 0;
         }
         klient->sende( message, message.getLength() );
@@ -113,7 +113,7 @@ __declspec( dllexport ) bool WebSocketClient::connect()
         if( handshakeResponse->getStatusCode() != 101 )
         {
             handshakeResponse->release();
-            klient = klient->release();
+            klient = (Klient *)klient->release();
             return 0;
         }
         handshakeResponse->release();
@@ -139,10 +139,10 @@ __declspec( dllexport ) bool WebSocketClient::send( __int64 size, const char *da
     f.dataLength = size;
     f.data = new char[ (int)f.dataLength ];
     memcpy( f.data, data, (int)f.dataLength );
-    f.key[ 0 ] = (unsigned char)(gen.rand() * 256);
-    f.key[ 1 ] = (unsigned char)(gen.rand() * 256);
-    f.key[ 2 ] = (unsigned char)(gen.rand() * 256);
-    f.key[ 3 ] = (unsigned char)(gen.rand() * 256);
+    f.key[ 0 ] = (unsigned char)( gen.rand() * 256 );
+    f.key[ 1 ] = (unsigned char)( gen.rand() * 256 );
+    f.key[ 2 ] = (unsigned char)( gen.rand() * 256 );
+    f.key[ 3 ] = (unsigned char)( gen.rand() * 256 );
     c.lock();
     queue->add( f );
     c.unlock();
@@ -175,50 +175,50 @@ __declspec( dllexport ) void WebSocketClient::thread()
                     return;
                 }
                 Frame message;
-                ok &= klient->getNachricht( (char*)&byte, 1 );
+                ok &= klient->getNachricht( (char *)&byte, 1 );
                 message.fin = ( byte & 0x80 ) != 0;
                 message.rsv1 = ( byte & 0x40 ) != 0;
                 message.rsv2 = ( byte & 0x20 ) != 0;
                 message.rsv3 = ( byte & 0x10 ) != 0;
                 message.opcode = byte & 0xF;
-                ok &= klient->getNachricht( (char*)&byte, 1 );
+                ok &= klient->getNachricht( (char *)&byte, 1 );
                 message.mask = ( byte & 0x80 ) != 0;
                 message.dataLength = byte & 0x7F;
                 if( message.dataLength == 126 )
                 {
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength = byte << 8;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= byte;
                 }
                 else if( message.dataLength == 127 )
                 {
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength = (__int64)byte << 56;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte << 48;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte << 40;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte << 32;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte << 24;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte << 16;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte << 8;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.dataLength |= (__int64)byte;
                 }
                 if( message.mask )
                 {
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.key[ 0 ] = byte;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.key[ 1 ] = byte;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.key[ 2 ] = byte;
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     message.key[ 3 ] = byte;
                 }
                 if( !ok )
@@ -228,7 +228,7 @@ __declspec( dllexport ) void WebSocketClient::thread()
                     message.data = new char[ (int)message.dataLength ];
                 for( int i = 0; i < message.dataLength; i++ )
                 {
-                    ok &= klient->getNachricht( (char*)&byte, 1 );
+                    ok &= klient->getNachricht( (char *)&byte, 1 );
                     if( message.mask )
                         message.data[ i ] = byte ^ message.key[ i % 4 ];
                     else
@@ -271,7 +271,7 @@ __declspec( dllexport ) void WebSocketClient::thread()
                     delete[] m.data;
                     c2.lock();
                     klient->trenne();
-                    klient = klient->release();
+                    klient = (Klient *)klient->release();
                     c2.unlock();
                     return;
                 }
@@ -310,7 +310,7 @@ __declspec( dllexport ) void WebSocketClient::thread()
                     c2.unlock();
                     return;
                 }
-                klient->sende( (char*)&byte, 1 );
+                klient->sende( (char *)&byte, 1 );
                 byte = ( f.mask ? 1 : 0 ) << 7;
                 if( f.dataLength < 126 )
                     byte |= (unsigned char)f.dataLength & 0x7F;
@@ -318,46 +318,46 @@ __declspec( dllexport ) void WebSocketClient::thread()
                     byte |= 126;
                 else
                     byte |= 127;
-                klient->sende( (char*)&byte, 1 );
+                klient->sende( (char *)&byte, 1 );
                 if( f.dataLength >= 126 )
                 {
                     if( f.dataLength <= 32767 )
                     {
                         byte = (unsigned char)( f.dataLength >> 8 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)f.dataLength & 0xFF;
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                     }
                     else
                     {
                         byte = (unsigned char)( f.dataLength >> 56 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)( f.dataLength >> 48 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)( f.dataLength >> 40 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)( f.dataLength >> 32 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)( f.dataLength >> 24 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)( f.dataLength >> 16 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)( f.dataLength >> 8 );
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                         byte = (unsigned char)f.dataLength & 0xFF;
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                     }
                 }
                 if( f.mask )
                 {
                     byte = (unsigned char)f.key[ 0 ];
-                    klient->sende( (char*)&byte, 1 );
+                    klient->sende( (char *)&byte, 1 );
                     byte = (unsigned char)f.key[ 1 ];
-                    klient->sende( (char*)&byte, 1 );
+                    klient->sende( (char *)&byte, 1 );
                     byte = (unsigned char)f.key[ 2 ];
-                    klient->sende( (char*)&byte, 1 );
+                    klient->sende( (char *)&byte, 1 );
                     byte = (unsigned char)f.key[ 3 ];
-                    klient->sende( (char*)&byte, 1 );
+                    klient->sende( (char *)&byte, 1 );
                 }
                 if( f.dataLength )
                 {
@@ -367,7 +367,7 @@ __declspec( dllexport ) void WebSocketClient::thread()
                             byte = (unsigned char)f.data[ i ] ^ f.key[ i % 4 ];
                         else
                             byte = (unsigned char)f.data[ i ];
-                        klient->sende( (char*)&byte, 1 );
+                        klient->sende( (char *)&byte, 1 );
                     }
                 }
                 c2.unlock();
@@ -381,7 +381,7 @@ __declspec( dllexport ) void WebSocketClient::thread()
                         c.unlock();
                         c2.lock();
                         klient->trenne();
-                        klient = klient->release();
+                        klient = (Klient *)klient->release();
                         c2.unlock();
                         return;
                     }
@@ -402,7 +402,7 @@ __declspec( dllexport ) void WebSocketClient::disconnect()
         return;
     c2.lock();
     klient->trenne();
-    klient = klient->release();
+    klient = (Klient *)klient->release();
     c2.unlock();
     warteAufThread( 1000 );
 }
@@ -410,14 +410,4 @@ __declspec( dllexport ) void WebSocketClient::disconnect()
 __declspec( dllexport ) bool WebSocketClient::isConnected() const
 {
     return klient != 0;
-}
-
-// löscht das objekt wenn es nicht mehr gebraucht wird und beendet den Thread
-Thread *WebSocketClient::release()
-{
-#ifdef WIN32
-    if( ref == 2 && run )
-        disconnect();
-#endif
-    return Thread::release();
 }

+ 0 - 2
Network/WebSocket.h

@@ -65,8 +65,6 @@ namespace Network
             __declspec( dllexport ) void disconnect();
             // Gibt 1 zurück, wenn eine Verbindung zum Server besteht, 0 sonnst
             __declspec( dllexport ) bool isConnected() const;
-            // löscht das objekt wenn es nicht mehr gebraucht wird und beendet den Thread
-            __declspec( dllexport ) Framework::Thread *release() override;
         };
     }
 }