Asynchronous reading helper. More...
#include <senf/Scheduler/ReadHelper.hh>
Public Types | |
typedef boost::intrusive_ptr< ReadHelper > | ptr |
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 () |
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'.
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.
typedef boost::function<void (ptr)> senf::ReadHelper< Handle >::Callback |
Callback type.
Definition at line 70 of file ReadHelper.hh.
typedef boost::intrusive_ptr<ReadHelper> senf::ReadHelper< Handle >::ptr |
Smart pointer type for this class.
Definition at line 69 of file ReadHelper.hh.
bool senf::ReadHelper< Handle >::complete | ( | ) | const |
Check whether the read has completed successfully.
std::string const& senf::ReadHelper< Handle >::data | ( | ) | const |
return data read
|
static |
Register new ReadHandler instance.
The registered Callback will be called after maxSize bytes have been read or EOF or some error is encountered.
[in] | handle | file descriptor or handle providing the Handle interface defined above. |
[in] | maxSize | maximum number of bytes to read |
[in] | 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.
[in] | handle | file descriptor or handle providing the Handle interface defined above. |
[in] | maxSize | maximum number of bytes to read |
[in] | predicate | predicate to check |
[in] | callback | callback |
bool senf::ReadHelper< Handle >::error | ( | ) | const |
Check for error condition.
Handle senf::ReadHelper< Handle >::handle | ( | ) | const |
Access the handle object.
std::string::size_type senf::ReadHelper< Handle >::maxSize | ( | ) | const |
Return maximum number of bytes to be read.
void senf::ReadHelper< Handle >::revoke | ( | ) |
Remove the ReadHelper from the scheduler.
std::string const& senf::ReadHelper< Handle >::tail | ( | ) | const |
return data read but not matched by the predicate
void senf::ReadHelper< Handle >::throw_error | ( | ) | const |
If an error occurred, throw it.