X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnntp%2Fnntplistener.cpp;h=01fa442c8a4c5d6a1c5be07d2a9ed9fdffb8d737;hb=0574a75431d98ed64c5cc6291600bb3759b399a6;hp=4d1c37dba0fe8bdca85f22d0311068f5e15758a4;hpb=6b896a9e1dc143bba86795be1e9336549db9b85f;p=fms.git diff --git a/src/nntp/nntplistener.cpp b/src/nntp/nntplistener.cpp index 4d1c37d..01fa442 100644 --- a/src/nntp/nntplistener.cpp +++ b/src/nntp/nntplistener.cpp @@ -9,7 +9,8 @@ #include #include #else - + #include // gcc - IPPROTO_ consts + #include // gcc - addrinfo #endif #ifdef XMEM @@ -26,14 +27,16 @@ NNTPListener::~NNTPListener() } -void NNTPListener::run() +void NNTPListener::Run() { int rval; - struct fd_set readfs; + fd_set readfs; struct timeval tv; std::vector::iterator listeni; SOCKET highsocket; + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread started."); + StartListen(); do @@ -68,28 +71,79 @@ void NNTPListener::run() { SOCKET newsock; struct sockaddr_storage addr; - int addrlen=sizeof(addr); + socklen_t addrlen=sizeof(addr); newsock=accept((*listeni),(struct sockaddr *)&addr,&addrlen); - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected"); - m_connections.execute(new NNTPConnection(newsock)); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::run NNTP client connected"); + //m_connections.execute(new NNTPConnection(newsock)); + //m_connectionthreads.push_back(new PThread::Thread(new NNTPConnection(newsock))); + m_connections.Execute(new NNTPConnection(newsock)); } } } +/* + // check for any non-running connection threads that we can delete + for(std::vector::iterator i=m_connectionthreads.begin(); i!=m_connectionthreads.end(); ) + { + if((*i)->IsRunning()==false) + { + delete (*i); + i=m_connectionthreads.erase(i); + } + if(i!=m_connectionthreads.end()) + { + i++; + } + } +*/ - }while(!ZThread::Thread::interrupted() && m_listensockets.size()>0); + //}while(!ZThread::Thread::interrupted() && m_listensockets.size()>0); + }while(!IsCancelled() && m_listensockets.size()>0); // see if any threads are still running - just calling interrupt without check would cause assert in debug mode + /* if(m_connections.wait(1)==false) { + LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run interrupting connection threads and waiting 60 seconds for exit."); try { m_connections.interrupt(); } catch(...) { + LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run caught unhandled exception."); + } + if(m_connections.wait(60000)==false) + { + LogFile::instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run connection threads did not exit after 60 seconds."); + } + } + */ + /* + for(std::vector::iterator i=m_connectionthreads.begin(); i!=m_connectionthreads.end(); i++) + { + if((*i)->IsRunning()) + { + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::Run waiting for connection thread to exit."); + (*i)->Cancel(); + (*i)->Join(); } - m_connections.wait(); + delete (*i); } + */ + m_connections.Cancel(); + m_connections.Join(); + + for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++) + { + #ifdef _WIN32 + closesocket((*listeni)); + #else + close((*listeni)); + #endif + } + m_listensockets.clear(); + + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread exiting."); } @@ -99,15 +153,15 @@ void NNTPListener::StartListen() std::string bindaddresses; std::vector listenaddresses; std::string nntpport; - if(Option::instance()->Get("NNTPListenPort",nntpport)==false) + if(Option::Instance()->Get("NNTPListenPort",nntpport)==false) { nntpport="1119"; - Option::instance()->Set("NNTPListenPort",nntpport); + Option::Instance()->Set("NNTPListenPort",nntpport); } - if(Option::instance()->Get("NNTPBindAddresses",bindaddresses)==false) + if(Option::Instance()->Get("NNTPBindAddresses",bindaddresses)==false) { bindaddresses="127.0.0.1"; - Option::instance()->Set("NNTPBindAddresses",bindaddresses); + Option::Instance()->Set("NNTPBindAddresses",bindaddresses); } StringFunctions::Split(bindaddresses,",",listenaddresses); @@ -130,16 +184,20 @@ void NNTPListener::StartListen() sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol); if(sock!=INVALID_SOCKET) { + #ifndef _WIN32 + const char optval='1'; + setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,1); + #endif if(bind(sock,current->ai_addr,current->ai_addrlen)==0) { if(listen(sock,10)==0) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport); m_listensockets.push_back(sock); } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed"); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed"); #ifdef _WIN32 closesocket(sock); #else @@ -149,7 +207,7 @@ void NNTPListener::StartListen() } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed"); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed"); #ifdef _WIN32 closesocket(sock); #else @@ -159,7 +217,7 @@ void NNTPListener::StartListen() } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket"); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket"); } } } @@ -170,6 +228,6 @@ void NNTPListener::StartListen() } if(m_listensockets.size()==0) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets"); + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets"); } }