4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief ClientSocketHandle non-inline template implementation
32 #include "ClientSocketHandle.ih"
36 #include <boost/utility/value_init.hpp>
37 #include <senf/Utils/Buffer.hh>
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
45 template <class Handle, class ForwardWritableRange, bool IsContiguous>
46 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
47 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
48 read(Handle & handle, ForwardWritableRange & range)
50 typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
51 SENF_SCOPED_BUFFER(char, buffer, nread);
52 return std::copy(buffer, handle.read(buffer,buffer+nread), boost::begin(range));
55 template <class Handle, class ForwardWritableRange, bool IsContiguous>
56 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
57 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
58 readfrom(Handle & handle, ForwardWritableRange & range, typename Handle::Address & addr)
60 typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
61 SENF_SCOPED_BUFFER(char, buffer, nread);
62 return std::copy(buffer, handle.readfrom(buffer,buffer+nread,addr), boost::begin(range));
65 //-/////////////////////////////////////////////////////////////////////////////////////////////////
66 // senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>
68 template <class Handle, class ForwardReadableRange, bool IsContiguous>
69 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
70 senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>::
71 write(Handle & handle, ForwardReadableRange & range)
73 typename boost::range_size<ForwardReadableRange>::type nwrite (boost::size(range));
74 typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
75 SENF_SCOPED_BUFFER(char, buffer, nwrite);
76 std::copy(i, boost::end(range), buffer);
77 std::advance(i, handle.write(std::make_pair(buffer, buffer+nwrite)) - buffer);
81 template <class Handle, class ForwardReadableRange, bool IsContiguous>
82 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
83 senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>::
84 writeto(Handle & handle, ForwardReadableRange & range, typename Handle::Address const & addr)
86 typename boost::range_size<ForwardReadableRange>::type nwrite (boost::size(range));
87 typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
88 SENF_SCOPED_BUFFER(char, buffer, nwrite);
89 std::copy(i, boost::end(range), buffer);
90 std::advance(i, handle.writeto(std::make_pair(buffer, buffer+nwrite), addr) - buffer);
94 //-/////////////////////////////////////////////////////////////////////////////////////////////////
95 // senf::ClientSocketHandle<Policy>
97 //-/////////////////////////////////////////////////////////////////////////////////////////////////
98 // reading and writing
100 // senf::ClientSocketHandle<Policy>::read
102 template <class SPolicy>
103 prefix_ std::string senf::ClientSocketHandle<SPolicy>::read(unsigned limit)
106 this->read(rv, limit);
110 template <class SPolicy>
111 template <class Sequence>
112 prefix_ void senf::ClientSocketHandle<SPolicy>::read(Sequence & container, unsigned limit)
116 container.resize(limit);
117 container.erase(read( std::make_pair(container.begin(), container.end()) ),
121 // senf::ClientSocketHandle<SPolicy>::readfrom
123 template <class SPolicy>
124 prefix_ std::pair<std::string, typename SPolicy::AddressingPolicy::Address>
125 senf::ClientSocketHandle<SPolicy>::readfrom(unsigned limit)
128 boost::value_initialized<typename SPolicy::AddressingPolicy::Address> addr;
129 this->readfrom(rv, addr.data(), limit);
130 return std::make_pair(rv, addr.data());
133 template <class SPolicy>
134 template <class Sequence>
135 prefix_ void senf::ClientSocketHandle<SPolicy>::readfrom(Sequence & container, Address & from,
140 container.resize(limit);
141 container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ),
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148 // senf::ClientSocketHandle<SPolicy>::available
150 template <class SPolicy>
151 prefix_ unsigned senf::ClientSocketHandle<SPolicy>::available()
153 unsigned nread = this->protocol().available();
154 if (nread == 0 && this->blocking()) {
155 // We have to block explicitly here so we can return the
156 // number of bytes available explicitly. If no more date can
157 // be expected to arive (i.e. the other end has closed the
158 // connection), the socket will always be in the readable
159 // state. This is the only case when available() will return
161 this->waitReadable();
162 nread = this->protocol().available();
167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
174 // c-file-style: "senf"
175 // indent-tabs-mode: nil
176 // ispell-local-dictionary: "american"
177 // compile-command: "scons -u test"
178 // comment-column: 40