Fix 'compiled' check in SConstruct
[senf.git] / Socket / ClientSocketHandle.hh
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 senf::ClientSocketHandle public header
25  */
26
27 #ifndef HH_ClientSocketHandle_
28 #define HH_ClientSocketHandle_ 1
29
30 // Custom includes
31 #include <boost/call_traits.hpp>
32 #include "SocketHandle.hh"
33
34 //#include "ClientSocketHandle.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38     
39     /// \addtogroup handle_group
40     /// @{
41     
42     template <class Policy> class ServerSocketHandle;
43
44     /** \brief Generic SocketHandle with client interface
45         
46         This class provides the client side policy interface of the
47         socket abstraction. ClientSocketHandle defines the complete
48         policy interface. It does not implement any functionality
49         itself however. All calls are forward to the following policy
50         classes:
51
52         <table class="senf">
53         <tr><th>ClientSocketHandle member</th> <th>Policy member</th></tr>
54         <tr><td>read()</td>       <td>ReadPolicy::read (\ref senf::ReadPolicyBase)</td></tr>
55         <tr><td>readfrom()</td>   <td>ReadPolicy::readfrom (\ref senf::ReadPolicyBase)</td></tr>
56         <tr><td>write()</td>      <td>WritePolicy::write (\ref senf::WritePolicyBase)</td></tr>
57         <tr><td>writeto()</td>    <td>WritePolicy::writeto (\ref senf::WritePolicyBase)</td></tr>
58         <tr><td>connect()</td>    <td>AddressingPolicy::connect (\ref senf::AddressingPolicyBase)</td></tr>
59         <tr><td>bind()</td>       <td>AddressingPolicy::bind (\ref senf::AddressingPolicyBase)</td></tr>
60         <tr><td>peer()</td>       <td>AddressingPolicy::peer (\ref senf::AddressingPolicyBase)</td></tr>
61         <tr><td>local()</td>      <td>AddressingPolicy::local (\ref senf::AddressingPolicyBase)</td></tr>
62         <tr><td>rcvbuf()</td>     <td>BufferingPolicy::sndbuf (\ref senf::BufferingPolicyBase)</td></tr>
63         <tr><td>sndbuf()</td>     <td>BufferingPolicy::rcvbuf (\ref senf::BufferingPolicyBase)</td></tr>
64         </table>
65
66         It is important to note, that not all members are always
67         accessible. Which are depends on the \c Policy template
68         argument. If any of the policy axis is left unspecified the
69         corresponding members will not be callable (you will get a
70         compile time error). Even if every policy axis is defined,
71         some members might (and will) not exist depending on the exact
72         policy. To find out, which members are available, you have to
73         check the documentation of the policy classes. You can also
74         find a summary of all active members in the leaf protocol
75         class documentation.
76
77         \todo Move all not template-parameter dependent code into a
78         non-template base class
79
80         \idea Give SocketHandle (and therefore ClientSocketHandle and
81         ServerSocketHandle) a \c protocol() template member and an
82         additional template arg \c Policies. This arg should be a
83         typelist of Poclicy classes which can be accessed. You use
84         protocol<ProtocolClass>() to access a protocol class. \c
85         Policies can of course be underspecified or even empty.
86
87         \idea add more flexible read/write members for a)
88         boost::arrays and arrays of other types b) std::vector (which
89         uses contiguous memory ..) c) other random-access containers
90         (we should use some configurable trait class to identify
91         containers with contiguous storage). Probably we should just
92         use a generic Boost.Range interface. Here we again come to the
93         point: make all except the most basic members be non-member
94         algorithms ? this would make the configuration of such
95         extenden members more flexible.
96
97         \see \ref policy_group
98              \ref protocol_group
99       */
100     template <class Policy>
101     class ClientSocketHandle
102         : public SocketHandle<Policy>
103     {
104     public:
105         ///////////////////////////////////////////////////////////////////////////
106         // Types
107
108         typedef typename Policy::AddressingPolicy::Address Address;
109         typedef typename boost::call_traits<Address>::param_type AddressParam;
110         typedef ServerSocketHandle<Policy> ServerSocketHandle;
111
112         ///////////////////////////////////////////////////////////////////////////
113         ///\name Structors and default members
114         ///@{
115
116         // no default constructor
117         // default copy constructor
118         // default copy assignment
119         // default destructor
120
121         // conversion constructors
122         template <class OtherPolicy>
123         ClientSocketHandle(ClientSocketHandle<OtherPolicy> other,
124                            typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type * = 0);
125
126         template <class OtherPolicy>
127         typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type const & 
128         operator=(ClientSocketHandle<OtherPolicy> other);
129
130         ///@}
131         ///////////////////////////////////////////////////////////////////////////
132
133         ///////////////////////////////////////////////////////////////////////////
134         ///\name Reading and Writing
135         ///@{
136
137         // read from socket (connected or unconnected)
138         std::string  read         (unsigned limit=0);
139         void         read         (std::string & buffer, unsigned limit=0);
140         unsigned     read         (char * buffer, unsigned size);
141
142         // read from unconnected socket returning peer address
143         std::pair<std::string, Address> 
144                      readfrom     ();
145         void         readfrom     (std::string & buffer, Address & from);
146         unsigned     readfrom     (char * buffer, unsigned size, Address & from);
147
148         // write to connected socket
149         unsigned     write        (std::string const & data);
150         unsigned     write        (char const * buffer, unsigned size);
151
152         // write to unconnected socket
153         unsigned     writeto      (AddressParam addr, std::string const & data);
154         unsigned     writeto      (AddressParam addr, char const * buffer, unsigned size);
155
156         ///@}
157
158         ///////////////////////////////////////////////////////////////////////////
159         ///\name Addressing
160         ///@{
161
162         void         connect      (AddressParam addr);
163         void         bind         (AddressParam addr);
164
165         Address      peer         ();
166         void         peer         (Address & addr);
167         Address      local        ();
168         void         local        (Address & addr);
169
170         ///@}
171
172         ///////////////////////////////////////////////////////////////////////////
173         ///\name Buffering
174         ///@{
175         
176         unsigned     rcvbuf      ();
177         void         rcvbuf      (unsigned size);        
178         unsigned     sndbuf      ();
179         void         sndbuf      (unsigned size);
180         
181         ///@}
182         
183         static ClientSocketHandle cast_static(FileHandle handle);
184         static ClientSocketHandle cast_dynamic(FileHandle handle);
185
186         // we need to override both since SocketHandle is *not* polymorphic
187         void state(SocketStateMap & map, unsigned lod=0);
188         std::string dumpState(unsigned lod=0);
189
190     protected:
191         ClientSocketHandle(FileHandle other, bool isChecked);
192         explicit ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol,
193                                     int fd = -1);
194
195     private:
196         unsigned available();
197
198         friend class senf::ServerSocketHandle<Policy>;
199     };
200
201     /// @}
202 }
203
204 ///////////////////////////////hh.e////////////////////////////////////////
205 //#include "ClientSocketHandle.cci"
206 #include "ClientSocketHandle.ct"
207 #include "ClientSocketHandle.cti"
208 #endif
209
210 \f
211 // Local Variables:
212 // mode: c++
213 // c-file-style: "senf"
214 // End: