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 public header
27 #ifndef HH_ClientSocketHandle_
28 #define HH_ClientSocketHandle_ 1
31 #include <boost/call_traits.hpp>
32 #include "SocketHandle.hh"
34 //#include "ClientSocketHandle.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
39 /// \addtogroup handle_group
42 template <class Policy> class ServerSocketHandle;
44 /** \brief Generic SocketHandle with client interface
46 This class provides the client side policy interface of the socket
47 abstraction. ClientSocketHandle defines the complete policy interface. It does not implement
48 any functionality itself however. All calls are forward to the following policy classes:
51 <tr><th>ClientSocketHandle member</th> <th>Policy member</th></tr>
52 <tr><td>read()</td> <td>ReadPolicy::read (\ref senf::ReadPolicyBase)</td></tr>
53 <tr><td>readfrom()</td> <td>ReadPolicy::readfrom (\ref senf::ReadPolicyBase)</td></tr>
54 <tr><td>write()</td> <td>WritePolicy::write (\ref senf::WritePolicyBase)</td></tr>
55 <tr><td>writeto()</td> <td>WritePolicy::writeto (\ref senf::WritePolicyBase)</td></tr>
56 <tr><td>connect()</td> <td>AddressingPolicy::connect (\ref senf::AddressingPolicyBase)</td></tr>
57 <tr><td>bind()</td> <td>AddressingPolicy::bind (\ref senf::AddressingPolicyBase)</td></tr>
58 <tr><td>peer()</td> <td>AddressingPolicy::peer (\ref senf::AddressingPolicyBase)</td></tr>
59 <tr><td>local()</td> <td>AddressingPolicy::local (\ref senf::AddressingPolicyBase)</td></tr>
60 <tr><td>rcvbuf()</td> <td>BufferingPolicy::sndbuf (\ref senf::BufferingPolicyBase)</td></tr>
61 <tr><td>sndbuf()</td> <td>BufferingPolicy::rcvbuf (\ref senf::BufferingPolicyBase)</td></tr>
64 It is important to note, that not all members are always accessible. Which are depends on
65 the \c Policy template argument. If any of the policy axis is left unspecified the
66 corresponding members will not be callable (you will get a compile time error). Even if
67 every policy axis is defined, some members might (and will) not exist if they are
68 meaningless for the protocol of the socket. This depends on the exact policy.
70 To find out, which members are available, you have to check the documentation of the policy
71 classes. You can also find a summary of all members available in the leaf protocol class
74 \todo Move all not template-parameter dependent code into a non-template base class
76 \idea Give SocketHandle (and therefore ClientSocketHandle and ServerSocketHandle) a \c
77 protocol() template member and an additional template arg \c Policies. This arg should be a
78 typelist of Poclicy classes which can be accessed. You use protocol<ProtocolClass>() to
79 access a protocol class. \c Policies can of course be underspecified or even empty.
81 \idea add more flexible read/write members for a) boost::arrays and arrays of other types b)
82 std::vector (which uses contiguous memory ..) c) other random-access containers (we should
83 use some configurable trait class to identify containers with contiguous storage). Probably
84 we should just use a generic Boost.Range interface. Here we again come to the point: make
85 all except the most basic members be non-member algorithms ? this would make the
86 configuration of such extenden members more flexible.
88 \see \ref policy_group \n
91 template <class Policy>
92 class ClientSocketHandle
93 : public SocketHandle<Policy>
96 ///////////////////////////////////////////////////////////////////////////
99 /// Address type from the addressing policy
100 typedef typename Policy::AddressingPolicy::Address Address;
101 /// 'Best' type for passing address as parameter
102 /** Depending on the type of \c Address, this will be either <tt>Address</tt> or <tt>Address
104 href="http://www.boost.org/libs/utility/call_traits.htm">call_traits documentation in
105 the Boost.Utility library.</a>
107 typedef typename boost::call_traits<Address>::param_type AddressParam;
108 /// Corresponding server socket handle with the same policy
109 /** This class will probably only be usable, if the \c CommunicationPolicy is \c
110 ConnectedCommunicationPolicy and the \c AddressingPolicy is not \c
111 NoAddressingPolicy. */
112 typedef ServerSocketHandle<Policy> ServerSocketHandle;
114 ///////////////////////////////////////////////////////////////////////////
115 ///\name Structors and default members
118 // no default constructor
119 // default copy constructor
120 // default copy assignment
121 // default destructor
123 // conversion constructors
124 template <class OtherPolicy>
125 ClientSocketHandle(ClientSocketHandle<OtherPolicy> other,
126 typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type * = 0);
128 template <class OtherPolicy>
129 typename SocketHandle<Policy>::template IsCompatible<OtherPolicy>::type const &
130 operator=(ClientSocketHandle<OtherPolicy> other);
133 ///////////////////////////////////////////////////////////////////////////
135 ///////////////////////////////////////////////////////////////////////////
136 ///\name Reading and Writing
139 /** \brief Read data from socket
141 If the sockets \c FramingPolicy is \c DatagramFramingPolicy, every read() command will
142 return a single datagram. If the sockets FramingPolicy is StreamFraming, the operation will
143 return as much data as possible from the socket buffer. However it cannot be guaranteed,
144 that the socket buffer will be empty after read() returns.
146 \attention If the space available for the data read is limited, the read will return no
147 more than that amount of data. For a datagram socket, a full datagram is still dequeued
148 from the socket buffer, the remainder of the datagram will be lost.
150 There are three variants of read which differ in how they return the read string.
152 \throws senf::SystemException
155 This variant will read up to \c limit bytes from the
156 socket and return them as a \c std::string object.
158 On a blocking socket, this member will \e always return some data (as long as the socket
159 has not been closed at the other end) and will block, if no data is available now. If
160 you do not want to block, you \e must make the socket non-blocking (using
161 FileHandle::blocking()).
163 \param[in] limit Maximum number of bytes to read or 0 if unlimited.
166 \implementation The read() family of members will use standard POSIX \c read calls, not
169 std::string read (unsigned limit=0);
170 void read (std::string & buffer, unsigned limit=0);
171 ///< Read data into string buffer
172 /**< On a blocking socket, this member will \e always return
173 some data (as long as the socket has not been closed at
174 the other end) and will block, if no data is available
175 now. If you do not want to block, you \e must make the
176 socket non-blocking (using FileHandle::blocking()).
177 \param[out] buffer data read
178 \param[in] limit Maximum number of bytes to read or 0
181 unsigned read (char * buffer, unsigned size);
182 ///< Read data into memory area
183 /**< This variant will read data into the memory area at \c
184 buffer of size \c size. This is the most performant
186 \param[in] buffer address of buffer to store data at
187 \param[in] size size of memory buffer
188 \returns Number of bytes read
191 /** \brief Read data from unconnected socket returning address
193 This member behaves like read() but should only be available, if the sockets \c
194 CommunicationPolicy is \c UnconnectedCommunicationPolicy and the \c AddressingPolicy is
195 not \c NoAddressingPolicy. The readfrom() family will in addition to the data return the
196 address of the sender.
198 \throws senf::SystemException
200 This variant will return the data read and the address as a std::pair.
202 \returns \c std::pair of data read (a string) and the peers address
204 \fixme Add \c limit argument
206 \implementation The readfrom() family of members will use \c recvfrom from the BSD
209 std::pair<std::string, Address>
211 void readfrom (std::string & buffer, Address & from);
212 ///< Read data into string buffer
213 /**< This variant will return the result in the locations
215 \param[out] buffer data read
216 \param[out] from peer address
217 \see \ref readfrom() */
218 unsigned readfrom (char * buffer, unsigned size, Address & from);
219 ///< Read data into memory buffer
220 /**< This variant will read data into the memory area at \c
221 buffer of size \c size. This is the most performant
222 version of readfrom().
223 \param[in] buffer address of buffer to store data at
224 \param[in] size size of buffer
225 \param[out] from peer address
226 \returns Number of bytes read
227 \see \ref readfrom() */
230 /** \brief Write data to socket
232 The write() family of members will write out the data to the socket. If the sockets \c
233 FramingPolicy is \c DatagramFramingPolicy, every write() call will result in one
236 A single write call might depending on the circumstances write only part of the data.
238 There are two variants of this member
240 \throws senf::SystemException
243 This variant will write out the string \c data.
245 \param[in] data Data to write
246 \returns number of bytes written
247 \implementation The write() family of members will use POSIX \c write calls, not \c
250 unsigned write (std::string const & data);
251 unsigned write (char const * buffer, unsigned size);
252 ///< Write data to socket from memory buffer
253 /**< \param[in] buffer address of buffer to write
254 \param[in] size amount of data to write
255 \returns Number of bytes written
258 /** \brief Write data to unconnected socket
260 This member behaves like write() but should only be available, if the sockets \c
261 CommunicationPolicy is \c UnconnectedCommunicationPolicy and the \c AddressingPolicy is
262 not \c NoAddressingPolicy. The writeto() family of members takes the target address as
263 an additional argument.
265 There are two variants of this member.
267 \throw senf::SystemException
270 This variant will send the string \c data to the peer \c addr.
272 \param[in] addr Address of peer to send data to
273 \param[in] data data to send
274 \returns Number of bytes written
276 unsigned writeto (AddressParam addr, std::string const & data);
277 unsigned writeto (AddressParam addr, char const * buffer, unsigned size);
278 ///< Write data from memory buffer to unconnected socket
279 /**< \param[in] addr Address of peer to send data to
280 \param[in] buffer address of buffer to write
281 \param[in] size amount of data to write
282 \returns Number of bytes written
283 \see \ref writeto() */
285 ///////////////////////////////////////////////////////////////////////////
289 /** \brief Connect to remote peer
291 This member will establish a connection for addressable connection-oriented protocols
292 (that is, the CommunicationPolicy is ConnectedCommunicationPolicy and the
293 AddressingPolicy is not NoAddressingPolicy).
295 \param[in] addr Address to connect to
297 \throws senf::SystemException
299 void connect (AddressParam addr);
301 /** \brief Set local address
303 For addressable protocols (AddressingPolicy is not NoAddressingPolicy), bind() will set
304 the local address of the socket.
306 \param[in] addr Local socket address to asign
308 \throws senf::SystemException
310 void bind (AddressParam addr);
312 /** \brief Query remote address
314 This member will return the address of the communication partner in addressable
315 connection-oriented protocols (that is, the CommunicationPolicy is
316 ConnectedCommunicationPolicy and the AddressingPolicy is not NoAddressingPolicy).
318 There are two Variants of this member, one will return the address by value, the other
319 takes a reference argument to elide the copy operation.
321 \throws senf::SystemException
324 void peer (Address & addr);
325 ///< Query remote address
326 /**< \see \ref peer() */
328 /** \brief Query local address
330 This member will return the address of the local socket in addressable protocols
331 (AddressingPolicy is not NoAddressingPolicy).
333 There are two Variants of this member, one will return the address by value, the other
334 takes a reference argument to elide the copy operation.
336 \throws senf::SystemException
339 void local (Address & addr);
340 ///< Query local address
341 /**< \see \ref local() */
345 ///////////////////////////////////////////////////////////////////////////
349 unsigned rcvbuf (); ///< Check size of receive buffer
350 /**< \returns size of receive buffer in bytes */
351 void rcvbuf (unsigned size);
352 ///< Set size of receive buffer
353 /**< \param[in] size size of receive buffer in bytes */
355 unsigned sndbuf (); ///< Check size of send buffer
356 /**< \returns size of send buffer in bytes */
357 void sndbuf (unsigned size);
358 ///< Set size of send buffer
359 /**< \param[in] size size of send buffer in bytes */
363 static ClientSocketHandle cast_static(FileHandle handle);
364 static ClientSocketHandle cast_dynamic(FileHandle handle);
366 // we need to override both since SocketHandle is *not* polymorphic
367 void state(SocketStateMap & map, unsigned lod=0);
368 std::string dumpState(unsigned lod=0);
371 ClientSocketHandle(FileHandle other, bool isChecked);
372 explicit ClientSocketHandle(std::auto_ptr<SocketProtocol> protocol,
376 unsigned available();
378 friend class senf::ServerSocketHandle<Policy>;
384 ///////////////////////////////hh.e////////////////////////////////////////
385 //#include "ClientSocketHandle.cci"
386 #include "ClientSocketHandle.ct"
387 #include "ClientSocketHandle.cti"
394 // c-file-style: "senf"
395 // indent-tabs-mode: nil
396 // ispell-local-dictionary: "american"