senf::ReadHelper< Handle > Class Template Reference

Asynchronous reading helper. More...

#include <senf/Scheduler/ReadHelper.hh>

Inheritance diagram for senf::ReadHelper< Handle >:

Public Types

typedef boost::intrusive_ptr< ReadHelperptr
 Smart pointer type for this class. More...
 
typedef boost::function< void(ptr)> Callback
 Callback type. More...
 
- Public Types inherited from senf::intrusive_refcount_base
typedef unsigned refcount_t
 

Public Member Functions

Handle handle () const
 Access the handle object. More...
 
std::string::size_type maxSize () const
 Return maximum number of bytes to be read. More...
 
std::string const & data () const
 return data read More...
 
std::string const & tail () const
 return data read but not matched by the predicate More...
 
bool complete () const
 Check whether the read has completed successfully. More...
 
bool error () const
 Check for error condition. More...
 
void throw_error () const
 If an error occurred, throw it. More...
 
void revoke ()
 Remove the ReadHelper from the scheduler. More...
 
- Public Member Functions inherited from senf::intrusive_refcount_base
virtual ~intrusive_refcount_base ()
 
refcount_t refcount () const
 
bool is_shared () const
 

Structors and default members

static ptr dispatch (Handle handle, std::string::size_type maxSize, Callback callback)
 Register new ReadHandler instance. More...
 
template<class Predicate >
static ptr dispatch (Handle handle, std::string::size_type maxSize, Predicate const &predicate, Callback callback)
 Register new ReadHelper instance. More...
 

Additional Inherited Members

- Protected Member Functions inherited from senf::intrusive_refcount
 intrusive_refcount ()
 
- Protected Member Functions inherited from intrusive_refcount_t< intrusive_refcount >
 intrusive_refcount_t ()
 
- Protected Member Functions inherited from senf::intrusive_refcount_base
 intrusive_refcount_base ()
 
void add_ref ()
 
bool release ()
 

Detailed Description

template<class Handle>
class senf::ReadHelper< Handle >

Asynchronous reading helper.

This class provides a simple asynchronous reading facility. This helper will register with the Scheduler and read incoming data. It will collect the data until a specific number of bytes has been read or some Predicate evaluated on the data read thus far signals end of data.

The ReadHelper accepts the same flexible file handle interfaces as the Scheduler.

The callback must take a ReadHelper::ptr argument. Using this ReadHelper instance, the callback can access the data read or retrieve state information.

The ReadHelper separates the data into two parts: data() will return the matched data, tail() will return any surplus data already read. If you don't specify a predicate, tail() will always be empty (there may however some data be left in the socket input buffer after the ReadHelper returns).

The predicate is any class instance with an operator(std::string const &). This operator is called, whenever some data has been read. If the data is not yet complete, the predicate must return std::string::npos. If the ReadHelper should stop reading more data, the predicate must return the number of bytes which are to be considered 'matched'.

Todo:

Move all not Handle dependent members to a ReadHandleBase class

Add an optional std::string const & tail argument to the constructors which takes the tail() of a previous ReadHelper instance.

Definition at line 62 of file ReadHelper.hh.

Member Typedef Documentation

◆ Callback

template<class Handle >
typedef boost::function<void (ptr)> senf::ReadHelper< Handle >::Callback

Callback type.

Definition at line 70 of file ReadHelper.hh.

◆ ptr

template<class Handle >
typedef boost::intrusive_ptr<ReadHelper> senf::ReadHelper< Handle >::ptr

Smart pointer type for this class.

Definition at line 69 of file ReadHelper.hh.

Member Function Documentation

◆ complete()

template<class Handle >
bool senf::ReadHelper< Handle >::complete ( ) const

Check whether the read has completed successfully.

◆ data()

template<class Handle >
std::string const& senf::ReadHelper< Handle >::data ( ) const

return data read

◆ dispatch() [1/2]

template<class Handle >
static ptr senf::ReadHelper< Handle >::dispatch ( Handle  handle,
std::string::size_type  maxSize,
Callback  callback 
)
static

Register new ReadHandler instance.

The registered Callback will be called after maxSize bytes have been read or EOF or some error is encountered.

Postcondition
The returned ReadHelper instance is registered with the Scheduler to handle read events.
Parameters
[in]handlefile descriptor or handle providing the Handle interface defined above.
[in]maxSizemaximum number of bytes to read
[in]callbackcallback
Returns
Smart pointer to new ReadHelper instance

◆ dispatch() [2/2]

template<class Handle >
template<class Predicate >
static ptr senf::ReadHelper< Handle >::dispatch ( Handle  handle,
std::string::size_type  maxSize,
Predicate const &  predicate,
Callback  callback 
)
static

Register new ReadHelper instance.

The registered Callback will be called after the predicate returns a Value other than std::string::npos, maxSize bytes have been read, or EOF or some error condition is encountered.

Postcondition
The returned ReadHelper instance is registered with the Scheduler to handle read events .
Parameters
[in]handlefile descriptor or handle providing the Handle interface defined above.
[in]maxSizemaximum number of bytes to read
[in]predicatepredicate to check
[in]callbackcallback
Returns
smart pointer to new ReadHelper instance

◆ error()

template<class Handle >
bool senf::ReadHelper< Handle >::error ( ) const

Check for error condition.

◆ handle()

template<class Handle >
Handle senf::ReadHelper< Handle >::handle ( ) const

Access the handle object.

◆ maxSize()

template<class Handle >
std::string::size_type senf::ReadHelper< Handle >::maxSize ( ) const

Return maximum number of bytes to be read.

◆ revoke()

template<class Handle >
void senf::ReadHelper< Handle >::revoke ( )

Remove the ReadHelper from the scheduler.

◆ tail()

template<class Handle >
std::string const& senf::ReadHelper< Handle >::tail ( ) const

return data read but not matched by the predicate

◆ throw_error()

template<class Handle >
void senf::ReadHelper< Handle >::throw_error ( ) const

If an error occurred, throw it.


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