Ipmlemented more compatible, standards conformant and flexible ClientSocketHandle...
[senf.git] / Socket / ClientSocketHandle.ct
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 non-inline template implementation
25  */
26
27 #include "ClientSocketHandle.ih"
28
29 // Custom includes
30 #include <algorithm>
31 #include "Utils/Buffer.hh"
32
33 #define prefix_
34 ///////////////////////////////ct.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
38
39 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::read
40
41 template <class Handle, class ForwardWritableRange, bool IsContiguous>
42 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
43 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
44 read(Handle & handle, ForwardWritableRange & range)
45 {
46     typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
47     SENF_SCOPED_BUFFER(char, buffer, nread);
48     return std::copy(buffer, handle.read(buffer,buffer+nread), boost::begin(range));
49 }
50
51 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::readfrom
52
53 template <class Handle, class ForwardWritableRange, bool IsContiguous>
54 prefix_ typename boost::range_iterator<ForwardWritableRange>::type
55 senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::
56 readfrom(Handle & handle, ForwardWritableRange & range, typename Handle::Address & addr)
57 {
58     typename boost::range_size<ForwardWritableRange>::type nread (boost::size(range));
59     SENF_SCOPED_BUFFER(char, buffer, nread);
60     return std::copy(buffer, handle.readfrom(buffer,buffer+nread,addr), boost::begin(range));
61 }
62
63 ///////////////////////////////////////////////////////////////////////////
64 // senf::ClientSocketHandle<Policy>
65
66 ////////////////////////////////////////
67 // reading and writing
68
69 // senf::ClientSocketHandle<Policy>::read
70
71 template <class Policy>
72 prefix_ std::string senf::ClientSocketHandle<Policy>::read(unsigned limit)
73 {
74     std::string rv;
75     this->read(rv,limit);
76     return rv;
77 }
78
79 template <class Policy>
80 template <class Sequence>
81 prefix_ void senf::ClientSocketHandle<Policy>::read(Sequence & container, unsigned limit)
82 {
83     unsigned nread (available());
84     if (limit>0 && nread>limit)
85         nread = limit;
86     container.resize(nread);
87     container.erase(read( std::make_pair(container.begin(), container.end()) ), 
88                     container.end());
89 }
90
91 // senf::ClientSocketHandle<Policy>::readfrom
92
93 template <class Policy>
94 prefix_ std::pair<std::string, typename Policy::AddressingPolicy::Address>
95 senf::ClientSocketHandle<Policy>::readfrom(unsigned limit)
96 {
97     std::string rv;
98     typename Policy::AddressingPolicy::Address addr;
99     this->readfrom(rv,addr,limit);
100     return std::make_pair(rv,addr);
101 }
102
103 template <class Policy>
104 template <class Sequence>
105 prefix_ void senf::ClientSocketHandle<Policy>::readfrom(Sequence & container, Address & from,
106                                                         unsigned limit)
107 {
108     unsigned nread (available());
109     if (limit>0 && nread>limit)
110         nread = limit;
111     container.resize(nread);
112     container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ), 
113                     container.end());
114 }
115
116 // senf::ClientSocketHandle<Policy>::write
117
118 template <class Policy>
119 prefix_ unsigned senf::ClientSocketHandle<Policy>::write(std::string const & data)
120 {
121     unsigned written = this->write(data.data(),data.size());
122     if (written == 0)
123         throw SystemException(EPIPE);
124     // This implementation ensures, we only call blocking() when
125     // necessary (since it incurs a system call overhead ...)
126     if (written < data.size() && this->blocking())
127         // We need to enforce in the WritePolicy implementation, that
128         // DatagramFramingPolicy sockets will ALWAYS either write the
129         // complete datagram or nothing at all
130         while (written < data.size()) {
131             unsigned n = this->write(data.data()+written,data.size()-written);
132             if (n == 0)
133                 throw SystemException(EPIPE);
134             written += n;
135         }
136     return written;
137 }
138
139 ////////////////////////////////////////
140 // private members
141
142 // senf::ClientSocketHandle<Policy>::available
143
144 template <class Policy>
145 prefix_ unsigned senf::ClientSocketHandle<Policy>::available()
146 {
147     unsigned nread = this->protocol().available();
148     if (nread == 0 && this->blocking()) {
149         // We have to block explicitly here so we can return the
150         // number of bytes available explicitly. If no more date can
151         // be expected to arive (i.e. the other end has closed the
152         // connection), the socket will always be in the readable
153         // state. This is the only case when available() will return
154         // 0.
155         this->waitReadable();
156         nread = this->protocol().available();
157     }
158     return nread;
159 }
160
161 ///////////////////////////////ct.e////////////////////////////////////////
162 #undef prefix_
163
164 \f
165 // Local Variables:
166 // mode: c++
167 // fill-column: 100
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // End: