Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SocketSet Class Reference

#include <vsnet_socketset.h>

Inheritance diagram for SocketSet:
VSThread

Public Member Functions

 SocketSet (bool blockmainthread=false)
 
 ~SocketSet ()
 
bool addDownloadManager (boost::shared_ptr< VsnetDownload::Client::Manager >mgr)
 
bool addDownloadManager (boost::shared_ptr< VsnetDownload::Server::Manager >mgr)
 
void set (VsnetSocketBase *s)
 
void unset (VsnetSocketBase *s)
 The upper thread takes a socket out of the _autoset. More...
 
int wait (timeval *tv=NULL)
 The upper thread waits for something to arrive on the socket. More...
 
void add_pending (int fd)
 
void rem_pending (int fd)
 
void waste_time (long sec, long usec)
 unthreaded case More...
 
virtual void run ()
 
void wakeup ()
 
void predestroy ()
 only call predestroy if you know the whole set will be destructed soonish–clears the autoset More...
 
- Public Member Functions inherited from VSThread
 VSThread (bool detached)
 
virtual ~VSThread ()
 
void start ()
 
void join ()
 

Additional Inherited Members

- Static Public Member Functions inherited from VSThread
static void init ()
 global initialization function for the thread subsystem More...
 

Detailed Description

Definition at line 56 of file vsnet_socketset.h.

Constructor & Destructor Documentation

SocketSet::SocketSet ( bool  blockmainthread = false)

Definition at line 12 of file vsnet_socketset.cpp.

12  :
13  VSThread( false )
14  , _blockmain( blockmainthread )
15  , _blockmain_pending( 0 )
16 {
17 #ifndef USE_NO_THREAD
18  _thread_end = false;
19 #endif
20  FD_ZERO( &_blockmain_set );
21 }
SocketSet::~SocketSet ( )

Definition at line 23 of file vsnet_socketset.cpp.

References VSMutex::lock(), VSMutex::unlock(), and VSCond::wait().

24 {
25 #ifndef USE_NO_THREAD
26  _thread_mx.lock();
27  _thread_end = true;
28  _blockmain = false; //signalling would be dangerous
29  _thread_wakeup.closewrite();
30  _thread_cond.wait( _thread_mx );
31  _thread_wakeup.closeread();
32  _thread_mx.unlock();
33 #endif
34 }

Member Function Documentation

void SocketSet::add_pending ( int  fd)

Definition at line 120 of file vsnet_socketset.cpp.

References VSMutex::lock(), and VSMutex::unlock().

Referenced by VsnetTCPSocket::inner_complete_a_packet(), VsnetUDPSocket::lower_selected(), VsnetTCPSocket::lower_selected(), and ServerSocketTCP::lower_selected().

121 {
122  if (_blockmain) {
123  _blockmain_mx.lock();
124  FD_SET( fd, &_blockmain_set );
125  if (fd >= _blockmain_pending) _blockmain_pending = fd+1;
126  _blockmain_mx.unlock();
127  }
128 }
bool SocketSet::addDownloadManager ( boost::shared_ptr< VsnetDownload::Client::Manager mgr)

Definition at line 39 of file vsnet_socketset.cpp.

Referenced by NetClient::NetClient().

40 {
41  if ( !_client_mgr.expired() ) return false;
42  _client_mgr = mgr;
43  return true;
44 }
bool SocketSet::addDownloadManager ( boost::shared_ptr< VsnetDownload::Server::Manager mgr)

Definition at line 46 of file vsnet_socketset.cpp.

47 {
48  if ( !_server_mgr.expired() ) return false;
49  _server_mgr = mgr;
50  return true;
51 }
void SocketSet::predestroy ( )

only call predestroy if you know the whole set will be destructed soonish–clears the autoset

Definition at line 35 of file vsnet_socketset.cpp.

36 {
37  _autoset.clear();
38 }
void SocketSet::rem_pending ( int  fd)

Definition at line 130 of file vsnet_socketset.cpp.

References VSMutex::lock(), and VSMutex::unlock().

Referenced by ServerSocketTCP::acceptNewConn(), VsnetTCPSocket::isActive(), VsnetUDPSocket::recvbuf(), and VsnetTCPSocket::recvbuf().

131 {
132  if (_blockmain) {
133  _blockmain_mx.lock();
134  FD_CLR( fd, &_blockmain_set );
135  if (fd == _blockmain_pending-1) {
136  while (_blockmain_pending > 0) {
137  if ( FD_ISSET( _blockmain_pending-1, &_blockmain_set ) )
138  break;
139  _blockmain_pending -= 1;
140  }
141  }
142  _blockmain_mx.unlock();
143  }
144 }
void SocketSet::run ( void  )
virtual

Implements VSThread.

Definition at line 315 of file vsnet_socketset.cpp.

References VSMutex::lock(), VSCond::signal(), and VSMutex::unlock().

316 {
317 #ifndef USE_NO_THREAD
318  while (!_thread_end)
319  private_select( NULL );
320  _thread_mx.lock();
321  _thread_cond.signal();
322  _thread_mx.unlock();
323 #endif
324 }
void SocketSet::set ( VsnetSocketBase s)

Once a socket is registered using this function, setRead is automatically called for it before each select

Definition at line 53 of file vsnet_socketset.cpp.

Referenced by SOCKETALT::addToSet(), VsnetSocketBase::setSet(), and VsnetSocketBase::VsnetSocketBase().

54 {
55  _autoset.insert( s );
56  private_wakeup();
57 }
void SocketSet::unset ( VsnetSocketBase s)

The upper thread takes a socket out of the _autoset.

Definition at line 59 of file vsnet_socketset.cpp.

Referenced by VsnetSocketBase::disconnect(), VsnetSocketBase::setSet(), and VsnetSocketBase::~VsnetSocketBase().

60 {
61  if ( _autoset.size() )
62  _autoset.erase( s );
63  private_wakeup();
64 }
int SocketSet::wait ( timeval *  tv = NULL)

The upper thread waits for something to arrive on the socket.

Definition at line 67 of file vsnet_socketset.cpp.

References COUT, and i.

Referenced by AccountServer::start().

68 {
69  //assert( _blockmain ); // can't call wait if we haven't ordered the feature (?)
70  if (_blockmain_pending == 0) {
71  int ret;
72  do {
73  if (tv == NULL) {
74  ret = private_select( NULL );
75  } else {
76  timeval tvCopy( *tv ); //select resets timeval.
77  ret = private_select( &tvCopy );
78  }
79  } while (ret == 1);
80  return ret;
81  } else {
82 #ifdef VSNET_DEBUG
83  std::ostringstream ostr;
84  for (int i = 0; i < _blockmain_pending; i++)
85  if ( FD_ISSET( i, &_blockmain_set ) ) ostr<<" "<<i;
86  COUT<<"something pending for sockets:"
87  <<ostr.str()
88  <<" ("<<_blockmain_pending<<")"<<endl;
89 #endif
90  struct timeval zerotv;
91  zerotv.tv_sec = 0;
92  zerotv.tv_usec = 0;
93  return private_select( &zerotv );
94  }
95 }
void SocketSet::wakeup ( )

Definition at line 279 of file vsnet_socketset.cpp.

Referenced by VsnetDownload::Server::Manager::addCmdDownload(), VsnetDownload::Client::Manager::addItems(), and VsnetTCPSocket::sendbuf().

280 {
281  private_wakeup();
282 }
void SocketSet::waste_time ( long  sec,
long  usec 
)

unthreaded case

Use this function liberally in you code. If you don't have a network thread, it will check select and return. If you have a place in your code where you want to wait anyway, replace your waiting function with a call to this function.

Definition at line 290 of file vsnet_socketset.cpp.

Referenced by NetClient::loginAcctLoop(), and VsnetTCPSocket::private_nothread_conditional_write().

291 {
292  struct timeval tv;
293  tv.tv_sec = sec;
294  tv.tv_usec = usec;
295  private_select( &tv );
296 }

The documentation for this class was generated from the following files: