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