Thrill
0.1
|
SelectDispatcher is a higher level wrapper for select().
One can register Socket objects for readability and writability checks, buffered reads and writes with completion callbacks, and also timer functions.
Definition at line 51 of file select_dispatcher.hpp.
#include <select_dispatcher.hpp>
Classes | |
struct | Watch |
callback vectors per watched file descriptor More... | |
Public Types | |
using | Callback = AsyncCallback |
type for file descriptor readiness callbacks More... | |
Public Member Functions | |
SelectDispatcher () | |
constructor More... | |
SelectDispatcher (const SelectDispatcher &)=delete | |
non-copyable: delete copy-constructor More... | |
SelectDispatcher (SelectDispatcher &&)=default | |
move-constructor: default More... | |
~SelectDispatcher () | |
void | AddRead (int fd, const Callback &read_cb) |
Register a buffered read callback and a default exception callback. More... | |
void | AddRead (net::Connection &c, const Callback &read_cb) final |
Register a buffered read callback and a default exception callback. More... | |
void | AddWrite (net::Connection &c, const Callback &write_cb) final |
Register a buffered write callback and a default exception callback. More... | |
void | Cancel (net::Connection &c) final |
Cancel all callbacks on a given fd. More... | |
void | CheckSize (int fd) |
Grow table if needed. More... | |
void | DispatchOne (const std::chrono::milliseconds &timeout) final |
Run one iteration of dispatching select(). More... | |
void | Interrupt () final |
Interrupt the current select via self-pipe. More... | |
SelectDispatcher & | operator= (const SelectDispatcher &)=delete |
non-copyable: delete assignment operator More... | |
SelectDispatcher & | operator= (SelectDispatcher &&)=default |
move-assignment operator: default More... | |
void | SetExcept (net::Connection &c, const Callback &except_cb) |
Register a buffered write callback and a default exception callback. More... | |
Public Member Functions inherited from Dispatcher | |
Dispatcher ()=default | |
default constructor More... | |
Dispatcher (const Dispatcher &)=delete | |
non-copyable: delete copy-constructor More... | |
virtual | ~Dispatcher () |
virtual destructor More... | |
Dispatcher & | operator= (const Dispatcher &)=delete |
non-copyable: delete assignment operator More... | |
void | AddTimer (const std::chrono::milliseconds &timeout, const TimerCallback &cb) |
Register a relative timeout callback. More... | |
virtual void | AsyncRead (Connection &c, uint32_t, size_t size, const AsyncReadBufferCallback &done_cb) |
asynchronously read n bytes and deliver them to the callback More... | |
virtual void | AsyncRead (Connection &c, uint32_t, size_t size, data::PinnedByteBlockPtr &&block, const AsyncReadByteBlockCallback &done_cb) |
asynchronously read the full ByteBlock and deliver it to the callback More... | |
virtual void | AsyncWrite (Connection &c, uint32_t, Buffer &&buffer, const AsyncWriteCallback &done_cb=AsyncWriteCallback()) |
virtual void | AsyncWrite (Connection &c, uint32_t, data::PinnedBlock &&block, const AsyncWriteCallback &done_cb=AsyncWriteCallback()) |
void | AsyncWriteCopy (Connection &c, uint32_t seq, const void *buffer, size_t size, const AsyncWriteCallback &done_cb=AsyncWriteCallback()) |
void | AsyncWriteCopy (Connection &c, uint32_t seq, const std::string &str, const AsyncWriteCallback &done_cb=AsyncWriteCallback()) |
void | Dispatch () |
Dispatch one or more events. More... | |
void | Loop () |
Loop over Dispatch() until terminate_ flag is set. More... | |
void | Terminate () |
bool | HasAsyncWrites () const |
Check whether there are still AsyncWrite()s in the queue. More... | |
Private Member Functions | |
bool | SelfPipeCallback () |
Self-pipe callback. More... | |
Static Private Member Functions | |
static bool | DefaultExceptionCallback () |
Default exception handler. More... | |
Private Attributes | |
Select | select_ |
select() manager object More... | |
int | self_pipe_ [2] |
self-pipe to wake up select. More... | |
char | self_pipe_buffer_ [32] |
buffer to receive one byte signals from self-pipe More... | |
std::vector< Watch > | watch_ |
Static Private Attributes | |
static constexpr bool | debug = false |
static constexpr bool | self_verify_ = common::g_self_verify |
Additional Inherited Members | |
Protected Types inherited from Dispatcher | |
using | TimerPQ = std::priority_queue< Timer, std::vector< Timer, mem::GPoolAllocator< Timer > > > |
priority queue of timer callbacks More... | |
Static Protected Member Functions inherited from Dispatcher | |
static bool | ExceptionCallback (Connection &c) |
Default exception handler. More... | |
Protected Attributes inherited from Dispatcher | |
std::deque< AsyncReadBuffer, mem::GPoolAllocator< AsyncReadBuffer > > | async_read_ |
deque of asynchronous readers More... | |
std::deque< AsyncReadByteBlock, mem::GPoolAllocator< AsyncReadByteBlock > > | async_read_block_ |
deque of asynchronous readers More... | |
std::deque< AsyncWriteBuffer, mem::GPoolAllocator< AsyncWriteBuffer > > | async_write_ |
deque of asynchronous writers More... | |
std::deque< AsyncWriteBlock, mem::GPoolAllocator< AsyncWriteBlock > > | async_write_block_ |
deque of asynchronous writers More... | |
std::atomic< bool > | terminate_ { false } |
true if dispatcher needs to stop More... | |
TimerPQ | timer_pq_ |
using Callback = AsyncCallback |
type for file descriptor readiness callbacks
Definition at line 59 of file select_dispatcher.hpp.
|
inlineexplicit |
constructor
Definition at line 62 of file select_dispatcher.hpp.
References SelectDispatcher::AddRead(), LOG1, thrill::common::MakePipe(), SelectDispatcher::operator=(), SelectDispatcher::self_pipe_, SelectDispatcher::SelfPipeCallback(), and Socket::SetNonBlocking().
|
delete |
non-copyable: delete copy-constructor
|
default |
move-constructor: default
|
inline |
Definition at line 88 of file select_dispatcher.hpp.
References SelectDispatcher::self_pipe_.
|
inline |
Register a buffered read callback and a default exception callback.
Definition at line 102 of file select_dispatcher.hpp.
References SelectDispatcher::CheckSize(), SelectDispatcher::select_, Select::SetException(), Select::SetRead(), and SelectDispatcher::watch_.
Referenced by SelectDispatcher::AddRead(), and SelectDispatcher::SelectDispatcher().
|
inlinefinalvirtual |
Register a buffered read callback and a default exception callback.
Implements Dispatcher.
Definition at line 113 of file select_dispatcher.hpp.
References SelectDispatcher::AddRead(), Socket::fd(), and Connection::GetSocket().
|
inlinefinalvirtual |
Register a buffered write callback and a default exception callback.
Implements Dispatcher.
Definition at line 121 of file select_dispatcher.hpp.
References SelectDispatcher::CheckSize(), Socket::fd(), Connection::GetSocket(), SelectDispatcher::select_, Select::SetException(), Select::SetWrite(), and SelectDispatcher::watch_.
|
inlinefinalvirtual |
Cancel all callbacks on a given fd.
Implements Dispatcher.
Definition at line 148 of file select_dispatcher.hpp.
References SelectDispatcher::Watch::active, SelectDispatcher::CheckSize(), Select::ClearException(), Select::ClearRead(), Select::ClearWrite(), SelectDispatcher::DispatchOne(), SelectDispatcher::Watch::except_cb, Socket::fd(), Connection::GetSocket(), SelectDispatcher::Interrupt(), LOG, SelectDispatcher::Watch::read_cb, SelectDispatcher::select_, SelectDispatcher::watch_, and SelectDispatcher::Watch::write_cb.
|
inline |
Grow table if needed.
Definition at line 94 of file select_dispatcher.hpp.
References SelectDispatcher::watch_.
Referenced by SelectDispatcher::AddRead(), SelectDispatcher::AddWrite(), SelectDispatcher::Cancel(), and SelectDispatcher::SetExcept().
|
inlinestaticprivate |
Default exception handler.
Definition at line 203 of file select_dispatcher.hpp.
References SelectDispatcher::SelfPipeCallback().
Referenced by SelectDispatcher::DispatchOne().
|
finalvirtual |
Run one iteration of dispatching select().
Implements Dispatcher.
Definition at line 22 of file select_dispatcher.cpp.
References SelectDispatcher::Watch::active, Select::ClearException(), Select::ClearRead(), Select::ClearWrite(), SelectDispatcher::debug, SelectDispatcher::DefaultExceptionCallback(), SelectDispatcher::Watch::except_cb, Select::InException(), Select::InRead(), Select::InWrite(), LOG, SelectDispatcher::Watch::read_cb, SelectDispatcher::select_, Select::select_timeout(), SelectDispatcher::self_verify_, SelectDispatcher::watch_, and SelectDispatcher::Watch::write_cb.
Referenced by SelectDispatcher::Cancel().
|
finalvirtual |
Interrupt the current select via self-pipe.
Implements Dispatcher.
Definition at line 164 of file select_dispatcher.cpp.
References die_unless, LOG1, and SelectDispatcher::self_pipe_.
Referenced by SelectDispatcher::Cancel().
|
delete |
non-copyable: delete assignment operator
Referenced by SelectDispatcher::SelectDispatcher().
|
default |
move-assignment operator: default
|
private |
Self-pipe callback.
Definition at line 182 of file select_dispatcher.cpp.
References SelectDispatcher::self_pipe_, and SelectDispatcher::self_pipe_buffer_.
Referenced by SelectDispatcher::DefaultExceptionCallback(), and SelectDispatcher::SelectDispatcher().
|
inline |
Register a buffered write callback and a default exception callback.
Definition at line 135 of file select_dispatcher.hpp.
References SelectDispatcher::CheckSize(), Socket::fd(), Connection::GetSocket(), SelectDispatcher::select_, Select::SetException(), and SelectDispatcher::watch_.
|
staticprivate |
Definition at line 53 of file select_dispatcher.hpp.
Referenced by SelectDispatcher::DispatchOne().
|
private |
select() manager object
Definition at line 178 of file select_dispatcher.hpp.
Referenced by SelectDispatcher::AddRead(), SelectDispatcher::AddWrite(), SelectDispatcher::Cancel(), SelectDispatcher::DispatchOne(), and SelectDispatcher::SetExcept().
|
private |
self-pipe to wake up select.
Definition at line 181 of file select_dispatcher.hpp.
Referenced by SelectDispatcher::Interrupt(), SelectDispatcher::SelectDispatcher(), SelectDispatcher::SelfPipeCallback(), and SelectDispatcher::~SelectDispatcher().
|
private |
buffer to receive one byte signals from self-pipe
Definition at line 184 of file select_dispatcher.hpp.
Referenced by SelectDispatcher::SelfPipeCallback().
|
staticprivate |
Definition at line 55 of file select_dispatcher.hpp.
Referenced by SelectDispatcher::DispatchOne().
|
private |
handlers for all registered file descriptors. the fd integer range should be small enough, otherwise a more complicated data structure is needed.
Definition at line 200 of file select_dispatcher.hpp.
Referenced by SelectDispatcher::AddRead(), SelectDispatcher::AddWrite(), SelectDispatcher::Cancel(), SelectDispatcher::CheckSize(), SelectDispatcher::DispatchOne(), and SelectDispatcher::SetExcept().