Thrill  0.1
Dispatcher Class Referenceabstract

Detailed Description

Dispatcher is a high level wrapper for asynchronous callback processing.

. One can register Connection objects for readability and writability checks, buffered reads and writes with completion callbacks, and also timer functions.

Definition at line 510 of file dispatcher.hpp.

+ Inheritance diagram for Dispatcher:

#include <dispatcher.hpp>

Classes

struct  Timer
 struct for timer callbacks More...
 

Public Member Functions

 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...
 
Timeout Callbacks
void AddTimer (const std::chrono::milliseconds &timeout, const TimerCallback &cb)
 Register a relative timeout callback. More...
 
Connection Callbacks
virtual void AddRead (Connection &c, const AsyncCallback &read_cb)=0
 Register a buffered read callback and a default exception callback. More...
 
virtual void AddWrite (Connection &c, const AsyncCallback &write_cb)=0
 Register a buffered write callback and a default exception callback. More...
 
virtual void Cancel (Connection &c)=0
 Cancel all callbacks on a given connection. More...
 
Asynchronous Data Reader/Writer Callbacks
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())
 
Dispatch
void Dispatch ()
 Dispatch one or more events. More...
 
void Loop ()
 Loop over Dispatch() until terminate_ flag is set. More...
 
virtual void Interrupt ()=0
 Interrupt current dispatch. More...
 
void Terminate ()
 
bool HasAsyncWrites () const
 Check whether there are still AsyncWrite()s in the queue. More...
 

Protected Types

using TimerPQ = std::priority_queue< Timer, std::vector< Timer, mem::GPoolAllocator< Timer > > >
 priority queue of timer callbacks More...
 

Protected Member Functions

virtual void DispatchOne (const std::chrono::milliseconds &timeout)=0
 

Static Protected Member Functions

static bool ExceptionCallback (Connection &c)
 Default exception handler. More...
 

Protected Attributes

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_
 

Private Types

using milliseconds = std::chrono::milliseconds
 import into class namespace More...
 
using steady_clock = std::chrono::steady_clock
 import into class namespace More...
 

Static Private Attributes

static constexpr bool debug = false
 

Member Typedef Documentation

◆ milliseconds

using milliseconds = std::chrono::milliseconds
private

import into class namespace

Definition at line 519 of file dispatcher.hpp.

◆ steady_clock

using steady_clock = std::chrono::steady_clock
private

import into class namespace

Definition at line 516 of file dispatcher.hpp.

◆ TimerPQ

using TimerPQ = std::priority_queue< Timer, std::vector<Timer, mem::GPoolAllocator<Timer> > >
protected

priority queue of timer callbacks

Definition at line 784 of file dispatcher.hpp.

Constructor & Destructor Documentation

◆ Dispatcher() [1/2]

Dispatcher ( )
default

default constructor

◆ Dispatcher() [2/2]

Dispatcher ( const Dispatcher )
delete

non-copyable: delete copy-constructor

◆ ~Dispatcher()

virtual ~Dispatcher ( )
inlinevirtual

virtual destructor

Reimplemented in Dispatcher, and Dispatcher.

Definition at line 534 of file dispatcher.hpp.

Member Function Documentation

◆ AddRead()

virtual void AddRead ( Connection c,
const AsyncCallback read_cb 
)
pure virtual

Register a buffered read callback and a default exception callback.

Implemented in Dispatcher, SelectDispatcher, and Dispatcher.

◆ AddTimer()

void AddTimer ( const std::chrono::milliseconds &  timeout,
const TimerCallback cb 
)
inline

Register a relative timeout callback.

Definition at line 540 of file dispatcher.hpp.

◆ AddWrite()

virtual void AddWrite ( Connection c,
const AsyncCallback write_cb 
)
pure virtual

Register a buffered write callback and a default exception callback.

Implemented in Dispatcher, SelectDispatcher, and Dispatcher.

◆ AsyncRead() [1/2]

virtual void AsyncRead ( Connection c,
uint32_t  ,
size_t  size,
const AsyncReadBufferCallback done_cb 
)
inlinevirtual

asynchronously read n bytes and deliver them to the callback

Reimplemented in Dispatcher.

Definition at line 567 of file dispatcher.hpp.

References Connection::IsValid(), and LOG.

◆ AsyncRead() [2/2]

virtual void AsyncRead ( Connection c,
uint32_t  ,
size_t  size,
data::PinnedByteBlockPtr &&  block,
const AsyncReadByteBlockCallback done_cb 
)
inlinevirtual

asynchronously read the full ByteBlock and deliver it to the callback

Reimplemented in Dispatcher.

Definition at line 587 of file dispatcher.hpp.

References Connection::IsValid(), and LOG.

◆ AsyncWrite() [1/2]

virtual void AsyncWrite ( Connection c,
uint32_t  ,
Buffer &&  buffer,
const AsyncWriteCallback done_cb = AsyncWriteCallback() 
)
inlinevirtual

asynchronously write buffer and callback when delivered. The buffer is MOVED into the async writer.

Reimplemented in Dispatcher.

Definition at line 609 of file dispatcher.hpp.

References AsyncReadBuffer::buffer(), Connection::IsValid(), and Buffer::size().

◆ AsyncWrite() [2/2]

virtual void AsyncWrite ( Connection c,
uint32_t  ,
data::PinnedBlock &&  block,
const AsyncWriteCallback done_cb = AsyncWriteCallback() 
)
inlinevirtual

asynchronously write buffer and callback when delivered. The buffer is MOVED into the async writer.

Reimplemented in Dispatcher.

Definition at line 630 of file dispatcher.hpp.

References Connection::IsValid().

◆ AsyncWriteCopy() [1/2]

void AsyncWriteCopy ( Connection c,
uint32_t  seq,
const void *  buffer,
size_t  size,
const AsyncWriteCallback done_cb = AsyncWriteCallback() 
)
inline

asynchronously write buffer and callback when delivered. COPIES the data into a Buffer!

Definition at line 651 of file dispatcher.hpp.

◆ AsyncWriteCopy() [2/2]

void AsyncWriteCopy ( Connection c,
uint32_t  seq,
const std::string &  str,
const AsyncWriteCallback done_cb = AsyncWriteCallback() 
)
inline

asynchronously write buffer and callback when delivered. COPIES the data into a Buffer!

Definition at line 659 of file dispatcher.hpp.

◆ Cancel()

virtual void Cancel ( Connection c)
pure virtual

Cancel all callbacks on a given connection.

Implemented in Dispatcher, SelectDispatcher, and Dispatcher.

◆ Dispatch()

void Dispatch ( )
inline

Dispatch one or more events.

Definition at line 671 of file dispatcher.hpp.

References Dispatcher::Timer::cb, LOG, Dispatcher::Timer::next_timeout, sLOG, and Dispatcher::Timer::timeout.

◆ DispatchOne()

virtual void DispatchOne ( const std::chrono::milliseconds &  timeout)
protectedpure virtual

Implemented in Dispatcher, Dispatcher, and SelectDispatcher.

◆ ExceptionCallback()

static bool ExceptionCallback ( Connection c)
inlinestaticprotected

Default exception handler.

Definition at line 749 of file dispatcher.hpp.

References Connection::ToString().

◆ HasAsyncWrites()

bool HasAsyncWrites ( ) const
inline

Check whether there are still AsyncWrite()s in the queue.

Definition at line 739 of file dispatcher.hpp.

◆ Interrupt()

virtual void Interrupt ( )
pure virtual

Interrupt current dispatch.

Implemented in Dispatcher, Dispatcher, and SelectDispatcher.

◆ Loop()

void Loop ( )
inline

Loop over Dispatch() until terminate_ flag is set.

Definition at line 722 of file dispatcher.hpp.

◆ operator=()

Dispatcher& operator= ( const Dispatcher )
delete

non-copyable: delete assignment operator

◆ Terminate()

void Terminate ( )
inline

Causes the dispatcher to break out after the next timeout occurred Does not interrupt the currently running read/write operation, but breaks after the operation finished or timed out.

Definition at line 734 of file dispatcher.hpp.

Member Data Documentation

◆ async_read_

std::deque<AsyncReadBuffer, mem::GPoolAllocator<AsyncReadBuffer> > async_read_
protected

deque of asynchronous readers

Definition at line 794 of file dispatcher.hpp.

◆ async_read_block_

std::deque<AsyncReadByteBlock, mem::GPoolAllocator<AsyncReadByteBlock> > async_read_block_
protected

deque of asynchronous readers

Definition at line 802 of file dispatcher.hpp.

◆ async_write_

std::deque<AsyncWriteBuffer, mem::GPoolAllocator<AsyncWriteBuffer> > async_write_
protected

deque of asynchronous writers

Definition at line 798 of file dispatcher.hpp.

◆ async_write_block_

std::deque<AsyncWriteBlock, mem::GPoolAllocator<AsyncWriteBlock> > async_write_block_
protected

deque of asynchronous writers

Definition at line 806 of file dispatcher.hpp.

◆ debug

constexpr bool debug = false
staticprivate

Definition at line 512 of file dispatcher.hpp.

◆ terminate_

std::atomic<bool> terminate_ { false }
protected

true if dispatcher needs to stop

Definition at line 757 of file dispatcher.hpp.

◆ timer_pq_

TimerPQ timer_pq_
protected

priority queue of timer callbacks, obviously kept in timeout order. Currently not addressable.

Definition at line 788 of file dispatcher.hpp.


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