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
*/\r
void ReTCPConnection::globalClose() {\r
#if defined __WIN32__\r
- WSACleanup();\r
+ if (isGlobalInitialized)\r
+ WSACleanup();\r
#endif\r
}\r
\r
* @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
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
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
}\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