903bc29f96b7ce9c865418d48445c2a2e6344fd9
[senf.git] / senf / Socket / ReadWritePolicy.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 ReadPolicy and WritePolicy public header
25
26     \todo ReadWritePolicy.test.cc
27  */
28
29 #ifndef HH_SENF_Socket_ReadWritePolicy_
30 #define HH_SENF_Socket_ReadWritePolicy_ 1
31
32 // Custom includes
33 #include "SocketPolicy.hh"
34 #include "ClientSocketHandle.hh"
35 #include "CommunicationPolicy.hh"
36
37 //#include "ReadWritePolicy.mpp"
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40
41 struct sockaddr;
42
43 namespace senf {
44
45     /// \addtogroup policy_impl_group
46     //\{
47
48     /** \brief ReadPolicy for readable sockets
49
50         This policy provides support for readable sockets via the standard UNIX read/recvfrom system
51         calls. The concrete semantics of the read calls depend on the framing policy of the socket.
52      */
53     struct ReadablePolicy : public ReadPolicyBase
54     {
55         static unsigned read(FileHandle & handle, char * buffer, unsigned size);
56                                         ///< read data from socket
57                                         /**< \param[in] handle socket handle to read from
58                                              \param[in] buffer address of buffer to write data to
59                                              \param[in] size size of buffer
60                                              \returns number of bytes read */
61 #       ifndef DOXYGEN
62         template <class SPolicy>
63         static unsigned readfrom(ClientSocketHandle<SPolicy> & handle, char * buffer, unsigned size,
64                                  typename SPolicy::AddressingPolicy::Address & address,
65                                  typename IfCommunicationPolicyIs<
66                                      SPolicy,UnconnectedCommunicationPolicy>::type * = 0);
67 #       else
68         template <class SPolicy>
69         static unsigned readfrom(ClientSocketHandle<SPolicy> & handle, char * buffer, unsigned size,
70                                  typename Policy::AddressingPolicy::Address & address);
71                                         ///< read data from socket returning peer address
72                                         /**< \param[in] handle socket handle to read from
73                                              \param[in] buffer address of buffer to write data to
74                                              \param[in] size size of buffer
75                                              \param[out] address peer address
76                                              \returns number of bytes read */
77 #       endif
78
79     private:
80         static unsigned do_readfrom(FileHandle & handle, char * buffer, unsigned size,
81                                     struct ::sockaddr * addr, socklen_t * len);
82     };
83
84     /** \brief ReadPolicy for unreadable sockets
85
86         This is different from UndefinedReadPolicy (which is the same as ReadPolicyBase). This
87         policy class defines the socket readability -- it explicitly states, that the socket does
88         not support reading.
89      */
90     struct NotReadablePolicy : public ReadPolicyBase
91     {};
92
93     /** \brief WritePolicy for writeable sockets
94
95         This policy provides support for writable sockets via the standard UNIX write/sendto system
96         calls. The concrete semantics of the write calls depend on the framing policy of the socket.
97      */
98     struct WriteablePolicy : public WritePolicyBase
99     {
100 #       ifndef DOXYGEN
101         template <class SPolicy>
102         static unsigned write(ClientSocketHandle<SPolicy> & handle, char const * buffer, unsigned size,
103                               typename IfCommunicationPolicyIs<
104                                   SPolicy,ConnectedCommunicationPolicy>::type * = 0);
105 #       else
106         template <class SPolicy>
107         static unsigned write(ClientSocketHandle<SPolicy> & handle, char const * buffer,
108                               unsigned size);
109                                         ///< write data to socket
110                                         /**< This member is only enabled if the socket uses
111                                              connected communication. Otherwise the communication
112                                              partner must be specified explicitly using the sendto
113                                              call
114
115                                              \param[in] handle socket handle to write data to
116                                              \param[in] buffer address of buffer to send
117                                              \param[in] size number of bytes to write
118                                              \returns number of bytes written */
119 #       endif
120 #       ifndef DOXYGEN
121         template <class SPolicy>
122         static unsigned writeto(ClientSocketHandle<SPolicy> & handle,
123                                 typename boost::call_traits<
124                                     typename SPolicy::AddressingPolicy::Address>::param_type addr,
125                                 char const * buffer, unsigned size,
126                                 typename IfCommunicationPolicyIs<
127                                     SPolicy,UnconnectedCommunicationPolicy>::type * = 0);
128 #       else
129         template <class SPolicy>
130         static unsigned writeto(ClientSocketHandle<SPolicy> & handle,
131                                 typename Policy::AddressingPolicy::Address const & addr,
132                                 char const * buffer, unsigned size);
133                                         ///< write data to socket sending to given peer
134                                         /**< This member is only enabled if the socket uses
135                                              unconnected communication. Otherwise no target may be
136                                              specified since it is implied in the connection.
137
138                                              \param[in] handle socket handle to write data to
139                                              \param[in] buffer address of buffer to send
140                                              \param[in] size number of bytes to write
141                                              \param[in] addr peer to send data to
142                                              \returns number of bytes written
143                                           */
144 #       endif
145
146     private:
147         static unsigned do_write(FileHandle & handle, char const * buffer, unsigned size);
148         static unsigned do_writeto(FileHandle & handle, char const * buffer, unsigned size,
149                                    struct sockaddr const * addr, socklen_t len);
150     };
151
152     /** \brief WritePolicy for unwriteable sockets
153
154         This is different from UndefinedWritePolicy (which is the same as WritePolicyBase). This
155         policy class defines the socket writeability -- it explicitly states, that the socket does
156         not support writing.
157      */
158     struct NotWriteablePolicy : public WritePolicyBase
159     {};
160
161     //\}
162
163 }
164
165
166 //-/////////////////////////////////////////////////////////////////////////////////////////////////
167 //#include "ReadWritePolicy.cci"
168 //#include "ReadWritePolicy.ct"
169 #include "ReadWritePolicy.cti"
170 #endif
171
172 \f
173 // Local Variables:
174 // mode: c++
175 // fill-column: 100
176 // c-file-style: "senf"
177 // indent-tabs-mode: nil
178 // ispell-local-dictionary: "american"
179 // compile-command: "scons -u test"
180 // comment-column: 40
181 // End: