]> gitweb.hamatoma.de Git - crepublib/commitdiff
windows corrections
authorHamatoma <git.tortouse@hm.f-r-e-i.de>
Sun, 22 Mar 2015 23:16:06 +0000 (00:16 +0100)
committerHamatoma <git.tortouse@hm.f-r-e-i.de>
Sun, 22 Mar 2015 23:16:06 +0000 (00:16 +0100)
net/ReTCP.cpp
net/ReTCP.hpp

index 74adb0df661f7f0bd881c49b71021a4029fb91d5..905b1642f36f5812fb44812d77be649f55189aa4 100644 (file)
@@ -212,11 +212,14 @@ ReTCPConnection::ReTCPConnection(int id, ReLoggerOwner* loggerOwner) :
            m_noReceived(0),\r
            m_logSendReceive(true) {\r
 #if defined __WIN32__\r
-       WSADATA wsaData;\r
-       if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {\r
-               loggerOwner->logger()->sayF(LOG_ERROR | CAT_NETWORK, LC_TCP_CONNECTION_1,\r
-                       i18n("WSAStartup() failed: $1")).arg(errno).arg(getLastOSError()).end();\r
-               throw ReException("WSAStartup() failed");\r
+       if (! isGlobalInitialized){\r
+               WSADATA wsaData;\r
+               if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {\r
+                       loggerOwner->logger()->sayF(LOG_ERROR | CAT_NETWORK, LC_TCP_CONNECTION_1,\r
+                               i18n("WSAStartup() failed: $1")).arg(errno).arg(getLastOSError()).end();\r
+                       throw ReException("WSAStartup() failed");\r
+               }\r
+               isGlobalInitialized = true;\r
        }\r
 #endif\r
 }\r
@@ -241,7 +244,8 @@ void ReTCPConnection::close() {
  */\r
 void ReTCPConnection::globalClose() {\r
 #if defined __WIN32__\r
-       WSACleanup();\r
+       if (isGlobalInitialized)\r
+               WSACleanup();\r
 #endif\r
 }\r
 \r
@@ -472,10 +476,20 @@ ReTCPServer::~ReTCPServer() {
  * @param handleSocket the handle of the read/write channel\r
  * @address                            the data about the client connection (ip, port)\r
  */\r
-ReTCPServerConnection* ReTCPServer::createConnection(int id, int handleSocket,\r
-    const struct sockaddr& address) {\r
+ReTCPServerConnection* ReTCPServer::createConnection(int id, int handleSocket) {\r
        ReTCPServerConnection* rc = NULL;\r
        if (m_threadPool.countThreads() < m_maxConnections) {\r
+               union {\r
+                       struct sockaddr address;\r
+                       uint8_t dummy[32];\r
+               } u;\r
+               struct sockaddr& address = u.address;\r
+               memset(&u, -1, sizeof u);\r
+               socklen_t sizeAddress = sizeof u; //.address;\r
+               int rc2 = getpeername(handleSocket, &address, &sizeAddress);\r
+               if (rc2 != 0)\r
+                       m_logger->sayF(LOG_ERROR | CAT_NETWORK, LC_LISTEN_FOR_ALL_1,\r
+                   i18n("getpeername() failed: $1")).arg(getLastOSError()).end();\r
                rc = new ReTCPServerConnection(id, this);\r
                rc->setId(id);\r
                rc->setHandleSocket(handleSocket);\r
@@ -544,10 +558,8 @@ bool ReTCPServer::listenForAll() {
                        int nextId = 1;\r
                        //Accept and incoming connection\r
                        int clientSocket;\r
-                       struct sockaddr addrClient;\r
-                       socklen_t lengthAddr = sizeof(struct sockaddr_in);\r
                        while ((clientSocket = accept(m_handleSocket,\r
-                           (struct sockaddr *) &addrClient, &lengthAddr)) != 0) {\r
+                                       NULL, NULL)) > 0) {\r
                                if (!m_threadPool.shouldStop()) {\r
                                        if (m_threadPool.countThreads() >= m_maxConnections) {\r
                                                // close the connection at once:\r
@@ -559,7 +571,7 @@ bool ReTCPServer::listenForAll() {
                                                reCloseSocket(clientSocket);\r
                                        } else {\r
                                                ReTCPServerConnection* connection = createConnection(\r
-                                                   nextId++, clientSocket, addrClient);\r
+                                                   nextId++, clientSocket);\r
                                                connection->setLogSendReceive(m_logSendReceive);\r
                                                if (!m_threadPool.startThread(connection)) {\r
                                                        m_logger->sayF(LOG_ERROR | CAT_PROCESS,\r
@@ -574,6 +586,11 @@ bool ReTCPServer::listenForAll() {
                                        }\r
                                }\r
                        }\r
+                       if (clientSocket < 0)\r
+                               m_logger->sayF(LOG_ERROR | CAT_PROCESS,\r
+                                                           LC_LISTEN_FOR_ALL_6,\r
+                                                           i18n("accept() failed: $1")).arg(\r
+                                                           getLastOSError()).end();\r
                        if (m_threadPool.shouldStop()) {\r
                                m_logger->say(LOG_INFO | CAT_PROCESS, LC_LISTEN_FOR_ALL_7,\r
                                    i18n("stop request received"));\r
index 05066bae1eec0721adb0fcb044d4f03cf877feb3..67f20f9eaced6b7ef346d46b6c3e7766f7a2cb3b 100644 (file)
@@ -182,8 +182,7 @@ public:
        bool listenForAll();
        virtual ReLogger* logger();
 private:
-       ReTCPServerConnection* createConnection(int id, int handleSocket,
-           const struct sockaddr& address);
+       ReTCPServerConnection* createConnection(int id, int handleSocket);
 
 protected:
        int m_maxConnections;