38 G_DEBUG(
"GNet::EventHandler::readEvent: no override" ) ;
43 G_DEBUG(
"GNet::EventHandler::writeEvent: no override" ) ;
57 bool operator()(
const std::pair<GNet::Descriptor,GNet::EventHandler*> & p1 ,
const std::pair<GNet::Descriptor,GNet::EventHandler*> & p2 )
59 return p1.first < p2.first ;
73 return contains(m_list,fd) || (m_lock && contains(m_pending_list,fd)) ;
78 List::const_iterator p = std::lower_bound( list.begin() , list.end() , List::value_type(fd,
nullptr) , fdless() ) ;
79 return p != list.end() && (*p).first == fd && (*p).second != nullptr ;
85 EventHandler * p2 = m_lock ? find( m_pending_list , fd ) : nullptr ;
91 List::const_iterator p = std::lower_bound( list.begin() , list.end() , List::value_type(fd,
nullptr) , fdless() ) ;
92 return ( p != list.end() && (*p).first == fd ) ? (*p).second :
nullptr ;
97 G_ASSERT( fd.
valid() && handler != nullptr ) ;
if( !fd.
valid() || handler == nullptr )
return ;
98 G_DEBUG(
"GNet::EventHandlerList::add: " << m_type <<
"-list: " <<
"adding " << fd ) ;
100 add( m_lock?m_pending_list:m_list , fd , handler ) ;
105 typedef std::pair<List::iterator,List::iterator> Range ;
106 Range range = std::equal_range( list.begin() , list.end() , List::value_type(fd,
nullptr) , fdless() ) ;
107 if( range.first == range.second )
108 list.insert( range.second , List::value_type(fd,handler) ) ;
110 (*range.first).second = handler ;
115 G_DEBUG(
"GNet::EventHandlerList::remove: " << m_type <<
"-list: " <<
"removing " << fd ) ;
118 if( disable(m_list,fd) ) m_has_garbage = true ;
119 disable( m_pending_list , fd ) ;
123 remove( m_list , fd ) ;
127 bool GNet::EventHandlerList::disable( List & list ,
Descriptor fd )
129 typedef std::pair<List::iterator,List::iterator> Range ;
130 Range range = std::equal_range( list.begin() , list.end() , List::value_type(fd,
nullptr) , fdless() ) ;
131 const bool found = range.first != range.second ;
133 (*range.first).second =
nullptr ;
139 typedef std::pair<List::iterator,List::iterator> Range ;
140 Range range = std::equal_range( list.begin() , list.end() , List::value_type(fd,
nullptr) , fdless() ) ;
141 const bool found = range.first != range.second ;
143 list.erase( range.first ) ;
154 G_ASSERT( m_lock != 0U ) ;
164 void GNet::EventHandlerList::commit()
166 const List::iterator end = m_pending_list.end() ;
167 for( List::iterator p = m_pending_list.begin() ; p != end ; ++p )
169 if( (*p).second !=
nullptr )
170 add( m_list , (*p).first , (*p).second ) ;
172 m_pending_list.clear() ;
179 const List::iterator end = m_list.end() ;
180 for( List::iterator p = m_list.begin() ; p != end ; )
182 if( (*p).second ==
nullptr )
183 p = m_list.erase( p ) ;
187 m_has_garbage = false ;
void collectGarbage()
Collects garbage resulting from remove()s.
virtual void readEvent()
Called for a read event.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
void lock()
To be called at the start of an begin()/end() iteration if the list might change during the iteration...
virtual void writeEvent()
Called for a write event.
bool valid() const
Returns true if the descriptor is valid.
bool contains(Descriptor fd) const
Returns true if the list contains the given file-descriptor.
A base class for classes that handle asynchronous events from the event loop.
void add(Descriptor fd, EventHandler *handler)
Adds a file-descriptor/handler pair to the list.
virtual ~EventExceptionHandler()
Destructor.
void unlock()
Called at the end of a begin()/end() iteration to match a call to lock().
void remove(Descriptor fd)
Removes a file-descriptor from the list.
A general-purpose exception class derived from std::exception and containing a std::string.
EventHandlerList(const std::string &type)
Constructor.
EventHandler * find(Descriptor fd)
Finds the handler associated with the given file descriptor.
virtual ~EventHandler()
Destructor.
virtual void exceptionEvent()
Called for a socket-exception event.