X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnntp%2Fnntplistener.cpp;h=860da2cc064fdbecb62e6273f08bba3714740ff2;hb=023b2219068d481cd6a300982427c99bacd88a12;hp=29f8eb4752dbe442cec9c8c2ecf7a8d211cb8183;hpb=868c533e84b3c81b6604b45b84efa32073aa20b4;p=fms.git diff --git a/src/nntp/nntplistener.cpp b/src/nntp/nntplistener.cpp index 29f8eb4..860da2c 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 @@ -35,6 +38,8 @@ void NNTPListener::run() std::vector::iterator listeni; SOCKET highsocket; + m_log->debug("NNTPListener::run thread started."); + StartListen(); do @@ -69,28 +74,30 @@ 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)); + m_log->information("NNTPListener::run NNTP client connected"); + m_connections.Start(new NNTPConnection(newsock)); } } } - }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) + m_connections.Cancel(); + m_connections.Join(); + + for(listeni=m_listensockets.begin(); listeni!=m_listensockets.end(); listeni++) { - try - { - m_connections.interrupt(); - } - catch(...) - { - } - m_connections.wait(); + #ifdef _WIN32 + closesocket((*listeni)); + #else + close((*listeni)); + #endif } + m_listensockets.clear(); + + m_log->debug("NNTPListener::run thread exiting."); } @@ -100,15 +107,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); @@ -128,19 +135,26 @@ void NNTPListener::StartListen() { for(current=result; current!=NULL; current=current->ai_next) { + 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) { + #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) { if(listen(sock,10)==0) { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPListener::StartListen started listening at "+(*i)+":"+nntpport); + m_log->information("NNTPListener::StartListen started listening on "+sa.toString()); m_listensockets.push_back(sock); } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket listen failed"); + m_log->error("NNTPListener::StartListen socket listen failed"); #ifdef _WIN32 closesocket(sock); #else @@ -150,7 +164,7 @@ void NNTPListener::StartListen() } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen socket bind failed"); + m_log->error("NNTPListener::StartListen socket bind failed"); #ifdef _WIN32 closesocket(sock); #else @@ -160,7 +174,7 @@ void NNTPListener::StartListen() } else { - LogFile::instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPListener::StartListen couldn't create socket"); + m_log->error("NNTPListener::StartListen couldn't create socket"); } } } @@ -171,6 +185,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 sockets"); } }