WriteHelper.ct
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 
14 /** \file
15  \brief WriteHelper non-inline template implementation */
16 
17 //#include "WriteHelper.ih"
18 
19 // Custom includes
20 #include <boost/bind.hpp>
21 #include <senf/Utils/Exception.hh>
22 
23 #define prefix_
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 
26 template <class Handle>
27 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
28  Callback callback)
29  : handle_(handle),
30  fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
31  handle, scheduler::FdEvent::EV_WRITE),
32  data_(data), callback_(callback), offset_(data_.begin()), errno_(0)
33 {}
34 
35 template <class Handle>
36 prefix_ std::string const & senf::WriteHelper<Handle>::data()
37  const
38 {
39  if (offset_ > data_.begin()) {
40  data_.erase(data_.begin(),offset_);
41  offset_ = data_.begin();
42  }
43  return data_;
44 }
45 
46 template <class Handle>
47 prefix_ void senf::WriteHelper<Handle>::revoke()
48 {
49  ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
50  fde_.disable();
51  fde_.action(0); // To remove the smart pointer reference to this
52 }
53 
54 template <class Handle>
55 prefix_ void senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
56  scheduler::FdEvent::Events event)
57 {
58  // since we have a 'ptr' argument, the instance cannot be deleted
59  // before this method returns
60  helper->process(handle, event);
61 }
62 
63 template <class Handle>
64 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
65  scheduler::FdEvent::Events event)
66 {
67  bool complete_ (false);
68  try {
69  if (event != scheduler::FdEvent::EV_WRITE)
70  throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
71  offset_ = handle.write(std::make_pair(offset_,data_.end()));
72  if (offset_ == data_.end()) {
73  data_.erase();
74  offset_ = data_.begin();
75  complete_ = true;
76  }
77  }
78  catch (senf::SystemException const & ex) {
79  errno_ = ex.errorNumber();
80  done();
81  return;
82  }
83  if (complete_)
84  done();
85 }
86 
87 template <class Handle>
88 prefix_ void senf::WriteHelper<Handle>::done()
89 {
90  revoke();
91  callback_(ptr(this));
92 }
93 
94 //-/////////////////////////////////////////////////////////////////////////////////////////////////
95 #undef prefix_
96 
97 
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // c-file-style: "senf"
102 // indent-tabs-mode: nil
103 // ispell-local-dictionary: "american"
104 // compile-command: "scons -u test"
105 // comment-column: 40
106 // End: