Implement more compatible and flexible ClientSocketHandle::write() and writeto()...
[senf.git] / Socket / ClientSocketHandle.cti
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 inline template implementation
25  */
26
27 #include "ClientSocketHandle.ih"
28
29 // Custom includes
30 #include <typeinfo>
31
32 #define prefix_ inline
33 ///////////////////////////////cti.p///////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::detail::ReadRange<Policy,ForwardWritableRange,true>
37
38 template <class Handle, class ForwardWritableRange>
39 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
40 senf::detail::ReadRange<Handle,ForwardWritableRange,true>::read(Handle & handle,
41                                                                 ForwardWritableRange & range)
42 {
43     typename boost::range_iterator<ForwardWritableRange>::type const i (boost::begin(range));
44     char * const ic (reinterpret_cast<char*>(storage_iterator(i)));
45     return i + (handle.read( ic, 
46                              reinterpret_cast<char*>(storage_iterator(boost::end(range))) )
47                 - ic);
48 }
49
50 template <class Handle, class ForwardWritableRange>
51 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
52 senf::detail::ReadRange<Handle,ForwardWritableRange,true>::
53 readfrom(Handle & handle, ForwardWritableRange & range, typename Handle::Address & addr)
54 {
55     typename boost::range_iterator<ForwardWritableRange>::type const i (boost::begin(range));
56     char * const ic (reinterpret_cast<char*>(storage_iterator(i)));
57     return i + (handle.readfrom( ic, 
58                                  reinterpret_cast<char*>(storage_iterator(boost::end(range))),
59                                  addr )
60                 - ic);
61 }
62
63 ///////////////////////////////////////////////////////////////////////////
64 // senf::detail::WriteRange<Handle, ForwardReadableRange, true>
65
66 template <class Handle, class ForwardReadableRange>
67 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
68 senf::detail::WriteRange<Handle, ForwardReadableRange, true>::
69 write(Handle & handle, ForwardReadableRange & range)
70 {
71     typename boost::range_const_iterator<ForwardReadableRange>::type const i 
72         (boost::const_begin(range));
73     char const * const ic (reinterpret_cast<char const *>(storage_iterator(i)));
74     return i + (handle.write(ic,
75                              reinterpret_cast<char const *>(
76                                  storage_iterator(boost::const_end(range))))
77                 - ic);
78 }
79
80 template <class Handle, class ForwardReadableRange>
81 prefix_ typename boost::range_const_iterator<ForwardReadableRange>::type
82 senf::detail::WriteRange<Handle, ForwardReadableRange, true>::
83 writeto(Handle & handle, ForwardReadableRange & range, typename Handle::Address const & addr)
84 {
85     typename boost::range_const_iterator<ForwardReadableRange>::type const i 
86         (boost::const_begin(range));
87     char const * const ic (reinterpret_cast<char const *>(storage_iterator(i)));
88     return i + (handle.writeto(addr,
89                                ic,
90                                reinterpret_cast<char const *>(
91                                    storage_iterator(boost::const_end(range))))
92                 - ic);
93 }
94
95 ///////////////////////////////////////////////////////////////////////////
96 // senf::ClientSocketHandle<Policy>
97
98 ////////////////////////////////////////
99 // structors
100
101 template <class Policy>
102 template <class OtherPolicy>
103 prefix_ senf::ClientSocketHandle<Policy>::
104 ClientSocketHandle(ClientSocketHandle<OtherPolicy> other,
105                    typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type *)
106     : SocketHandle<Policy>(other,true) 
107 {}
108
109 template <class Policy>
110 prefix_ senf::ClientSocketHandle<Policy>::ClientSocketHandle(FileHandle other, bool isChecked)
111     : SocketHandle<Policy>(other, isChecked) 
112 {}
113
114 template <class Policy>
115 prefix_
116 senf::ClientSocketHandle<Policy>::ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol,
117                                                      int fd)
118     : SocketHandle<Policy>(protocol,false) 
119 {
120     this->body().fd(fd);
121 }
122
123 template <class Policy>
124 template <class OtherPolicy>
125 prefix_ typename senf::SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type const &
126 senf::ClientSocketHandle<Policy>::operator=(ClientSocketHandle<OtherPolicy> other)
127 {
128     assign(other);
129     return *this;
130 }
131
132 ////////////////////////////////////////
133 // reading and writing
134
135 // senf::ClientSocketHandle<Policy>::read
136
137 template <class Policy>
138 template <class ForwardWritableRange>
139 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
140 senf::ClientSocketHandle<Policy>::read(ForwardWritableRange const & range)
141 {
142     return detail::ReadRange< 
143         ClientSocketHandle<Policy>, 
144         ForwardWritableRange const, 
145         contiguous_storage_iterator<
146             typename boost::range_iterator<ForwardWritableRange>::type
147         >::value && sizeof(typename boost::range_value<ForwardWritableRange>::type)==sizeof(char)
148         >::read(*this, range);
149 }
150
151 template <class Policy>
152 template <class ForwardWritableRange>
153 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
154 senf::ClientSocketHandle<Policy>::read(ForwardWritableRange & range)
155 {
156     return detail::ReadRange< 
157         ClientSocketHandle<Policy>, 
158         ForwardWritableRange, 
159         contiguous_storage_iterator<
160             typename boost::range_iterator<ForwardWritableRange>::type
161         >::value && sizeof(typename boost::range_value<ForwardWritableRange>::type)==sizeof(char)
162         >::read(*this, range);
163 }
164
165 template <class Policy>
166 prefix_ char * senf::ClientSocketHandle<Policy>::read(char * start, char * end)
167 {
168     return start + Policy::ReadPolicy::read(*this, start, end-start);
169 }
170
171 // senf::ClientSocketHandle<Policy>::readfrom
172
173 template <class Policy>
174 template <class ForwardWritableRange>
175 prefix_ typename boost::range_iterator<ForwardWritableRange const>::type
176 senf::ClientSocketHandle<Policy>::readfrom(ForwardWritableRange const & range, Address & from)
177 {
178     return detail::ReadRange< 
179         ClientSocketHandle<Policy>, 
180         ForwardWritableRange const, 
181         contiguous_storage_iterator<
182             typename boost::range_iterator<ForwardWritableRange>::type
183         >::value && sizeof(typename boost::range_value<ForwardWritableRange>::type)==sizeof(char)
184         >::readfrom(*this, range, from);
185 }
186
187 template <class Policy>
188 template <class ForwardWritableRange>
189 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
190 senf::ClientSocketHandle<Policy>::readfrom(ForwardWritableRange & range, Address & from)
191 {
192     return detail::ReadRange< 
193         ClientSocketHandle<Policy>, 
194         ForwardWritableRange, 
195         contiguous_storage_iterator<
196             typename boost::range_iterator<ForwardWritableRange>::type
197         >::value && sizeof(typename boost::range_value<ForwardWritableRange>::type)==sizeof(char)
198         >::readfrom(*this, range, from);
199 }
200
201 template <class Policy>
202 prefix_ char * senf::ClientSocketHandle<Policy>::readfrom(char * start, char * end,
203                                                           Address & from)
204 {
205     return start + Policy::ReadPolicy::readfrom(*this, start, end-start, from);
206 }
207
208 // senf::ClientSocketHandle<Policy>::write
209
210 template <class Policy>
211 template <class ForwardReadableRange>
212 prefix_ typename boost::range_const_iterator<ForwardReadableRange const>::type
213 senf::ClientSocketHandle<Policy>::write(ForwardReadableRange const & range)
214 {
215     return detail::WriteRange<
216         ClientSocketHandle<Policy>,
217         ForwardReadableRange const,
218         contiguous_storage_iterator<
219             typename boost::range_iterator<ForwardReadableRange>::type
220         >::value && sizeof(typename boost::range_value<ForwardReadableRange>::type)==sizeof(char)
221         >::write(*this, range);
222 }
223
224 template <class Policy>
225 prefix_ char const * senf::ClientSocketHandle<Policy>::write(char const * start, char const * end)
226 {
227     return start + Policy::WritePolicy::write(*this, start, end-start);
228 }
229
230 // senf::ClientSocketHandle<Policy>::writeto
231
232 template <class Policy>
233 template <class ForwardReadableRange>
234 prefix_ typename boost::range_const_iterator<ForwardReadableRange const>::type
235 senf::ClientSocketHandle<Policy>::writeto(AddressParam addr, ForwardReadableRange const & range)
236 {
237     return detail::WriteRange<
238         ClientSocketHandle<Policy>,
239         ForwardReadableRange const,
240         contiguous_storage_iterator<
241             typename boost::range_iterator<ForwardReadableRange>::type
242         >::value && sizeof(typename boost::range_value<ForwardReadableRange>::type)==sizeof(char)
243         >::writeto(*this, range, addr);
244 }
245
246 template <class Policy>
247 prefix_ char const * senf::ClientSocketHandle<Policy>::writeto(AddressParam addr,
248                                                                char const * start,
249                                                                char const * end)
250 {
251     return start + Policy::WritePolicy::writeto(*this, addr, start, end-start);
252 }
253
254 ////////////////////////////////////////
255 // addressing
256
257 // senf::ClientSocketHandle<Policy>::peer
258
259 template <class Policy>
260 prefix_ typename Policy::AddressingPolicy::Address
261 senf::ClientSocketHandle<Policy>::peer()
262 {
263     typename Policy::AddressingPolicy::Address addr;
264     this->peer(addr);
265     return addr;
266 }
267
268 template <class Policy>
269 prefix_ void senf::ClientSocketHandle<Policy>::
270 peer(typename Policy::AddressingPolicy::Address & addr)
271 {
272     Policy::AddressingPolicy::peer(*this,addr);
273 }
274
275 // senf::ClientSocketHandle<Policy>::local
276
277 template <class Policy>
278 prefix_ typename Policy::AddressingPolicy::Address
279 senf::ClientSocketHandle<Policy>::local()
280 {
281     typename Policy::AddressingPolicy::Address addr;
282     this->local(addr);
283     return addr;
284 }
285
286 template <class Policy>
287 prefix_ void senf::ClientSocketHandle<Policy>::
288 local(typename Policy::AddressingPolicy::Address & addr)
289 {
290     Policy::AddressingPolicy::local(*this,addr);
291 }
292
293 // senf::ClientSocketHandle<Policy>::connect
294
295 template <class Policy>
296 prefix_ void senf::ClientSocketHandle<Policy>::connect(AddressParam addr)
297 {
298     Policy::AddressingPolicy::connect(*this,addr);
299 }
300
301 // senf::ClientSocketHandle<Policy>::bind
302
303 template <class Policy>
304 prefix_ void senf::ClientSocketHandle<Policy>::
305 bind(typename boost::call_traits<typename Policy::AddressingPolicy::Address>::param_type addr)
306 {
307     Policy::AddressingPolicy::bind(*this,addr);
308 }
309
310 ////////////////////////////////////////
311 // Buffering
312
313 // senf::ClientSocketHandle<Policy>::rcvbuf
314
315 template <class Policy>
316 prefix_ unsigned senf::ClientSocketHandle<Policy>::rcvbuf()
317 {
318     return Policy::BufferingPolicy::rcvbuf(*this);
319 }
320
321 template <class Policy>
322 prefix_ void senf::ClientSocketHandle<Policy>::rcvbuf(unsigned size)
323 {
324     Policy::BufferingPolicy::rcvbuf(*this,size);
325 }
326
327 // senf::ClientSocketHandle<Policy>::sndbuf
328
329 template <class Policy>
330 prefix_ unsigned senf::ClientSocketHandle<Policy>::sndbuf()
331 {
332     return Policy::BufferingPolicy::sndbuf(*this);
333 }
334
335 template <class Policy>
336 prefix_ void senf::ClientSocketHandle<Policy>::sndbuf(unsigned size)
337 {
338     Policy::BufferingPolicy::sndbuf(*this,size);
339 }
340
341 ////////////////////////////////////////
342 // Casting
343
344 template <class Policy>
345 prefix_ senf::ClientSocketHandle<Policy>
346 senf::ClientSocketHandle<Policy>::cast_static(FileHandle handle)
347 {
348     return ClientSocketHandle(handle, true);
349 }
350
351 template <class Policy>
352 prefix_ senf::ClientSocketHandle<Policy>
353 senf::ClientSocketHandle<Policy>::cast_dynamic(FileHandle handle)
354 {
355     SocketHandle<Policy> h (SocketHandle<Policy>::cast_dynamic(handle));
356     if (static_cast<SocketBody&>(FileHandle::body(h)).isServer())
357         throw std::bad_cast();
358     return cast_static(handle);
359 }
360
361 ////////////////////////////////////////
362 // State information
363
364 template <class Policy>
365 prefix_ void senf::ClientSocketHandle<Policy>::state(SocketStateMap & map, unsigned lod)
366 {
367     map["handle"] = prettyName(typeid(*this));
368     this->body().state(map,lod);
369 }
370
371 template <class Policy>
372 prefix_ std::string senf::ClientSocketHandle<Policy>::dumpState(unsigned lod)
373 {
374     SocketStateMap map;
375     state(map,lod);
376     return detail::dumpState(map);
377 }
378
379 ///////////////////////////////cti.e///////////////////////////////////////
380 #undef prefix_
381
382 \f
383 // Local Variables:
384 // mode: c++
385 // fill-column: 100
386 // c-file-style: "senf"
387 // indent-tabs-mode: nil
388 // ispell-local-dictionary: "american"
389 // End: