]> gitweb.hamatoma.de Git - crepublib/commitdiff
shouldStop for ReTCPPool
authorhama <hama@siduction.net>
Mon, 16 Mar 2015 23:14:12 +0000 (00:14 +0100)
committerhama <hama@siduction.net>
Mon, 16 Mar 2015 23:14:12 +0000 (00:14 +0100)
base/ReThread.cpp
base/ReThread.hpp
net/ReTCP.cpp

index 0fcc6cd4038856c6e5377826d636526613a277fe..6d6c768ecbb4b36ea74ace15d04a6980a1c1594f 100644 (file)
@@ -28,7 +28,7 @@ ReThread::ReThread(bool autoDelete) :
            m_threadId(-1),\r
            m_threadLogger(false),\r
            m_appender(NULL),\r
-           m_starter(NULL),\r
+           m_pool(NULL),\r
 #if defined __linux__\r
            m_threadInfo(),\r
 #elif defined __WIN32__\r
@@ -84,7 +84,7 @@ void ReThread::kill() {
 bool ReThread::prepareToRun(int id, ReLogger* masterLogger,\r
     ReThreadPool* starter) {\r
        bool rc = false;\r
-       if (m_starter != NULL) {\r
+       if (m_pool != NULL) {\r
                globalLogger()->say(LOG_ERROR | CAT_LIB, LC_PREPARE_TO_RUN_1,\r
                    i18n("setMasterLogger() is called multiple times"));\r
        } else {\r
@@ -94,7 +94,7 @@ bool ReThread::prepareToRun(int id, ReLogger* masterLogger,
                            '0' + (id % ('z' - '0' + 1)));\r
                else\r
                        m_appender->setMasterLogger(masterLogger);\r
-               m_starter = starter;\r
+               m_pool = starter;\r
                rc = true;\r
        }\r
        return rc;\r
index de73c0ec3037b6b642793d9086d804edcee08af2..e9ea9730619752b4b34e42f94208a68e8984d5ee 100644 (file)
@@ -54,7 +54,7 @@ protected:
        int m_threadId;\r
        ReLogger m_threadLogger;\r
        ReSlaveAppender* m_appender;\r
-       ReThreadPool* m_starter;\r
+       ReThreadPool* m_pool;\r
 #if defined __linux__\r
        pthread_t m_threadInfo;\r
 #elif defined __WIN32__\r
@@ -87,6 +87,17 @@ public:
        bool startThread(ReThread* thread);\r
        bool waitForAlmostAll(int mayResist, int timeoutSec);\r
        bool waitForDone(int timeoutSec);\r
+       /** Returns whether the threads should stop.\r
+        * @return      <code>true</code>: all threads should stop\r
+        */\r
+       inline bool shouldStop() const {\r
+               return m_shouldStop;\r
+       }\r
+       /** Sets that the threads should stop.\r
+        */\r
+       inline void setShouldStop() {\r
+               m_shouldStop = true;\r
+       }\r
 private:\r
        bool insertThread(ReThread* thread);\r
 private:\r
@@ -96,5 +107,6 @@ private:
        ReThread** m_threads;\r
        int m_maxKillTimeSec;\r
        ReMutex m_mutexThreads;\r
+       bool m_shouldStop;\r
 };\r
 #endif /* BASE_RETHREAD_HPP_ */\r
index 4d9fc2fc1a17f58f00dd8edef1b8ace9af298c9e..1cc70cf6b55ca0838ae5c580ea15fd150e0e5c55 100644 (file)
@@ -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"));
                        }
                }
        }