NLNET::CListenTask Class Reference

#include <buf_server.h>

Inheritance diagram for NLNET::CListenTask:

NLMISC::IRunnable NLNET::CServerTask

Detailed Description

Code of listening thread

Definition at line 87 of file buf_server.h.

Public Member Functions

 CListenTask (CBufServer *server)
 Constructor.

void close ()
 Close listening socket.

virtual void getName (std::string &result) const
void init (uint16 port, sint32 maxExpectedBlockSize)
 Begins to listen on the specified port (call before running thread).

const CInetAddresslocalAddr ()
 Returns the listening address.

void requireExit ()
 Tells the task to exit.

virtual void run ()
 Run (exits when the listening socket disconnects).


Data Fields

uint32 NbLoop

Protected Member Functions

bool exitRequired () const
 Returns true if the requireExit() has been called.


Private Attributes

CListenSock _ListenSock
uint32 _MaxExpectedBlockSize
CBufServer_Server


Constructor & Destructor Documentation

NLNET::CListenTask::CListenTask CBufServer server  )  [inline]
 

Constructor.

Definition at line 92 of file buf_server.h.

References _Server.

00092 : CServerTask(), _Server(server) {}


Member Function Documentation

void NLNET::CListenTask::close  )  [inline]
 

Close listening socket.

Definition at line 101 of file buf_server.h.

References _ListenSock, and NLNET::CSock::close().

00101 { _ListenSock.close(); }

bool NLNET::CServerTask::exitRequired  )  const [inline, protected, inherited]
 

Returns true if the requireExit() has been called.

Definition at line 71 of file buf_server.h.

References NLNET::CServerTask::_ExitRequired.

Referenced by NLNET::CServerReceiveTask::run(), and run().

00071 { return _ExitRequired; }

virtual void NLMISC::IRunnable::getName std::string &  result  )  const [inline, virtual, inherited]
 

Reimplemented in NL3D::CAsyncFileManager3D::CMeshLoad, NL3D::CAsyncFileManager3D::CIGLoad, NL3D::CAsyncFileManager3D::CIGLoadUser, NL3D::CAsyncFileManager3D::CTextureLoad, NL3D::CZoneLoadingTask, NLPACS::CGlobalRetriever::CLrLoader, NLMISC::CAsyncFileManager::CFileLoad, NLMISC::CAsyncFileManager::CMultipleFileLoad, and NLMISC::CAsyncFileManager::CSignal.

Definition at line 74 of file thread.h.

Referenced by NLMISC::CTaskManager::run().

00075         {
00076                 result = "NoName";
00077         }

void NLNET::CListenTask::init uint16  port,
sint32  maxExpectedBlockSize
 

Begins to listen on the specified port (call before running thread).

Definition at line 111 of file buf_server.cpp.

References _ListenSock, NLNET::CListenSock::init(), nlnettrace, sint32, and uint16.

Referenced by NLNET::CBufServer::init().

00112 {
00113         nlnettrace( "CListenTask::init" );
00114         _ListenSock.init( port );
00115         _MaxExpectedBlockSize = maxExpectedBlockSize;
00116 }

const CInetAddress& NLNET::CListenTask::localAddr  )  [inline]
 

Returns the listening address.

Definition at line 104 of file buf_server.h.

References _ListenSock, and NLNET::CSock::localAddr().

Referenced by NLNET::CBufServer::listenAddress().

00104 { return _ListenSock.localAddr(); }

void NLNET::CServerTask::requireExit  )  [inline, inherited]
 

Tells the task to exit.

Definition at line 56 of file buf_server.h.

References NLNET::CServerTask::_ExitRequired.

Referenced by NLNET::CBufServer::~CBufServer().

00056 { _ExitRequired = true; }

void NLNET::CListenTask::run  )  [virtual]
 

Run (exits when the listening socket disconnects).

Implements NLMISC::IRunnable.

Definition at line 779 of file buf_server.cpp.

References _ListenSock, _Server, NLNET::CListenSock::accept(), NLNET::CServerBufSock::advertiseConnection(), NLNET::CBufSock::asString(), NLNET::CSock::descriptor(), NLNET::CBufServer::dispatchNewSocket(), NLNET::CServerTask::exitRequired(), NLNET::NbNetworkTask, NLNET::NbServerListenTask, nldebug, nlerror, nlinfo, nlnettrace, NLNET::CBufServer::noDelay(), res, NLNET::CNonBlockingBufSock::setMaxExpectedBlockSize(), NLNET::CTcpSock::setNoDelay(), NLNET::CNonBlockingBufSock::setNonBlocking(), NLNET::CBufSock::Sock, and uint8.

00780 {
00781         NbNetworkTask++;
00782         NbServerListenTask++;
00783 
00784         nlnettrace( "CListenTask::run" );
00785 
00786 #ifdef NL_OS_UNIX
00787         SOCKET descmax;
00788         fd_set readers;
00789         timeval tv;
00790         descmax = _ListenSock.descriptor()>_WakeUpPipeHandle[PipeRead]?_ListenSock.descriptor():_WakeUpPipeHandle[PipeRead];
00791 #endif
00792 
00793         // Accept connections
00794         while ( ! exitRequired() )
00795         {
00796                 try
00797                 {
00798                         // Get and setup the new socket
00799 #ifdef NL_OS_UNIX
00800                         FD_ZERO( &readers );
00801                         FD_SET( _ListenSock.descriptor(), &readers );
00802                         FD_SET( _WakeUpPipeHandle[PipeRead], &readers );
00803                         tv.tv_sec = 60; 
00804                         tv.tv_usec = 0;
00805                         int res = ::select( descmax+1, &readers, NULL, NULL, &tv );
00806 
00807                         switch ( res )
00808                         {
00809                         case  0 : continue; // time-out expired, no results
00810                         case -1 :
00811                                 // we'll ignore message (Interrupted system call) caused by a CTRL-C
00812                                 if (CSock::getLastError() == 4)
00813                                 {
00814                                         nldebug ("LNETL1: Select failed (in listen thread): %s (code %u) but IGNORED", CSock::errorString( CSock::getLastError() ).c_str(), CSock::getLastError());
00815                                         continue;
00816                                 }
00817                                 nlerror( "LNETL1: Select failed (in listen thread): %s (code %u)", CSock::errorString( CSock::getLastError() ).c_str(), CSock::getLastError() );
00818                         }
00819 
00820                         if ( FD_ISSET( _WakeUpPipeHandle[PipeRead], &readers ) )
00821                         {
00822                                 uint8 b;
00823                                 if ( read( _WakeUpPipeHandle[PipeRead], &b, 1 ) == -1 ) // we were woken-up by the wake-up pipe
00824                                 {
00825                                         nldebug( "LNETL1: In CListenTask::run(): read() failed" );
00826                                 }
00827                                 nldebug( "LNETL1: listen thread select woken-up" );
00828                                 continue;
00829                         }
00830 #endif
00831                         nldebug( "LNETL1: Waiting incoming connection..." );
00832                         CServerBufSock *bufsock = new CServerBufSock( _ListenSock.accept() );
00833                         nldebug( "LNETL1: New connection : %s", bufsock->asString().c_str() );
00834                         bufsock->setNonBlocking();
00835                         bufsock->setMaxExpectedBlockSize( _MaxExpectedBlockSize );
00836                         if ( _Server->noDelay() )
00837                         {
00838                                 bufsock->Sock->setNoDelay( true );
00839                         }
00840 
00841                         // Notify the new connection
00842                         bufsock->advertiseConnection( _Server );
00843 
00844                         // Dispatch the socket into the thread pool
00845                         _Server->dispatchNewSocket( bufsock );
00846 
00847                         NbLoop++;
00848                 }
00849                 catch ( ESocket& e )
00850                 {
00851                         nlinfo( "LNETL1: Exception in listen thread: %s", e.what() ); // It can occur in normal behavior (e.g. when exiting)
00852                         // It can also occur when too many sockets are open (e.g. 885 connections)
00853                 }
00854         }
00855 
00856         nlnettrace( "Exiting CListenTask::run" );
00857         NbServerListenTask--;
00858         NbNetworkTask--;
00859 }


Field Documentation

CListenSock NLNET::CListenTask::_ListenSock [private]
 

Definition at line 109 of file buf_server.h.

Referenced by close(), init(), localAddr(), and run().

uint32 NLNET::CListenTask::_MaxExpectedBlockSize [private]
 

Definition at line 110 of file buf_server.h.

CBufServer* NLNET::CListenTask::_Server [private]
 

Definition at line 108 of file buf_server.h.

Referenced by CListenTask(), and run().

uint32 NLNET::CServerTask::NbLoop [inherited]
 

Definition at line 63 of file buf_server.h.

Referenced by NLNET::CBufServer::displayThreadStat().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:58:21 2004 for NeL by doxygen 1.3.6