#include <senf/Scheduler/ReadHelper.hh>
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'.
Add an optional std::string const & tail
argument to the constructors which takes the tail() of a previous ReadHelper instance.
Definition at line 69 of file ReadHelper.hh.
Classes |
|
struct | InternalPredicate |
Abstract predicate interface. More... |
|
Public Types |
|
typedef boost::intrusive_ptr < ReadHelper > |
ptr |
Smart pointer type for this class. |
|
typedef boost::function< void(ptr)> | Callback |
Callback type. |
|
Public Member Functions |
|
Handle | handle () const |
Access the handle object. |
|
std::string::size_type | maxSize () const |
Return maximum number of bytes to be read. |
|
std::string const & | data () const |
return data read |
|
std::string const & | tail () const |
return data read but not matched by the predicate |
|
bool | complete () const |
Check whether the read has completed successfully. |
|
bool | error () const |
Check for error condition. |
|
void | throw_error () const |
If an error occurred, throw it. |
|
void | revoke () |
Remove the ReadHelper from the scheduler. |
|
Structors and default members |
|
static ptr | dispatch (Handle handle, std::string::size_type maxSize, Callback callback) |
Register new ReadHandler instance. |
|
template<class Predicate > | |
static ptr | dispatch (Handle handle, std::string::size_type maxSize, Predicate const &predicate, Callback callback) |
Register new ReadHelper instance. |
typedef boost::function<void (ptr)> senf::ReadHelper< Handle >:: | ||||
Callback | ||||
Callback type.
Definition at line 77 of file ReadHelper.hh.
typedef boost::intrusive_ptr<ReadHelper> senf::ReadHelper< Handle >:: | ||||
ptr | ||||
Smart pointer type for this class.
Definition at line 76 of file ReadHelper.hh.
bool senf::ReadHelper< Handle >:: | ||||
complete | () | |||
Check whether the read has completed successfully.
Definition at line 80 of file ReadHelper.cti.
std::string const & senf::ReadHelper< Handle >:: | ||||
data | () | |||
return data read
Definition at line 66 of file ReadHelper.cti.
senf::ReadHelper< Handle >::ptr senf::ReadHelper< Handle >:: | ||||
dispatch | ( | Handle | handle, | |
std::string::size_type | maxSize, | |||
Predicate const & | predicate, | |||
Callback | callback | ) | ||
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 |
Definition at line 43 of file ReadHelper.cti.
senf::ReadHelper< Handle >::ptr senf::ReadHelper< Handle >:: | ||||
dispatch | ( | Handle | handle, | |
std::string::size_type | maxSize, | |||
Callback | callback | ) | ||
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 |
Definition at line 34 of file ReadHelper.cti.
bool senf::ReadHelper< Handle >:: | ||||
error | () | |||
Check for error condition.
Definition at line 87 of file ReadHelper.cti.
Handle senf::ReadHelper< Handle >:: | ||||
handle | () | |||
Access the handle object.
Definition at line 52 of file ReadHelper.cti.
std::string::size_type senf::ReadHelper< Handle >:: | ||||
maxSize | () | |||
Return maximum number of bytes to be read.
Definition at line 59 of file ReadHelper.cti.
void senf::ReadHelper< Handle >:: | ||||
revoke | () | |||
Remove the ReadHelper from the scheduler.
Definition at line 48 of file ReadHelper.ct.
std::string const & senf::ReadHelper< Handle >:: | ||||
tail | () | |||
return data read but not matched by the predicate
Definition at line 73 of file ReadHelper.cti.
void senf::ReadHelper< Handle >:: | ||||
throw_error | () | |||
If an error occurred, throw it.
Definition at line 94 of file ReadHelper.cti.