version 0.3.4
[fms.git] / src / nntp / nntplistener.cpp
index 29f8eb4..860da2c 100644 (file)
@@ -1,10 +1,13 @@
 #include "../../include/nntp/nntplistener.h"\r
 #include "../../include/nntp/nntpconnection.h"\r
 #include "../../include/option.h"\r
-#include "../../include/logfile.h"\r
 #include "../../include/global.h"\r
 #include "../../include/stringfunctions.h"\r
 \r
+#include <Poco/Net/SocketAddress.h>\r
+\r
+#include <cstring>\r
+\r
 #ifdef _WIN32\r
        #include <winsock2.h>\r
        #include <ws2tcpip.h>\r
@@ -35,6 +38,8 @@ void NNTPListener::run()
        std::vector<SOCKET>::iterator listeni;\r
        SOCKET highsocket;\r
 \r
+       m_log->debug("NNTPListener::run thread started.");\r
+\r
        StartListen();\r
 \r
        do\r
@@ -69,28 +74,30 @@ void NNTPListener::run()
                                {\r
                                        SOCKET newsock;\r
                                        struct sockaddr_storage addr;\r
-                                       int addrlen=sizeof(addr);\r
+                                       socklen_t addrlen=sizeof(addr);\r
                                        newsock=accept((*listeni),(struct sockaddr *)&addr,&addrlen);\r
-                                       LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected");\r
-                                       m_connections.execute(new NNTPConnection(newsock));\r
+                                       m_log->information("NNTPListener::run NNTP client connected");\r
+                                       m_connections.Start(new NNTPConnection(newsock));\r
                                }\r
                        }\r
                }\r
 \r
-       }while(!ZThread::Thread::interrupted() && m_listensockets.size()>0);\r
+       }while(!IsCancelled() && m_listensockets.size()>0);\r
 \r
-       // see if any threads are still running - just calling interrupt without check would cause assert in debug mode\r
-       if(m_connections.wait(1)==false)\r
+       m_connections.Cancel();\r
+       m_connections.Join();\r
+\r
+       for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)\r
        {\r
-               try\r
-               {\r
-                       m_connections.interrupt();\r
-               }\r
-               catch(...)\r
-               {\r
-               }\r
-               m_connections.wait();\r
+               #ifdef _WIN32\r
+               closesocket((*listeni));\r
+               #else\r
+               close((*listeni));\r
+               #endif\r
        }\r
+       m_listensockets.clear();\r
+\r
+       m_log->debug("NNTPListener::run thread exiting.");\r
 \r
 }\r
 \r
@@ -100,15 +107,15 @@ void NNTPListener::StartListen()
        std::string bindaddresses;\r
        std::vector<std::string> listenaddresses;\r
        std::string nntpport;\r
-       if(Option::instance()->Get("NNTPListenPort",nntpport)==false)\r
+       if(Option::Instance()->Get("NNTPListenPort",nntpport)==false)\r
        {\r
                nntpport="1119";\r
-               Option::instance()->Set("NNTPListenPort",nntpport);\r
+               Option::Instance()->Set("NNTPListenPort",nntpport);\r
        }\r
-       if(Option::instance()->Get("NNTPBindAddresses",bindaddresses)==false)\r
+       if(Option::Instance()->Get("NNTPBindAddresses",bindaddresses)==false)\r
        {\r
                bindaddresses="127.0.0.1";\r
-               Option::instance()->Set("NNTPBindAddresses",bindaddresses);\r
+               Option::Instance()->Set("NNTPBindAddresses",bindaddresses);\r
        }\r
        StringFunctions::Split(bindaddresses,",",listenaddresses);\r
        \r
@@ -128,19 +135,26 @@ void NNTPListener::StartListen()
                {\r
                        for(current=result; current!=NULL; current=current->ai_next)\r
                        {\r
+                               Poco::Net::SocketAddress sa(current->ai_addr,current->ai_addrlen);\r
+                               m_log->debug("NNTPListener::StartListen trying to create socket, bind, and listen on "+sa.toString());\r
+\r
                                sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol);\r
                                if(sock!=INVALID_SOCKET)\r
                                {\r
+                                       #ifndef _WIN32\r
+                                       const char optval='1';\r
+                                       setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));\r
+                                       #endif\r
                                        if(bind(sock,current->ai_addr,current->ai_addrlen)==0)\r
                                        {\r
                                                if(listen(sock,10)==0)\r
                                                {\r
-                                                       LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport);\r
+                                                       m_log->information("NNTPListener::StartListen started listening on "+sa.toString());\r
                                                        m_listensockets.push_back(sock);\r
                                                }\r
                                                else\r
                                                {\r
-                                                       LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed");\r
+                                                       m_log->error("NNTPListener::StartListen socket listen failed");\r
                                                        #ifdef _WIN32\r
                                                        closesocket(sock);\r
                                                        #else\r
@@ -150,7 +164,7 @@ void NNTPListener::StartListen()
                                        }\r
                                        else\r
                                        {\r
-                                               LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed");\r
+                                               m_log->error("NNTPListener::StartListen socket bind failed");\r
                                                #ifdef _WIN32\r
                                                closesocket(sock);\r
                                                #else\r
@@ -160,7 +174,7 @@ void NNTPListener::StartListen()
                                }\r
                                else\r
                                {\r
-                                       LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket");\r
+                                       m_log->error("NNTPListener::StartListen couldn't create socket");\r
                                }\r
                        }\r
                }\r
@@ -171,6 +185,6 @@ void NNTPListener::StartListen()
        }\r
        if(m_listensockets.size()==0)\r
        {\r
-               LogFile::instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets");\r
+               m_log->fatal("NNTPListener::StartListen couldn't start listening on any sockets");\r
        }\r
 }\r