Fixed whitespace in all files (no tabs)
[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
31 #define prefix_
32 ///////////////////////////////ct.p////////////////////////////////////////
33
34 template <class Policy>
35 prefix_ std::string senf::ClientSocketHandle<Policy>::read(unsigned limit)
36 {
37     std::string rv;
38     this->read(rv,limit);
39     return rv;
40 }
41
42 template <class Policy>
43 prefix_ void senf::ClientSocketHandle<Policy>::read(std::string & buffer, unsigned limit)
44 {
45     unsigned nread = available();
46     if (limit>0 && nread>limit)
47         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);
51     if (rv < nread)
52         buffer.erase(buffer.begin()+rv,buffer.end());
53 }
54
55 template <class Policy>
56 prefix_ std::pair<std::string, typename Policy::AddressingPolicy::Address>
57 senf::ClientSocketHandle<Policy>::readfrom()
58 {
59     std::string rv;
60     typename Policy::AddressingPolicy::Address addr;
61     this->readfrom(rv,addr);
62     return std::make_pair(rv,addr);
63 }
64
65 template <class Policy>
66 prefix_ void senf::ClientSocketHandle<Policy>::
67 readfrom(std::string & buffer, typename Policy::AddressingPolicy::Address & from)
68 {
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);
73     if (rv < nread)
74         buffer.erase(buffer.begin()+rv,buffer.end());
75 }
76
77 template <class Policy>
78 prefix_ unsigned senf::ClientSocketHandle<Policy>::write(std::string const & data)
79 {
80     unsigned written = this->write(data.data(),data.size());
81     if (written == 0)
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);
91             if (n == 0)
92                 throw SystemException(EPIPE);
93             written += n;
94         }
95     return written;
96 }
97
98 template <class Policy>
99 prefix_ unsigned senf::ClientSocketHandle<Policy>::available()
100 {
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
108         // 0.
109         this->waitReadable();
110         nread = this->protocol().available();
111     }
112     return nread;
113 }
114
115 ///////////////////////////////ct.e////////////////////////////////////////
116 #undef prefix_
117
118 \f
119 // Local Variables:
120 // mode: c++
121 // fill-column: 100
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // End: