MPD  0.20.6
Data Structures | Public Member Functions | Static Public Attributes | Protected Member Functions | Friends
MultiSocketMonitor Class Referenceabstract

Similar to SocketMonitor, but monitors multiple sockets. More...

#include <MultiSocketMonitor.hxx>

Inheritance diagram for MultiSocketMonitor:
[legend]
Collaboration diagram for MultiSocketMonitor:
[legend]

Public Member Functions

 MultiSocketMonitor (EventLoop &_loop)
 
void Reset ()
 Clear the socket list and disable all EventLoop registrations. More...
 
void InvalidateSockets ()
 Invalidate the socket list. More...
 
void AddSocket (int fd, unsigned events)
 Add one socket to the list of monitored sockets. More...
 
void ClearSocketList ()
 Remove all sockets. More...
 
template<typename E >
void UpdateSocketList (E &&e)
 Update the known sockets by invoking the given function for each one; its return value is the events bit mask. More...
 
void ReplaceSocketList (pollfd *pfds, unsigned n)
 Replace the socket list with the given file descriptors. More...
 

Static Public Attributes

static constexpr unsigned READ = SocketMonitor::READ
 
static constexpr unsigned WRITE = SocketMonitor::WRITE
 
static constexpr unsigned ERROR = SocketMonitor::ERROR
 
static constexpr unsigned HANGUP = SocketMonitor::HANGUP
 

Protected Member Functions

virtual std::chrono::steady_clock::duration PrepareSockets ()=0
 Override this method and update the socket registrations. More...
 
virtual void DispatchSockets ()=0
 At least one socket is ready or the timeout has expired. More...
 

Friends

class SingleFD
 

Detailed Description

Similar to SocketMonitor, but monitors multiple sockets.

To use it, implement the methods PrepareSockets() and DispatchSockets(). In PrepareSockets(), use UpdateSocketList() and AddSocket(). DispatchSockets() will be called if at least one socket is ready.

Definition at line 54 of file MultiSocketMonitor.hxx.

Constructor & Destructor Documentation

MultiSocketMonitor::MultiSocketMonitor ( EventLoop _loop)

Member Function Documentation

void MultiSocketMonitor::AddSocket ( int  fd,
unsigned  events 
)
inline

Add one socket to the list of monitored sockets.

May only be called from PrepareSockets().

Definition at line 156 of file MultiSocketMonitor.hxx.

void MultiSocketMonitor::ClearSocketList ( )

Remove all sockets.

May only be called from PrepareSockets().

virtual void MultiSocketMonitor::DispatchSockets ( )
protectedpure virtual

At least one socket is ready or the timeout has expired.

This method should be used to perform I/O.

void MultiSocketMonitor::InvalidateSockets ( )
inline

Invalidate the socket list.

A call to PrepareSockets() is scheduled which will then update the list.

Definition at line 146 of file MultiSocketMonitor.hxx.

virtual std::chrono::steady_clock::duration MultiSocketMonitor::PrepareSockets ( )
protectedpure virtual

Override this method and update the socket registrations.

To do that, call AddSocket(), ClearSocketList(), UpdateSocketList() and ReplaceSocketList().

Returns
timeout or a negative value for no timeout
void MultiSocketMonitor::ReplaceSocketList ( pollfd *  pfds,
unsigned  n 
)

Replace the socket list with the given file descriptors.

The given pollfd array will be modified by this method.

May only be called from PrepareSockets().

void MultiSocketMonitor::Reset ( )

Clear the socket list and disable all EventLoop registrations.

Run this in the EventLoop thread before destroying this object.

Later, this object can be reused and reactivated by calling InvalidateSockets().

Note that this class doesn't have a destructor which calls this method, because this would be racy and thus pointless: at the time ~MultiSocketMonitor() is called, our virtual methods have been morphed to be pure again, and in the meantime the EventLoop thread could invoke those pure methods.

template<typename E >
void MultiSocketMonitor::UpdateSocketList ( E &&  e)
inline

Update the known sockets by invoking the given function for each one; its return value is the events bit mask.

A return value of 0 means the socket will be removed from the list.

May only be called from PrepareSockets().

Definition at line 176 of file MultiSocketMonitor.hxx.

Friends And Related Function Documentation

friend class SingleFD
friend

Definition at line 97 of file MultiSocketMonitor.hxx.

Field Documentation

constexpr unsigned MultiSocketMonitor::ERROR = SocketMonitor::ERROR
static

Definition at line 118 of file MultiSocketMonitor.hxx.

constexpr unsigned MultiSocketMonitor::HANGUP = SocketMonitor::HANGUP
static

Definition at line 119 of file MultiSocketMonitor.hxx.

constexpr unsigned MultiSocketMonitor::READ = SocketMonitor::READ
static

Definition at line 116 of file MultiSocketMonitor.hxx.

constexpr unsigned MultiSocketMonitor::WRITE = SocketMonitor::WRITE
static

Definition at line 117 of file MultiSocketMonitor.hxx.


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