From 6621afeb7170dfb9764f1548ba10c26022f3379d Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Mon, 23 Mar 2015 00:16:06 +0100 Subject: [PATCH] windows corrections --- net/ReTCP.cpp | 41 +++++++++++++++++++++++++++++------------ net/ReTCP.hpp | 3 +-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/net/ReTCP.cpp b/net/ReTCP.cpp index 74adb0d..905b164 100644 --- a/net/ReTCP.cpp +++ b/net/ReTCP.cpp @@ -212,11 +212,14 @@ ReTCPConnection::ReTCPConnection(int id, ReLoggerOwner* loggerOwner) : m_noReceived(0), m_logSendReceive(true) { #if defined __WIN32__ - WSADATA wsaData; - if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { - loggerOwner->logger()->sayF(LOG_ERROR | CAT_NETWORK, LC_TCP_CONNECTION_1, - i18n("WSAStartup() failed: $1")).arg(errno).arg(getLastOSError()).end(); - throw ReException("WSAStartup() failed"); + if (! isGlobalInitialized){ + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { + loggerOwner->logger()->sayF(LOG_ERROR | CAT_NETWORK, LC_TCP_CONNECTION_1, + i18n("WSAStartup() failed: $1")).arg(errno).arg(getLastOSError()).end(); + throw ReException("WSAStartup() failed"); + } + isGlobalInitialized = true; } #endif } @@ -241,7 +244,8 @@ void ReTCPConnection::close() { */ void ReTCPConnection::globalClose() { #if defined __WIN32__ - WSACleanup(); + if (isGlobalInitialized) + WSACleanup(); #endif } @@ -472,10 +476,20 @@ ReTCPServer::~ReTCPServer() { * @param handleSocket the handle of the read/write channel * @address the data about the client connection (ip, port) */ -ReTCPServerConnection* ReTCPServer::createConnection(int id, int handleSocket, - const struct sockaddr& address) { +ReTCPServerConnection* ReTCPServer::createConnection(int id, int handleSocket) { ReTCPServerConnection* rc = NULL; if (m_threadPool.countThreads() < m_maxConnections) { + union { + struct sockaddr address; + uint8_t dummy[32]; + } u; + struct sockaddr& address = u.address; + memset(&u, -1, sizeof u); + socklen_t sizeAddress = sizeof u; //.address; + int rc2 = getpeername(handleSocket, &address, &sizeAddress); + if (rc2 != 0) + m_logger->sayF(LOG_ERROR | CAT_NETWORK, LC_LISTEN_FOR_ALL_1, + i18n("getpeername() failed: $1")).arg(getLastOSError()).end(); rc = new ReTCPServerConnection(id, this); rc->setId(id); rc->setHandleSocket(handleSocket); @@ -544,10 +558,8 @@ bool ReTCPServer::listenForAll() { int nextId = 1; //Accept and incoming connection int clientSocket; - struct sockaddr addrClient; - socklen_t lengthAddr = sizeof(struct sockaddr_in); while ((clientSocket = accept(m_handleSocket, - (struct sockaddr *) &addrClient, &lengthAddr)) != 0) { + NULL, NULL)) > 0) { if (!m_threadPool.shouldStop()) { if (m_threadPool.countThreads() >= m_maxConnections) { // close the connection at once: @@ -559,7 +571,7 @@ bool ReTCPServer::listenForAll() { reCloseSocket(clientSocket); } else { ReTCPServerConnection* connection = createConnection( - nextId++, clientSocket, addrClient); + nextId++, clientSocket); connection->setLogSendReceive(m_logSendReceive); if (!m_threadPool.startThread(connection)) { m_logger->sayF(LOG_ERROR | CAT_PROCESS, @@ -574,6 +586,11 @@ bool ReTCPServer::listenForAll() { } } } + if (clientSocket < 0) + m_logger->sayF(LOG_ERROR | CAT_PROCESS, + LC_LISTEN_FOR_ALL_6, + i18n("accept() failed: $1")).arg( + getLastOSError()).end(); if (m_threadPool.shouldStop()) { m_logger->say(LOG_INFO | CAT_PROCESS, LC_LISTEN_FOR_ALL_7, i18n("stop request received")); diff --git a/net/ReTCP.hpp b/net/ReTCP.hpp index 05066ba..67f20f9 100644 --- a/net/ReTCP.hpp +++ b/net/ReTCP.hpp @@ -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; -- 2.39.5