Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Socket / ClientSocketHandle.ct
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief ClientSocketHandle non-inline template implementation
25  */
26
27 #include "ClientSocketHandle.ih"
28
29 // Custom includes
30 #include <algorithm>
31 #include <boost/utility/value_init.hpp>
32 #include <senf/Utils/Buffer.hh>
33
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
39
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)
44 {
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));
48 }
49
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)
54 {
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));
58 }
59
60 //-/////////////////////////////////////////////////////////////////////////////////////////////////
61 // senf::detail::WriteRange<Handle,ForwardReadableRange,IsContiguous>
62
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)
67 {
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);
73     return i;
74 }
75
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)
80 {
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);
86     return i;
87 }
88
89 //-/////////////////////////////////////////////////////////////////////////////////////////////////
90 // senf::ClientSocketHandle<Policy>
91
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 // reading and writing
94
95 // senf::ClientSocketHandle<Policy>::read
96
97 template <class SPolicy>
98 prefix_ std::string senf::ClientSocketHandle<SPolicy>::read(unsigned limit)
99 {
100     std::string rv;
101     this->read(rv, limit);
102     return rv;
103 }
104
105 template <class SPolicy>
106 template <class Sequence>
107 prefix_ void senf::ClientSocketHandle<SPolicy>::read(Sequence & container, unsigned limit)
108 {
109     if (limit == 0)
110         limit = available();
111     container.resize(limit);
112     container.erase(read( std::make_pair(container.begin(), container.end()) ),
113                     container.end());
114 }
115
116 // senf::ClientSocketHandle<SPolicy>::readfrom
117
118 template <class SPolicy>
119 prefix_ std::pair<std::string, typename SPolicy::AddressingPolicy::Address>
120 senf::ClientSocketHandle<SPolicy>::readfrom(unsigned limit)
121 {
122     std::string rv;
123     boost::value_initialized<typename SPolicy::AddressingPolicy::Address> addr;
124     this->readfrom(rv, addr.data(), limit);
125     return std::make_pair(rv, addr.data());
126 }
127
128 template <class SPolicy>
129 template <class Sequence>
130 prefix_ void senf::ClientSocketHandle<SPolicy>::readfrom(Sequence & container, Address & from,
131                                                         unsigned limit)
132 {
133     if (limit == 0)
134         limit = available();
135     container.resize(limit);
136     container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ),
137                     container.end());
138 }
139
140 //-/////////////////////////////////////////////////////////////////////////////////////////////////
141 // private members
142
143 // senf::ClientSocketHandle<SPolicy>::available
144
145 template <class SPolicy>
146 prefix_ unsigned senf::ClientSocketHandle<SPolicy>::available()
147 {
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
155         // 0.
156         this->waitReadable();
157         nread = this->protocol().available();
158     }
159     return nread;
160 }
161
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
163 #undef prefix_
164
165 \f
166 // Local Variables:
167 // mode: c++
168 // fill-column: 100
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
174 // End: