From: hama Date: Mon, 16 Mar 2015 23:14:12 +0000 (+0100) Subject: shouldStop for ReTCPPool X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=e017fbef3a3147a40e14abc019411a56f0ae94cf;p=crepublib shouldStop for ReTCPPool --- diff --git a/base/ReThread.cpp b/base/ReThread.cpp index 0fcc6cd..6d6c768 100644 --- a/base/ReThread.cpp +++ b/base/ReThread.cpp @@ -28,7 +28,7 @@ ReThread::ReThread(bool autoDelete) : m_threadId(-1), m_threadLogger(false), m_appender(NULL), - m_starter(NULL), + m_pool(NULL), #if defined __linux__ m_threadInfo(), #elif defined __WIN32__ @@ -84,7 +84,7 @@ void ReThread::kill() { bool ReThread::prepareToRun(int id, ReLogger* masterLogger, ReThreadPool* starter) { bool rc = false; - if (m_starter != NULL) { + if (m_pool != NULL) { globalLogger()->say(LOG_ERROR | CAT_LIB, LC_PREPARE_TO_RUN_1, i18n("setMasterLogger() is called multiple times")); } else { @@ -94,7 +94,7 @@ bool ReThread::prepareToRun(int id, ReLogger* masterLogger, '0' + (id % ('z' - '0' + 1))); else m_appender->setMasterLogger(masterLogger); - m_starter = starter; + m_pool = starter; rc = true; } return rc; diff --git a/base/ReThread.hpp b/base/ReThread.hpp index de73c0e..e9ea973 100644 --- a/base/ReThread.hpp +++ b/base/ReThread.hpp @@ -54,7 +54,7 @@ protected: int m_threadId; ReLogger m_threadLogger; ReSlaveAppender* m_appender; - ReThreadPool* m_starter; + ReThreadPool* m_pool; #if defined __linux__ pthread_t m_threadInfo; #elif defined __WIN32__ @@ -87,6 +87,17 @@ public: bool startThread(ReThread* thread); bool waitForAlmostAll(int mayResist, int timeoutSec); bool waitForDone(int timeoutSec); + /** Returns whether the threads should stop. + * @return true: all threads should stop + */ + inline bool shouldStop() const { + return m_shouldStop; + } + /** Sets that the threads should stop. + */ + inline void setShouldStop() { + m_shouldStop = true; + } private: bool insertThread(ReThread* thread); private: @@ -96,5 +107,6 @@ private: ReThread** m_threads; int m_maxKillTimeSec; ReMutex m_mutexThreads; + bool m_shouldStop; }; #endif /* BASE_RETHREAD_HPP_ */ diff --git a/net/ReTCP.cpp b/net/ReTCP.cpp index 4d9fc2f..1cc70cf 100644 --- a/net/ReTCP.cpp +++ b/net/ReTCP.cpp @@ -355,7 +355,9 @@ void ReTCPServerConnection::run() { i18n("unknown command: $1 length: $2")).arg(command).arg( m_received.length()).end(); } - } while (rc != ReNetCommandHandler::PS_STOP); + } while (rc != ReNetCommandHandler::PS_STOP && ! m_shouldStop); + if (rc != ReNetCommandHandler::PS_STOP) + m_pool->setShouldStop(); close(); m_id = -1; } @@ -434,7 +436,7 @@ bool ReTCPServer::listenForAll() { bool rc = false; struct addrinfo hints; struct addrinfo* addrInfo; - ReThreadPool threadStarter(m_maxConnections + 1, m_logger); + ReThreadPool pool(m_maxConnections + 1, m_logger); // first, load up address structs with getaddrinfo(): memset(&hints, 0, sizeof hints); @@ -482,37 +484,35 @@ bool ReTCPServer::listenForAll() { socklen_t lengthAddr = sizeof(struct sockaddr_in); while ((clientSocket = accept(m_handleSocket, (struct sockaddr *) &addrClient, &lengthAddr)) != 0) { - m_logger->sayF(LOG_INFO | CAT_NETWORK, LC_LISTEN_FOR_ALL_4, - i18n("accepted: $1")).arg(m_port).end(); - if (m_countConnections >= m_maxConnections) { - // close the connection atonce: - m_logger->sayF(LOG_WARNING | CAT_NETWORK, - LC_LISTEN_FOR_ALL_5, - i18n("connection refused (too many connections): $1")) - .arg(m_port).end(); - reCloseSocket(clientSocket); - } else { - pthread_t sniffer_thread; - ReTCPServerConnection* connection = createConnection( - nextId++, clientSocket, addrClient); - - if (!threadStarter.startThread(connection)) { - m_logger->sayF(LOG_ERROR | CAT_PROCESS, - LC_LISTEN_FOR_ALL_6, - i18n("cannot create a thread: $1")).arg( - getLastOSError()).end(); + if (! pool.shouldStop()) { + m_logger->sayF(LOG_INFO | CAT_NETWORK, LC_LISTEN_FOR_ALL_4, + i18n("accepted: $1")).arg(m_port).end(); + if (m_countConnections >= m_maxConnections) { + // close the connection at once: + m_logger->sayF(LOG_WARNING | CAT_NETWORK, + LC_LISTEN_FOR_ALL_5, + i18n( + "connection refused (too many connections): $1")) + .arg(m_port).end(); reCloseSocket(clientSocket); - clientSocket = -1; + } else { + ReTCPServerConnection* connection = createConnection( + nextId++, clientSocket, addrClient); + + if (!pool.startThread(connection)) { + m_logger->sayF(LOG_ERROR | CAT_PROCESS, + LC_LISTEN_FOR_ALL_6, + i18n("cannot create a thread: $1")).arg( + getLastOSError()).end(); + reCloseSocket(clientSocket); + clientSocket = -1; + } } - - //Now join the thread , so that we dont terminate before the thread - //pthread_join( sniffer_thread , NULL); - puts("Handler assigned"); } } - if (clientSocket < 0) { - perror("accept failed"); - return 1; + if (pool.shouldStop()) { + m_logger->say(LOG_INFO | CAT_PROCESS, LC_LISTEN_FOR_ALL_7, + i18n("stop request received")); } } }