Thrill  0.1
SelectDispatcher Class Referencefinal

Detailed Description

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.

+ Inheritance diagram for SelectDispatcher:
+ Collaboration diagram for SelectDispatcher:

#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...
 
SelectDispatcheroperator= (const SelectDispatcher &)=delete
 non-copyable: delete assignment operator More...
 
SelectDispatcheroperator= (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...
 
Dispatcheroperator= (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< Watchwatch_
 

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_
 

Member Typedef Documentation

◆ Callback

type for file descriptor readiness callbacks

Definition at line 59 of file select_dispatcher.hpp.

Constructor & Destructor Documentation

◆ SelectDispatcher() [1/3]

◆ SelectDispatcher() [2/3]

SelectDispatcher ( const SelectDispatcher )
delete

non-copyable: delete copy-constructor

◆ SelectDispatcher() [3/3]

move-constructor: default

◆ ~SelectDispatcher()

~SelectDispatcher ( )
inline

Definition at line 88 of file select_dispatcher.hpp.

References SelectDispatcher::self_pipe_.

Member Function Documentation

◆ AddRead() [1/2]

void AddRead ( int  fd,
const Callback read_cb 
)
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().

◆ AddRead() [2/2]

void AddRead ( net::Connection c,
const Callback read_cb 
)
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().

◆ AddWrite()

void AddWrite ( net::Connection c,
const Callback write_cb 
)
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_.

◆ Cancel()

◆ CheckSize()

void CheckSize ( int  fd)
inline

◆ DefaultExceptionCallback()

static bool DefaultExceptionCallback ( )
inlinestaticprivate

Default exception handler.

Definition at line 203 of file select_dispatcher.hpp.

References SelectDispatcher::SelfPipeCallback().

Referenced by SelectDispatcher::DispatchOne().

◆ DispatchOne()

◆ Interrupt()

void Interrupt ( )
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().

◆ operator=() [1/2]

SelectDispatcher& operator= ( const SelectDispatcher )
delete

non-copyable: delete assignment operator

Referenced by SelectDispatcher::SelectDispatcher().

◆ operator=() [2/2]

SelectDispatcher& operator= ( SelectDispatcher &&  )
default

move-assignment operator: default

◆ SelfPipeCallback()

bool SelfPipeCallback ( )
private

◆ SetExcept()

void SetExcept ( net::Connection c,
const Callback except_cb 
)
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_.

Member Data Documentation

◆ debug

constexpr bool debug = false
staticprivate

Definition at line 53 of file select_dispatcher.hpp.

Referenced by SelectDispatcher::DispatchOne().

◆ select_

◆ self_pipe_

int self_pipe_[2]
private

◆ self_pipe_buffer_

char self_pipe_buffer_[32]
private

buffer to receive one byte signals from self-pipe

Definition at line 184 of file select_dispatcher.hpp.

Referenced by SelectDispatcher::SelfPipeCallback().

◆ self_verify_

constexpr bool self_verify_ = common::g_self_verify
staticprivate

Definition at line 55 of file select_dispatcher.hpp.

Referenced by SelectDispatcher::DispatchOne().

◆ watch_

std::vector<Watch> watch_
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().


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