4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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 "Utils/Buffer.hh"
34 ///////////////////////////////ct.p////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>
39 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::read
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)
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));
51 // senf::detail::ReadRange<Handle,ForwardWritableRange,IsContiguous>::readfrom
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)
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));
63 ///////////////////////////////////////////////////////////////////////////
64 // senf::ClientSocketHandle<Policy>
66 ////////////////////////////////////////
67 // reading and writing
69 // senf::ClientSocketHandle<Policy>::read
71 template <class Policy>
72 prefix_ std::string senf::ClientSocketHandle<Policy>::read(unsigned limit)
79 template <class Policy>
80 template <class Sequence>
81 prefix_ void senf::ClientSocketHandle<Policy>::read(Sequence & container, unsigned limit)
83 unsigned nread (available());
84 if (limit>0 && nread>limit)
86 container.resize(nread);
87 container.erase(read( std::make_pair(container.begin(), container.end()) ),
91 // senf::ClientSocketHandle<Policy>::readfrom
93 template <class Policy>
94 prefix_ std::pair<std::string, typename Policy::AddressingPolicy::Address>
95 senf::ClientSocketHandle<Policy>::readfrom(unsigned limit)
98 typename Policy::AddressingPolicy::Address addr;
99 this->readfrom(rv,addr,limit);
100 return std::make_pair(rv,addr);
103 template <class Policy>
104 template <class Sequence>
105 prefix_ void senf::ClientSocketHandle<Policy>::readfrom(Sequence & container, Address & from,
108 unsigned nread (available());
109 if (limit>0 && nread>limit)
111 container.resize(nread);
112 container.erase(readfrom( std::make_pair(container.begin(), container.end()), from ),
116 // senf::ClientSocketHandle<Policy>::write
118 template <class Policy>
119 prefix_ unsigned senf::ClientSocketHandle<Policy>::write(std::string const & data)
121 unsigned written = this->write(data.data(),data.size());
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);
133 throw SystemException(EPIPE);
139 ////////////////////////////////////////
142 // senf::ClientSocketHandle<Policy>::available
144 template <class Policy>
145 prefix_ unsigned senf::ClientSocketHandle<Policy>::available()
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
155 this->waitReadable();
156 nread = this->protocol().available();
161 ///////////////////////////////ct.e////////////////////////////////////////
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"