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"
32 ///////////////////////////////ct.p////////////////////////////////////////
34 template <class Policy>
35 prefix_ std::string senf::ClientSocketHandle<Policy>::read(unsigned limit)
42 template <class Policy>
43 prefix_ void senf::ClientSocketHandle<Policy>::read(std::string & buffer, unsigned limit)
45 unsigned nread = available();
46 if (limit>0 && nread>limit)
48 /** \fixme This is not necessary correct and more or less a hack ... */
49 buffer.assign(nread,0);
50 unsigned rv = this->read(const_cast<char *>(buffer.data()),nread);
52 buffer.erase(buffer.begin()+rv,buffer.end());
55 template <class Policy>
56 prefix_ std::pair<std::string, typename Policy::AddressingPolicy::Address>
57 senf::ClientSocketHandle<Policy>::readfrom()
60 typename Policy::AddressingPolicy::Address addr;
61 this->readfrom(rv,addr);
62 return std::make_pair(rv,addr);
65 template <class Policy>
66 prefix_ void senf::ClientSocketHandle<Policy>::
67 readfrom(std::string & buffer, typename Policy::AddressingPolicy::Address & from)
69 unsigned nread = available();
70 /** \fixme This is not necessary correct and more or less a hack ... */
71 buffer.assign(nread,0);
72 unsigned rv = this->readfrom(const_cast<char *>(buffer.data()), nread, from);
74 buffer.erase(buffer.begin()+rv,buffer.end());
77 template <class Policy>
78 prefix_ unsigned senf::ClientSocketHandle<Policy>::write(std::string const & data)
80 unsigned written = this->write(data.data(),data.size());
82 throw SystemException(EPIPE);
83 // This implementation ensures, we only call blocking() when
84 // necessary (since it incurs a system call overhead ...)
85 if (written < data.size() && this->blocking())
86 // We need to enforce in the WritePolicy implementation, that
87 // DatagramFramingPolicy sockets will ALWAYS either write the
88 // complete datagram or nothing at all
89 while (written < data.size()) {
90 unsigned n = this->write(data.data()+written,data.size()-written);
92 throw SystemException(EPIPE);
98 template <class Policy>
99 prefix_ unsigned senf::ClientSocketHandle<Policy>::available()
101 unsigned nread = this->protocol().available();
102 if (nread == 0 && this->blocking()) {
103 // We have to block explicitly here so we can return the
104 // number of bytes available explicitly. If no more date can
105 // be expected to arive (i.e. the other end has closed the
106 // connection), the socket will always be in the readable
107 // state. This is the only case when available() will return
109 this->waitReadable();
110 nread = this->protocol().available();
115 ///////////////////////////////ct.e////////////////////////////////////////
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"