ClientSocketHandle.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 ClientSocketHandle non-inline template implementation
16  */
17 
18 #include "ClientSocketHandle.ih"
19 
20 // Custom includes
21 #include <algorithm>
22 #include <boost/utility/value_init.hpp>
23 #include <senf/Utils/Buffer.hh>
24 
25 #define prefix_
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
30 
31 template <class Handle, class ForwardWritableRange, bool IsContiguous>
32 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
33 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
34 read(Handle & handle, ForwardWritableRange & range)
35 {
36  typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
37  SENF_SCOPED_BUFFER(char, buffer, nread);
38  return std::copy(buffer, handle.read(buffer,buffer+nread), boost::begin(range));
39 }
40 
41 template <class Handle, class ForwardWritableRange, bool IsContiguous>
42 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
43 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
44 readfrom(Handle & handle, ForwardWritableRange & range, typename Handle::Address & addr)
45 {
46  typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
47  SENF_SCOPED_BUFFER(char, buffer, nread);
48  return std::copy(buffer, handle.readfrom(buffer,buffer+nread,addr), boost::begin(range));
49 }
50 
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>
53 
54 template <class Handle, class ForwardReadableRange, bool IsContiguous>
55 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
56 senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>::
57 write(Handle & handle, ForwardReadableRange & range)
58 {
59  typename boost::range_size<ForwardReadableRange>::type nwrite (boost::size(range));
60  typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
61  SENF_SCOPED_BUFFER(char, buffer, nwrite);
62  std::copy(i, boost::end(range), buffer);
63  std::advance(i, handle.write(std::make_pair(buffer, buffer+nwrite)) - buffer);
64  return i;
65 }
66 
67 template <class Handle, class ForwardReadableRange, bool IsContiguous>
68 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
69 senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>::
70 writeto(Handle & handle, ForwardReadableRange & range, typename Handle::Address const & addr)
71 {
72  typename boost::range_size<ForwardReadableRange>::type nwrite (boost::size(range));
73  typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
74  SENF_SCOPED_BUFFER(char, buffer, nwrite);
75  std::copy(i, boost::end(range), buffer);
76  std::advance(i, handle.writeto(std::make_pair(buffer, buffer+nwrite), addr) - buffer);
77  return i;
78 }
79 
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::ClientSocketHandle<Policy>
82 
83 //-/////////////////////////////////////////////////////////////////////////////////////////////////
84 // reading and writing
85 
86 // senf::ClientSocketHandle<Policy>::read
87 
88 template <class SPolicy>
89 prefix_ std::string senf::ClientSocketHandle<SPolicy>::read(unsigned limit)
90 {
91  std::string rv;
92  this->read(rv, limit);
93  return rv;
94 }
95 
96 template <class SPolicy>
97 template <class Sequence>
98 prefix_ void senf::ClientSocketHandle<SPolicy>::read(Sequence & container, unsigned limit)
99 {
100  if (limit == 0)
101  limit = available();
102  container.resize(limit);
103  container.erase(read( std::make_pair(container.begin(), container.end()) ),
104  container.end());
105 }
106 
107 // senf::ClientSocketHandle<SPolicy>::readfrom
108 
109 template <class SPolicy>
110 prefix_ std::pair<std::string, typename SPolicy::AddressingPolicy::Address>
111 senf::ClientSocketHandle<SPolicy>::readfrom(unsigned limit)
112 {
113  std::string rv;
114  boost::value_initialized<typename SPolicy::AddressingPolicy::Address> addr;
115  this->readfrom(rv, addr.data(), limit);
116  return std::make_pair(rv, addr.data());
117 }
118 
119 template <class SPolicy>
120 template <class Sequence>
121 prefix_ void senf::ClientSocketHandle<SPolicy>::readfrom(Sequence & container, Address & from,
122  unsigned limit)
123 {
124  if (limit == 0)
125  limit = available();
126  container.resize(limit);
127  container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ),
128  container.end());
129 }
130 
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 // private members
133 
134 // senf::ClientSocketHandle<SPolicy>::available
135 
136 template <class SPolicy>
137 prefix_ unsigned senf::ClientSocketHandle<SPolicy>::available()
138 {
139  unsigned nread = this->protocol().available();
140  if (nread == 0 && this->blocking()) {
141  // We have to block explicitly here so we can return the
142  // number of bytes available explicitly. If no more date can
143  // be expected to arive (i.e. the other end has closed the
144  // connection), the socket will always be in the readable
145  // state. This is the only case when available() will return
146  // 0.
147  this->waitReadable();
148  nread = this->protocol().available();
149  }
150  return nread;
151 }
152 
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154 #undef prefix_
155 
156 
157 // Local Variables:
158 // mode: c++
159 // fill-column: 100
160 // c-file-style: "senf"
161 // indent-tabs-mode: nil
162 // ispell-local-dictionary: "american"
163 // compile-command: "scons -u test"
164 // comment-column: 40
165 // End: