X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnntp%2Fnntplistener.cpp;h=6355ebc1abba88164736a9c62b341400c48d0557;hb=cd75427de26fc2ebc9baad558b1c0a081faa3b43;hp=01fa442c8a4c5d6a1c5be07d2a9ed9fdffb8d737;hpb=0574a75431d98ed64c5cc6291600bb3759b399a6;p=fms.git diff --git a/src/nntp/nntplistener.cpp b/src/nntp/nntplistener.cpp index 01fa442..6355ebc 100644 --- a/src/nntp/nntplistener.cpp +++ b/src/nntp/nntplistener.cpp @@ -1,10 +1,13 @@ #include "../../include/nntp/nntplistener.h" #include "../../include/nntp/nntpconnection.h" #include "../../include/option.h" -#include "../../include/logfile.h" #include "../../include/global.h" #include "../../include/stringfunctions.h" +#include + +#include + #ifdef _WIN32 #include #include @@ -27,7 +30,7 @@ NNTPListener::~NNTPListener() } -void NNTPListener::Run() +void NNTPListener::run() { int rval; fd_set readfs; @@ -35,7 +38,7 @@ void NNTPListener::Run() std::vector::iterator listeni; SOCKET highsocket; - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread started."); + m_log->debug("NNTPListener::run thread started."); StartListen(); @@ -73,63 +76,14 @@ void NNTPListener::Run() struct sockaddr_storage 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)); - //m_connectionthreads.push_back(new PThread::Thread(new NNTPConnection(newsock))); - m_connections.Execute(new NNTPConnection(newsock)); + m_log->information("NNTPListener::run NNTP client connected"); + m_connections.Start(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(!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(); - } - delete (*i); - } - */ m_connections.Cancel(); m_connections.Join(); @@ -143,7 +97,7 @@ void NNTPListener::Run() } m_listensockets.clear(); - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPListener::run thread exiting."); + m_log->debug("NNTPListener::run thread exiting."); } @@ -175,29 +129,47 @@ void NNTPListener::StartListen() hint.ai_socktype=SOCK_STREAM; hint.ai_protocol=IPPROTO_TCP; hint.ai_flags=AI_PASSIVE; + + m_log->trace("NNTPListener::StartListen getting address info for "+(*i)); rval=getaddrinfo((*i).c_str(),nntpport.c_str(),&hint,&result); if(rval==0) { for(current=result; current!=NULL; current=current->ai_next) { - sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol); - if(sock!=INVALID_SOCKET) + try { - #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) + Poco::Net::SocketAddress sa(current->ai_addr,current->ai_addrlen); + + m_log->debug("NNTPListener::StartListen trying to create socket, bind, and listen on "+sa.toString()); + + sock=socket(current->ai_family,current->ai_socktype,current->ai_protocol); + if(sock!=INVALID_SOCKET) { - if(listen(sock,10)==0) + #ifndef _WIN32 + const char optval='1'; + setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval)); + #endif + if(bind(sock,current->ai_addr,current->ai_addrlen)==0) { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport); - m_listensockets.push_back(sock); + if(listen(sock,10)==0) + { + m_log->information("NNTPListener::StartListen started listening on "+sa.toString()); + m_listensockets.push_back(sock); + } + else + { + m_log->error("NNTPListener::StartListen socket listen failed"); + #ifdef _WIN32 + closesocket(sock); + #else + close(sock); + #endif + } } else { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed"); + m_log->error("NNTPListener::StartListen socket bind failed"); #ifdef _WIN32 closesocket(sock); #else @@ -207,17 +179,18 @@ void NNTPListener::StartListen() } else { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed"); - #ifdef _WIN32 - closesocket(sock); - #else - close(sock); - #endif + m_log->error("NNTPListener::StartListen couldn't create socket"); } } - else + catch(Poco::Exception &e) + { + m_log->error("NNTPListener::StartListen caught "+e.displayText()); + continue; + } + catch(...) { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket"); + m_log->error("NNTPListener::StartListen caught unknown exception"); + continue; } } } @@ -228,6 +201,6 @@ void NNTPListener::StartListen() } if(m_listensockets.size()==0) { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_FATAL,"NNTPListener::StartListen couldn't start listening on any sockets"); + m_log->fatal("NNTPListener::StartListen couldn't start listening on any interfaces"); } }