xdrpp
RFC4506 XDR compiler and message library
|
Structure to poll for a set of file descriptors and timeouts. More...
#include <pollset.h>
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 |
Structure to poll for a set of file descriptors and timeouts.
enum xdr::pollset::op_t |
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:
Definition at line 217 of file pollset.cc.
|
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.
|
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. 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.
|
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.
|
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.
|
inline |
|
static |
An invalid timeout, useful for initializing PollSet::Timeout values before a timeout has been scheduled.
Definition at line 21 of file pollset.cc.
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.
|
inline |
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.