Forráskód Böngészése

fix waitForNextMessage functions on windows and incread debug output

Kolja Strohm 1 éve
szülő
commit
16b5cfb7d4
2 módosított fájl, 161 hozzáadás és 14 törlés
  1. 73 6
      Network/Klient.cpp
  2. 88 8
      Network/Server.cpp

+ 73 - 6
Network/Klient.cpp

@@ -99,6 +99,12 @@ bool Klient::sende(const char* nachricht, int len) // sendet zum Server
 #endif
         if (l <= 0)
         {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "send: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
             errorOccured = 1;
             return 0; // Fehler
         }
@@ -117,6 +123,12 @@ bool Klient::getNachricht(char* nachricht, int len) // empf
         int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
         if (l <= 0)
         {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "recv: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
             errorOccured = 1;
             return 0; // Fehler
         }
@@ -143,6 +155,12 @@ bool Klient::sendeEncrypted(const char* nachricht, int len) // sendet zum Server
 #endif
         if (l <= 0)
         {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "send: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
             n->release();
             errorOccured = 1;
             return 0; // Fehler
@@ -165,6 +183,12 @@ bool Klient::getNachrichtEncrypted(
         int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
         if (l <= 0)
         {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "recv: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
             errorOccured = 1;
             return 0; // Fehler
         }
@@ -215,9 +239,16 @@ bool Klient::hatNachricht(int zeit) // Wartet eine Zeit Lang auf eine Nachricht
     FD_ZERO(&set);
     FD_SET(sock, &set);
     timeval time = {zeit / 1000, zeit};
-    int result = select(0, &set, 0, 0, &time) == 1;
+    int result = select(0, &set, 0, 0, &time);
     if (result < 0)
-        errorOccured = 1;
+    {
+#ifdef WIN32
+#    ifdef _DEBUG
+        std::cout << "select: " << result << " Error: " << WSAGetLastError()
+                  << std::endl;
+#    endif
+#endif
+    }
     return result > 0;
 }
 
@@ -243,12 +274,34 @@ bool Klient::waitForNextMessage() const // wartet bis es etwas zu empfangen gibt
         timeout.tv_sec = 10;
         timeout.tv_usec = 0;
         rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
-        if (rv == -1) return 0;
+        if (rv == -1)
+        {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "select: " << rv << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
+            return 0;
+        }
     }
     if (!sock) return 0;
     char c;
+#ifdef WIN32
+    int l = (int)recv(sock, &c, 1, MSG_PEEK);
+#else
     int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
-    if (l <= 0) return 0; // Fehler
+#endif
+    if (l <= 0)
+    {
+#ifdef WIN32
+#    ifdef _DEBUG
+        std::cout << "recv: " << l << " Error: " << WSAGetLastError()
+                  << std::endl;
+#    endif
+#endif
+        return 0; // Fehler
+    }
     return 1;
 }
 
@@ -308,7 +361,14 @@ bool SSLKlient::sende(const char* nachricht, int len) // sendet zum Server
     while (len > 0)
     {
         int l = SSL_write(ssl, nachricht + ll, len);
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef _DEBUG
+            std::cout << "SSL_write: " << l
+                      << " Error: " << SSL_get_error(ssl, l) << std::endl;
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }
@@ -323,7 +383,14 @@ bool SSLKlient::getNachricht(char* nachricht, int len) // empf
     while (len > 0)
     {
         int l = SSL_read(ssl, nachricht + ll, len);
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef _DEBUG
+            std::cout << "SSL_read: " << l
+                      << " Error: " << SSL_get_error(ssl, l) << std::endl;
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }

+ 88 - 8
Network/Server.cpp

@@ -202,7 +202,16 @@ bool SKlient::sende(const char* nachricht, int len) // sendet zum Klient
 #else
         int l = (int)send(sock, nachricht + ll, len, MSG_NOSIGNAL);
 #endif
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "send: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }
@@ -218,7 +227,16 @@ bool SKlient::getNachricht(
     while (len > 0)
     {
         int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "recv: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }
@@ -242,6 +260,12 @@ bool SKlient::sendeEncrypted(
 #endif
         if (l <= 0)
         {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "send: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
             n->release();
             return 0; // Fehler
         }
@@ -261,7 +285,16 @@ bool SKlient::getNachrichtEncrypted(
     while (len > 0)
     {
         int l = (int)recv(sock, nachricht + ll, len, MSG_WAITALL);
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "recv: " << l << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }
@@ -305,7 +338,17 @@ bool SKlient::hatNachricht(
     FD_ZERO(&set);
     FD_SET(sock, &set);
     timeval time = {zeit / 1000, zeit};
-    return select(0, &set, 0, 0, &time) == 1;
+    int result = select(0, &set, 0, 0, &time);
+    if (result < 0)
+    {
+#ifdef WIN32
+#    ifdef _DEBUG
+        std::cout << "select: " << result << " Error: " << WSAGetLastError()
+                  << std::endl;
+#    endif
+#endif
+    }
+    return result > 0;
 }
 
 unsigned short SKlient::getPort() const // gibt den Port zurück
@@ -365,12 +408,34 @@ bool SKlient::waitForNextMessage()
         timeout.tv_sec = 10;
         timeout.tv_usec = 0;
         rv = select((int)sock + 1, &set, NULL, NULL, &timeout);
-        if (rv == -1) return 0;
+        if (rv == -1)
+        {
+#ifdef WIN32
+#    ifdef _DEBUG
+            std::cout << "select: " << rv << " Error: " << WSAGetLastError()
+                      << std::endl;
+#    endif
+#endif
+            return 0;
+        }
     }
     if (!sock) return 0;
     char c;
+#ifdef WIN32
+    int l = (int)recv(sock, &c, 1, MSG_PEEK);
+#else
     int l = (int)recv(sock, &c, 1, MSG_WAITALL | MSG_PEEK);
-    if (l <= 0) return 0; // Fehler
+#endif
+    if (l <= 0)
+    {
+#ifdef WIN32
+#    ifdef _DEBUG
+        std::cout << "recv: " << l << " Error: " << WSAGetLastError()
+                  << std::endl;
+#    endif
+#endif
+        return 0; // Fehler
+    }
     return 1;
 }
 
@@ -593,7 +658,14 @@ bool SSLSKlient::sende(const char* nachricht, int len) // sendet zum Klient
     while (len > 0)
     {
         int l = SSL_write(ssl, nachricht + ll, len);
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef _DEBUG
+            std::cout << "SSL_write: " << l
+                      << " Error: " << SSL_get_error(ssl, l) << std::endl;
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }
@@ -609,7 +681,15 @@ bool SSLSKlient::getNachricht(
     while (len > 0)
     {
         int l = (int)SSL_read(ssl, nachricht + ll, len);
-        if (l <= 0) return 0; // Fehler
+        if (l <= 0)
+        {
+#ifdef _DEBUG
+            SSL_ERROR_WANT_ASYNC
+            std::cout << "SSL_read: " << l
+                      << " Error: " << SSL_get_error(ssl, l) << std::endl;
+#endif
+            return 0; // Fehler
+        }
         len -= l;
         ll += l;
     }