xdrpp
RFC4506 XDR compiler and message library
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Attributes | List of all members
xdr::pollset Class Reference

Structure to poll for a set of file descriptors and timeouts. More...

#include <pollset.h>

Inheritance diagram for xdr::pollset:
Inheritance graph
[legend]

Classes

class  Timeout
 Abstract class used to represent a pending timeout. More...
 

Public Types

enum  op_t {
  Read = kReadFlag, Write = kWriteFlag, ReadWrite = kReadFlag | kWriteFlag, ReadOnce = kReadFlag | kOnceFlag,
  WriteOnce = kWriteFlag | kOnceFlag
}
 
using cb_t = std::function< void()>
 

Public Member Functions

 pollset (const pollset &)=delete
 
void poll (int timeout=-1)
 Go through one round of checking all file descriptors. More...
 
virtual bool pending () const
 Returns false if no file descriptor callbacks are registered and no timeouts or asynchronous events are pending. More...
 
void run ()
 Continously poll and only return on exception or when there is no more work to do. More...
 
template<typename CB >
void fd_cb (sock_t s, op_t op, CB &&cb)
 Set a read or write callback on a particular file descriptor. More...
 
void fd_cb (sock_t s, op_t op, std::nullptr_t=nullptr)
 Remove a callback on a file descriptor. More...
 
template<typename CB >
Timeout timeout (std::int64_t ms, CB &&cb)
 Set a callback to run a certain number of milliseconds from now. More...
 
template<typename CB >
Timeout timeout_at (std::int64_t ms, CB &&cb)
 Set a callback to run at a specific time (as returned by PollSet::now_ms()). More...
 
void timeout_cancel (Timeout &t)
 Cancel a pending timeout. More...
 
std::int64_t timeout_time (Timeout t) const
 Returns the absolute time (in milliseconds) at which a timeout will run. More...
 
void timeout_reschedule_at (Timeout &t, std::int64_t ms)
 Reschedule a timeout to run at a specific time. More...
 
void timeout_reschedule (Timeout &t, std::int64_t ms)
 Reschedule a timeout some number of milliseconds in the future.
 

Static Public Member Functions

static std::int64_t now_ms ()
 Number of milliseconds since an arbitrary but fixed time, used as the basis of all timeouts. More...
 
static Timeout timeout_null ()
 An invalid timeout, useful for initializing PollSet::Timeout values before a timeout has been scheduled. More...
 

Protected Member Functions

std::size_t num_cbs () const
 Number of registered file decriptor and timeout callbacks.
 

Static Protected Attributes

static constexpr int kReadFlag = 0x1
 
static constexpr int kWriteFlag = 0x2
 
static constexpr int kOnceFlag = 0x4
 

Detailed Description

Structure to poll for a set of file descriptors and timeouts.

Definition at line 23 of file pollset.h.

Member Enumeration Documentation

◆ op_t

Enumerator
Read 

Specify interest in read-ready condition.

Write 

Specify interest in write-ready condition.

ReadWrite 

Valid only when removing callbacks.

ReadOnce 

Like Read, but the callback only executes once.

WriteOnce 

Like Write, but the callback only executes once.

Definition at line 32 of file pollset.h.

Member Function Documentation

◆ poll()

void xdr::pollset::poll ( int  timeout = -1)

Go through one round of checking all file descriptors.

  • timeout is a timeout in milliseconds (or -1 to wait forever).

Typical usage:

PollSet ps;
// ... register some callbacks or asynchronous events ...
while(ps.pending())
ps.poll();

Definition at line 217 of file pollset.cc.

◆ pending()

virtual bool xdr::pollset::pending ( ) const
inlinevirtual

Returns false if no file descriptor callbacks are registered and no timeouts or asynchronous events are pending.

If it returns false, then PollSet::poll will pause forever in the absence of a signal or a call to PollSet::inject_cb in a different thread.

Reimplemented in xdr::pollset_plus.

Definition at line 93 of file pollset.h.

◆ run()

void xdr::pollset::run ( )
inline

Continously poll and only return on exception or when there is no more work to do.

Definition at line 97 of file pollset.h.

◆ fd_cb() [1/2]

template<typename CB >
void xdr::pollset::fd_cb ( sock_t  s,
op_t  op,
CB &&  cb 
)
inline

Set a read or write callback on a particular file descriptor.

  • fd is the file descriptor.
  • op specifies the condition on which to invoke the callback. Only one Read and one Write callback are permitted per file descriptor. E.g., calling set_cb with ReadOnce overwrites a previous Read callback on the same file descriptor. The value ReadWrite is illegal when adding a callback (you must set Read and Write callbacks separately).
  • cb is the callback, which must be convertible to PollSet::cb_t.

Definition at line 108 of file pollset.h.

◆ fd_cb() [2/2]

void xdr::pollset::fd_cb ( sock_t  s,
op_t  op,
std::nullptr_t  = nullptr 
)

Remove a callback on a file descriptor.

If op is ReadWrite, removes both read and write callbacks on the descriptor.

Definition at line 170 of file pollset.cc.

◆ now_ms()

std::int64_t xdr::pollset::now_ms ( )
static

Number of milliseconds since an arbitrary but fixed time, used as the basis of all timeouts.

Time zero is std::chrono::steady_clock's epoch, which in some implementations is the time a machine was booted.

Definition at line 409 of file pollset.cc.

◆ timeout()

template<typename CB >
Timeout xdr::pollset::timeout ( std::int64_t  ms,
CB &&  cb 
)
inline

Set a callback to run a certain number of milliseconds from now.

  • ms is the delay in milliseconds before running the callback.
  • cb must be convertible to PollSet::cb_t.
    Returns
    an object on which you can call the method PollSet::timeout_cancel to cancel the timeout.

Definition at line 148 of file pollset.h.

◆ timeout_at()

template<typename CB >
Timeout xdr::pollset::timeout_at ( std::int64_t  ms,
CB &&  cb 
)
inline

Set a callback to run at a specific time (as returned by PollSet::now_ms()).

Definition at line 153 of file pollset.h.

◆ timeout_null()

pollset::Timeout xdr::pollset::timeout_null ( )
static

An invalid timeout, useful for initializing PollSet::Timeout values before a timeout has been scheduled.

Definition at line 21 of file pollset.cc.

◆ timeout_cancel()

void xdr::pollset::timeout_cancel ( Timeout t)

Cancel a pending timeout.

Sets the PollSet::Timeout argument t to PollSet::timeout_null(), but obviously does not not affect other copies of t that will now be invalid.

Definition at line 417 of file pollset.cc.

◆ timeout_time()

std::int64_t xdr::pollset::timeout_time ( Timeout  t) const
inline

Returns the absolute time (in milliseconds) at which a timeout will run.

Definition at line 168 of file pollset.h.

◆ timeout_reschedule_at()

void xdr::pollset::timeout_reschedule_at ( Timeout t,
std::int64_t  ms 
)

Reschedule a timeout to run at a specific time.

Updates the argument t, but invalidates any other copies of t.

Definition at line 427 of file pollset.cc.


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