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
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
15 \brief ClientSocketHandle non-inline template implementation
18 #include "ClientSocketHandle.ih"
22 #include <boost/utility/value_init.hpp>
23 #include <senf/Utils/Buffer.hh>
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
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)
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));
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)
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));
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>
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)
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);
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)
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);
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::ClientSocketHandle<Policy>
83 //-/////////////////////////////////////////////////////////////////////////////////////////////////
84 // reading and writing
86 // senf::ClientSocketHandle<Policy>::read
88 template <class SPolicy>
89 prefix_ std::string senf::ClientSocketHandle<SPolicy>::read(unsigned limit)
92 this->read(rv, limit);
96 template <class SPolicy>
97 template <class Sequence>
98 prefix_ void senf::ClientSocketHandle<SPolicy>::read(Sequence & container, unsigned limit)
102 container.resize(limit);
103 container.erase(read( std::make_pair(container.begin(), container.end()) ),
107 // senf::ClientSocketHandle<SPolicy>::readfrom
109 template <class SPolicy>
110 prefix_ std::pair<std::string, typename SPolicy::AddressingPolicy::Address>
111 senf::ClientSocketHandle<SPolicy>::readfrom(unsigned limit)
114 boost::value_initialized<typename SPolicy::AddressingPolicy::Address> addr;
115 this->readfrom(rv, addr.data(), limit);
116 return std::make_pair(rv, addr.data());
119 template <class SPolicy>
120 template <class Sequence>
121 prefix_ void senf::ClientSocketHandle<SPolicy>::readfrom(Sequence & container, Address & from,
126 container.resize(limit);
127 container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ),
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
134 // senf::ClientSocketHandle<SPolicy>::available
136 template <class SPolicy>
137 prefix_ unsigned senf::ClientSocketHandle<SPolicy>::available()
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
147 this->waitReadable();
148 nread = this->protocol().available();
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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