#include "../../include/global.h"\r
#include "../../include/stringfunctions.h"\r
\r
+#include <cstring>\r
+\r
#ifdef _WIN32\r
#include <winsock2.h>\r
#include <ws2tcpip.h>\r
\r
}\r
\r
-void NNTPListener::run()\r
+void NNTPListener::Run()\r
{\r
int rval;\r
fd_set readfs;\r
std::vector<SOCKET>::iterator listeni;\r
SOCKET highsocket;\r
\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread started.");\r
+\r
StartListen();\r
\r
do\r
{\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
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected");\r
+ //m_connections.execute(new NNTPConnection(newsock));\r
+ //m_connectionthreads.push_back(new PThread::Thread(new NNTPConnection(newsock)));\r
+ m_connections.Execute(new NNTPConnection(newsock));\r
}\r
}\r
}\r
+/*\r
+ // check for any non-running connection threads that we can delete\r
+ for(std::vector<PThread::Thread *>::iterator i=m_connectionthreads.begin(); i!=m_connectionthreads.end(); )\r
+ {\r
+ if((*i)->IsRunning()==false)\r
+ {\r
+ delete (*i);\r
+ i=m_connectionthreads.erase(i);\r
+ }\r
+ if(i!=m_connectionthreads.end())\r
+ {\r
+ i++;\r
+ }\r
+ }\r
+*/\r
\r
- }while(!ZThread::Thread::interrupted() && m_listensockets.size()>0);\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
+ /*\r
if(m_connections.wait(1)==false)\r
{\r
LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run interrupting connection threads and waiting 60 seconds for exit.");\r
{\r
LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run caught unhandled exception.");\r
}\r
- if(m_connections.wait(60)==false)\r
+ if(m_connections.wait(60000)==false)\r
{\r
LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run connection threads did not exit after 60 seconds.");\r
}\r
}\r
+ */\r
+ /*\r
+ for(std::vector<PThread::Thread *>::iterator i=m_connectionthreads.begin(); i!=m_connectionthreads.end(); i++)\r
+ {\r
+ if((*i)->IsRunning())\r
+ {\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::Run waiting for connection thread to exit.");\r
+ (*i)->Cancel();\r
+ (*i)->Join();\r
+ }\r
+ delete (*i);\r
+ }\r
+ */\r
+ m_connections.Cancel();\r
+ m_connections.Join();\r
\r
for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++)\r
{\r
}\r
m_listensockets.clear();\r
\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread exiting.");\r
+\r
}\r
\r
void NNTPListener::StartListen()\r
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
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,1);\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
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport);\r
m_listensockets.push_back(sock);\r
}\r
else\r
{\r
- LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed");\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed");\r
#ifdef _WIN32\r
closesocket(sock);\r
#else\r
}\r
else\r
{\r
- LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed");\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed");\r
#ifdef _WIN32\r
closesocket(sock);\r
#else\r
}\r
else\r
{\r
- LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket");\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket");\r
}\r
}\r
}\r
}\r
if(m_listensockets.size()==0)\r
{\r
- LogFile::instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets");\r
+ LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets");\r
}\r
}\r