ReadHelper.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Scheduler_ReadHelper_
18 #define HH_SENF_Scheduler_ReadHelper_ 1
19 
20 // Custom includes
21 #include <string>
22 #include <boost/function.hpp>
23 #include <boost/intrusive_ptr.hpp>
24 #include <boost/scoped_ptr.hpp>
25 
27 #include "FdEvent.hh"
28 
29 //#include "ReadHelper.mpp"
30 //-/////////////////////////////////////////////////////////////////////////////////////////////////
31 
32 namespace senf {
33 
34 
61  template <class Handle>
62  class ReadHelper
64  {
65  public:
66  //-////////////////////////////////////////////////////////////////////////
67  // Types
68 
69  typedef boost::intrusive_ptr<ReadHelper> ptr;
70  typedef boost::function<void (ptr)> Callback;
71 
72  //-////////////////////////////////////////////////////////////////////////
74  //\{
75 
76  static ptr dispatch(Handle handle, std::string::size_type maxSize,
77  Callback callback);
78 
89  template <class Predicate>
90  static ptr dispatch(Handle handle, std::string::size_type maxSize, Predicate const & predicate,
91  Callback callback);
92 
105  //\}
106  //-////////////////////////////////////////////////////////////////////////
107 
108  Handle handle() const;
109  std::string::size_type maxSize() const;
110 
111  std::string const & data() const;
112  std::string const & tail() const;
113 
114  bool complete() const;
115  bool error() const;
116  void throw_error() const;
117 
118  void revoke();
119 
120  protected:
121 
122  private:
123  struct InternalPredicate;
124 
125  ReadHelper(Handle handle, std::string::size_type maxSize,
126  InternalPredicate * predicate, Callback cb);
127 
128  static void dispatchProcess(ptr helper, Handle handle, int event);
129  void process(Handle handle, int event);
130  void done();
131 
132  Handle handle_;
133  scheduler::FdEvent fde_;
134  std::string::size_type maxSize_;
135  boost::scoped_ptr<InternalPredicate> predicate_;
136  Callback callback_;
137 
138  std::string data_;
139  std::string tail_;
140  int errno_;
141  bool complete_;
142  };
143 
152  struct ReadUntil
153  {
154  ReadUntil(std::string const & target);
155  std::string::size_type operator()(std::string const & data);
156  std::string target;
157  };
158 
159 }
160 
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 #include "ReadHelper.cci"
163 #include "ReadHelper.ct"
164 #include "ReadHelper.cti"
165 //#include "ReadHelper.mpp"
166 #endif
167 
168 
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // comment-column: 40
177 // End:
static ptr dispatch(Handle handle, std::string::size_type maxSize, Callback callback)
Register new ReadHandler instance.
std::string target
Definition: ReadHelper.hh:156
std::string const & data() const
return data read
std::string::size_type maxSize() const
Return maximum number of bytes to be read.
bool complete() const
Check whether the read has completed successfully.
File descriptor event.
Definition: FdEvent.hh:72
void process()
Event handler main loop.
Definition: Scheduler.cc:77
void revoke()
Remove the ReadHelper from the scheduler.
bool error() const
Check for error condition.
Handle handle() const
Access the handle object.
FdDispatcher public header.
void throw_error() const
If an error occurred, throw it.
boost::intrusive_ptr< ReadHelper > ptr
Smart pointer type for this class.
Definition: ReadHelper.hh:69
std::string const & tail() const
return data read but not matched by the predicate
ReadHelper predicate matching an arbitrary string.
Definition: ReadHelper.hh:152
boost::function< void(ptr)> Callback
Callback type.
Definition: ReadHelper.hh:70
Asynchronous reading helper.
Definition: ReadHelper.hh:62