SocketSource.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 SocketSource non-inline template implementation */
16 
17 //#include "SocketSource.ih"
18 
19 // Custom includes
20 
21 #define prefix_
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::ppi::DgramReader<Packet,MaxSize>
26 
27 template <class Packet, unsigned MaxSize>
28 prefix_ Packet senf::ppi::DgramReader<Packet,MaxSize>::operator()(Handle & handle)
29 {
30  Packet packet (Packet::create(senf::noinit));
31  handle.read(packet.data(), MaxSize);
32 #ifdef SENF_PPI_READ_TIMESTAMP
33  packet.template annotation<senf::ppi::ReadTimestamp>().value = senf::ClockService::now();
34 #endif
35  return packet;
36 }
37 
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // senf::ppi::module::ActiveSocketSource<Reader>
40 
41 template <class Reader>
42 prefix_ senf::ppi::module::ActiveSocketSource<Reader>::ActiveSocketSource(unsigned max_burst)
43  : maxBurst_(max_burst), burst_(0)
44 {
45  registerEvent( event_, &ActiveSocketSource::read );
46  route(event_, output);
47 }
48 
49 template <class Reader>
50 prefix_ senf::ppi::module::ActiveSocketSource<Reader>::ActiveSocketSource(Reader reader, unsigned max_burst)
51  : reader_(reader), maxBurst_(maxBurst), burst_(0)
52 {
53  registerEvent( event_, &ActiveSocketSource::read );
54  route(event_, output);
55 }
56 
57 template <class Reader>
58 prefix_ senf::ppi::module::ActiveSocketSource<Reader>::ActiveSocketSource(Handle const & handle, unsigned max_burst)
59  : handle_(handle), event_(handle_, IOEvent::Read), maxBurst_(max_burst), burst_(0)
60 {
61  registerEvent( event_, &ActiveSocketSource::read );
62  route(event_, output);
63 }
64 
65 template <class Reader>
66 prefix_ senf::ppi::module::ActiveSocketSource<Reader>::ActiveSocketSource(Handle const & handle,
67  Reader reader, unsigned max_burst)
68  : handle_(handle), event_(handle_, IOEvent::Read), reader_(reader), maxBurst_(max_burst), burst_(0)
69 {
70  registerEvent( event_, &ActiveSocketSource::read );
71  route(event_, output);
72 }
73 
74 template <class Reader>
75 prefix_ void senf::ppi::module::ActiveSocketSource<Reader>::maxBurst(unsigned max_burst)
76 {
77  maxBurst_ = max_burst;
78 }
79 
80 template <class Reader>
81 prefix_ unsigned senf::ppi::module::ActiveSocketSource<Reader>::maxBurst()
82  const
83 {
84  return maxBurst_;
85 }
86 
87 template <class Reader>
88 prefix_ void senf::ppi::module::ActiveSocketSource<Reader>::read()
89 {
90  flush_pending_ = false;
91  for (burst_ = 1; handle_ and !flush_pending_ and (burst_ <= maxBurst_); ++burst_) {
92  typename Reader::PacketType p (reader_(handle_));
93  if (SENF_LIKELY(p and (p.data().size() > 0))) {
94  rxStats_.received++;
95  output(p);
96  }
97  else {
98  if (burst_ == 1)
99  rxStats_.noop++;
100  break;
101  }
102  }
103 
104  burst_ = 0;
105  if (flush_pending_) {
106  flush();
107  }
108 }
109 
110 template <class Reader>
111 prefix_ void senf::ppi::module::ActiveSocketSource<Reader>::flush()
112 {
113  if (!handle_)
114  return;
115 
116  if (burst_ > 0) {
117  flush_pending_ = true;
118  return;
119  }
120 
121  try {
122  while (handle_ && handle_.readable() && reader_(handle_));
123  }
124  catch (...) {};
125 }
126 
127 
128 //-/////////////////////////////////////////////////////////////////////////////////////////////////
129 #undef prefix_
130 
131 
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // comment-column: 40
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // End: