50c80a81ba6c12d0a2ba613ee2a2c5855956bd61
[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 // Definition of non-inline template functions
24
25 //#include "ClientSocketHandle.ih"
26
27 // Custom includes
28
29 #define prefix_
30 ///////////////////////////////ct.p////////////////////////////////////////
31
32 template <class Policy>
33 prefix_ std::string satcom::lib::ClientSocketHandle<Policy>::read(unsigned limit)
34 {
35     std::string rv;
36     this->read(rv,limit);
37     return rv;
38 }
39
40 template <class Policy>
41 prefix_ void satcom::lib::ClientSocketHandle<Policy>::read(std::string & buffer, unsigned limit)
42 {
43     unsigned nread = available();
44     if (limit>0 && nread>limit) 
45         nread = limit;
46     // FIXME: This is not necessary correct and more or less a hack ...
47     buffer.assign(nread,0);
48     unsigned rv = this->read(const_cast<char *>(buffer.data()),nread);
49     if (rv < nread)
50         buffer.erase(buffer.begin()+rv,buffer.end());
51 }
52
53 template <class Policy>
54 prefix_ std::pair<std::string, typename Policy::AddressingPolicy::Address>
55 satcom::lib::ClientSocketHandle<Policy>::readfrom()
56 {
57     std::string rv;
58     typename Policy::AddressingPolicy::Address addr;
59     this->readfrom(rv,addr);
60     return std::make_pair(rv,addr);
61 }
62
63 template <class Policy>
64 prefix_ void satcom::lib::ClientSocketHandle<Policy>::
65 readfrom(std::string & buffer, typename Policy::AddressingPolicy::Address & from)
66 {
67     unsigned nread = available();
68     // FIXME: This is not necessary correct and more or less a hack ...
69     buffer.assign(nread,0);
70     unsigned rv = this->readfrom(const_cast<char *>(buffer.data()), nread, from);
71     if (rv < nread)
72         buffer.erase(buffer.begin()+rv,buffer.end());
73 }
74
75 template <class Policy>
76 prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>::write(std::string const & data)
77 {
78     unsigned written = this->write(data.data(),data.size());
79     if (written == 0)
80         throw SystemException(EPIPE);
81     // This implementation ensures, we only call blocking() when
82     // necessary (since it incurs a system call overhead ...)
83     if (written < data.size() && this->blocking())
84         // We need to enforce in the WritePolicy implementation, that
85         // DatagramFramingPolicy sockets will ALWAYS either write the
86         // complete datagram or nothing at all
87         while (written < data.size()) {
88             unsigned n = this->write(data.data()+written,data.size()-written); 
89             if (n == 0)
90                 throw SystemException(EPIPE);
91             written += n;
92         }
93     return written;
94 }
95
96 template <class Policy>
97 prefix_ unsigned satcom::lib::ClientSocketHandle<Policy>::available()
98 {
99     unsigned nread = this->protocol().available();
100     if (nread == 0 && this->blocking()) {
101             this->waitReadable();
102             nread = this->protocol().available();
103     }
104     return nread;
105 }
106
107 ///////////////////////////////ct.e////////////////////////////////////////
108 #undef prefix_
109
110 \f
111 // Local Variables:
112 // mode: c++
113 // c-file-style: "satcom"
114 // End: