4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief ClientSocketHandle non-inline template implementation
27 #include "ClientSocketHandle.ih"
31 #include <boost/utility/value_init.hpp>
32 #include <senf/Utils/Buffer.hh>
35 ///////////////////////////////ct.p////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
40 template <class Handle, class ForwardWritableRange, bool IsContiguous>
41 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
42 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
43 read(Handle & handle, ForwardWritableRange & range)
45 typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
46 SENF_SCOPED_BUFFER(char, buffer, nread);
47 return std::copy(buffer, handle.read(buffer,buffer+nread), boost::begin(range));
50 template <class Handle, class ForwardWritableRange, bool IsContiguous>
51 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
52 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
53 readfrom(Handle & handle, ForwardWritableRange & range, typename Handle::Address & addr)
55 typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
56 SENF_SCOPED_BUFFER(char, buffer, nread);
57 return std::copy(buffer, handle.readfrom(buffer,buffer+nread,addr), boost::begin(range));
60 ///////////////////////////////////////////////////////////////////////////
61 // senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>
63 template <class Handle, class ForwardReadableRange, bool IsContiguous>
64 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
65 senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>::
66 write(Handle & handle, ForwardReadableRange & range)
68 typename boost::range_size<ForwardReadableRange>::type nwrite (boost::size(range));
69 typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
70 SENF_SCOPED_BUFFER(char, buffer, nwrite);
71 std::copy(i, boost::end(range), buffer);
72 std::advance(i, handle.write(std::make_pair(buffer, buffer+nwrite)) - buffer);
76 template <class Handle, class ForwardReadableRange, bool IsContiguous>
77 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
78 senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>::
79 writeto(Handle & handle, ForwardReadableRange & range, typename Handle::Address const & addr)
81 typename boost::range_size<ForwardReadableRange>::type nwrite (boost::size(range));
82 typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
83 SENF_SCOPED_BUFFER(char, buffer, nwrite);
84 std::copy(i, boost::end(range), buffer);
85 std::advance(i, handle.writeto(std::make_pair(buffer, buffer+nwrite), addr) - buffer);
89 ///////////////////////////////////////////////////////////////////////////
90 // senf::ClientSocketHandle<Policy>
92 ////////////////////////////////////////
93 // reading and writing
95 // senf::ClientSocketHandle<Policy>::read
97 template <class SPolicy>
98 prefix_ std::string senf::ClientSocketHandle<SPolicy>::read(unsigned limit)
101 this->read(rv, limit);
105 template <class SPolicy>
106 template <class Sequence>
107 prefix_ void senf::ClientSocketHandle<SPolicy>::read(Sequence & container, unsigned limit)
111 container.resize(limit);
112 container.erase(read( std::make_pair(container.begin(), container.end()) ),
116 // senf::ClientSocketHandle<SPolicy>::readfrom
118 template <class SPolicy>
119 prefix_ std::pair<std::string, typename SPolicy::AddressingPolicy::Address>
120 senf::ClientSocketHandle<SPolicy>::readfrom(unsigned limit)
123 boost::value_initialized<typename SPolicy::AddressingPolicy::Address> addr;
124 this->readfrom(rv, addr.data(), limit);
125 return std::make_pair(rv, addr.data());
128 template <class SPolicy>
129 template <class Sequence>
130 prefix_ void senf::ClientSocketHandle<SPolicy>::readfrom(Sequence & container, Address & from,
135 container.resize(limit);
136 container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ),
140 ////////////////////////////////////////
143 // senf::ClientSocketHandle<SPolicy>::available
145 template <class SPolicy>
146 prefix_ unsigned senf::ClientSocketHandle<SPolicy>::available()
148 unsigned nread = this->protocol().available();
149 if (nread == 0 && this->blocking()) {
150 // We have to block explicitly here so we can return the
151 // number of bytes available explicitly. If no more date can
152 // be expected to arive (i.e. the other end has closed the
153 // connection), the socket will always be in the readable
154 // state. This is the only case when available() will return
156 this->waitReadable();
157 nread = this->protocol().available();
162 ///////////////////////////////ct.e////////////////////////////////////////
169 // c-file-style: "senf"
170 // indent-tabs-mode: nil
171 // ispell-local-dictionary: "american"
172 // compile-command: "scons -u test"
173 // comment-column: 40