376ad681ffe92492e11e9d63d1603db9c57c205d
[senf.git] / 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 "../Utils/Buffer.hh"
33
34 #define prefix_
35 ///////////////////////////////ct.p////////////////////////////////////////
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     unsigned nread (available());
110     if (limit>0 && nread>limit)
111         nread = limit;
112     container.resize(nread);
113     container.erase(read( std::make_pair(container.begin(), container.end()) ), 
114                     container.end());
115 }
116
117 // senf::ClientSocketHandle<SPolicy>::readfrom
118
119 template <class SPolicy>
120 prefix_ std::pair<std::string, typename SPolicy::AddressingPolicy::Address>
121 senf::ClientSocketHandle<SPolicy>::readfrom(unsigned limit)
122 {
123     std::string rv;
124     boost::value_initialized<typename SPolicy::AddressingPolicy::Address> addr;
125     this->readfrom(rv, addr.data(), limit);
126     return std::make_pair(rv, addr.data());
127 }
128
129 template <class SPolicy>
130 template <class Sequence>
131 prefix_ void senf::ClientSocketHandle<SPolicy>::readfrom(Sequence & container, Address & from,
132                                                         unsigned limit)
133 {
134     unsigned nread (available());
135     if (limit>0 && nread>limit)
136         nread = limit;
137     container.resize(nread);
138     container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ), 
139                     container.end());
140 }
141
142 ////////////////////////////////////////
143 // private members
144
145 // senf::ClientSocketHandle<SPolicy>::available
146
147 template <class SPolicy>
148 prefix_ unsigned senf::ClientSocketHandle<SPolicy>::available()
149 {
150     unsigned nread = this->protocol().available();
151     if (nread == 0 && this->blocking()) {
152         // We have to block explicitly here so we can return the
153         // number of bytes available explicitly. If no more date can
154         // be expected to arive (i.e. the other end has closed the
155         // connection), the socket will always be in the readable
156         // state. This is the only case when available() will return
157         // 0.
158         this->waitReadable();
159         nread = this->protocol().available();
160     }
161     return nread;
162 }
163
164 ///////////////////////////////ct.e////////////////////////////////////////
165 #undef prefix_
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"
175 // comment-column: 40
176 // End: