switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / ReadWritePolicy.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ReadPolicy and WritePolicy public header
30
31     \todo ReadWritePolicy.test.cc
32  */
33
34 #ifndef HH_SENF_Socket_ReadWritePolicy_
35 #define HH_SENF_Socket_ReadWritePolicy_ 1
36
37 // Custom includes
38 #include "SocketPolicy.hh"
39 #include "ClientSocketHandle.hh"
40 #include "CommunicationPolicy.hh"
41
42 //#include "ReadWritePolicy.mpp"
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45
46 struct sockaddr;
47
48 namespace senf {
49
50     /// \addtogroup policy_impl_group
51     //\{
52
53     /** \brief ReadPolicy for readable sockets
54
55         This policy provides support for readable sockets via the standard UNIX read/recvfrom system
56         calls. The concrete semantics of the read calls depend on the framing policy of the socket.
57      */
58     struct ReadablePolicy : public ReadPolicyBase
59     {
60         static unsigned read(FileHandle & handle, char * buffer, unsigned size);
61                                         ///< read data from socket
62                                         /**< \param[in] handle socket handle to read from
63                                              \param[in] buffer address of buffer to write data to
64                                              \param[in] size size of buffer
65                                              \returns number of bytes read */
66 #       ifndef DOXYGEN
67         template <class SPolicy>
68         static unsigned readfrom(ClientSocketHandle<SPolicy> & handle, char * buffer, unsigned size,
69                                  typename SPolicy::AddressingPolicy::Address & address,
70                                  typename IfCommunicationPolicyIs<
71                                      SPolicy,UnconnectedCommunicationPolicy>::type * = 0);
72 #       else
73         template <class SPolicy>
74         static unsigned readfrom(ClientSocketHandle<SPolicy> & handle, char * buffer, unsigned size,
75                                  typename Policy::AddressingPolicy::Address & address);
76                                         ///< read data from socket returning peer address
77                                         /**< \param[in] handle socket handle to read from
78                                              \param[in] buffer address of buffer to write data to
79                                              \param[in] size size of buffer
80                                              \param[out] address peer address
81                                              \returns number of bytes read */
82 #       endif
83
84     private:
85         static unsigned do_readfrom(FileHandle & handle, char * buffer, unsigned size,
86                                     struct ::sockaddr * addr, socklen_t * len);
87     };
88
89     /** \brief ReadPolicy for unreadable sockets
90
91         This is different from UndefinedReadPolicy (which is the same as ReadPolicyBase). This
92         policy class defines the socket readability -- it explicitly states, that the socket does
93         not support reading.
94      */
95     struct NotReadablePolicy : public ReadPolicyBase
96     {};
97
98     /** \brief WritePolicy for writeable sockets
99
100         This policy provides support for writable sockets via the standard UNIX write/sendto system
101         calls. The concrete semantics of the write calls depend on the framing policy of the socket.
102      */
103     struct WriteablePolicy : public WritePolicyBase
104     {
105 #       ifndef DOXYGEN
106         template <class SPolicy>
107         static unsigned write(ClientSocketHandle<SPolicy> & handle, char const * buffer, unsigned size,
108                               typename IfCommunicationPolicyIs<
109                                   SPolicy,ConnectedCommunicationPolicy>::type * = 0);
110 #       else
111         template <class SPolicy>
112         static unsigned write(ClientSocketHandle<SPolicy> & handle, char const * buffer,
113                               unsigned size);
114                                         ///< write data to socket
115                                         /**< This member is only enabled if the socket uses
116                                              connected communication. Otherwise the communication
117                                              partner must be specified explicitly using the sendto
118                                              call
119
120                                              \param[in] handle socket handle to write data to
121                                              \param[in] buffer address of buffer to send
122                                              \param[in] size number of bytes to write
123                                              \returns number of bytes written */
124 #       endif
125 #       ifndef DOXYGEN
126         template <class SPolicy>
127         static unsigned writeto(ClientSocketHandle<SPolicy> & handle,
128                                 typename boost::call_traits<
129                                     typename SPolicy::AddressingPolicy::Address>::param_type addr,
130                                 char const * buffer, unsigned size,
131                                 typename IfCommunicationPolicyIs<
132                                     SPolicy,UnconnectedCommunicationPolicy>::type * = 0);
133 #       else
134         template <class SPolicy>
135         static unsigned writeto(ClientSocketHandle<SPolicy> & handle,
136                                 typename Policy::AddressingPolicy::Address const & addr,
137                                 char const * buffer, unsigned size);
138                                         ///< write data to socket sending to given peer
139                                         /**< This member is only enabled if the socket uses
140                                              unconnected communication. Otherwise no target may be
141                                              specified since it is implied in the connection.
142
143                                              \param[in] handle socket handle to write data to
144                                              \param[in] buffer address of buffer to send
145                                              \param[in] size number of bytes to write
146                                              \param[in] addr peer to send data to
147                                              \returns number of bytes written
148                                           */
149 #       endif
150
151     private:
152         static unsigned do_write(FileHandle & handle, char const * buffer, unsigned size);
153         static unsigned do_writeto(FileHandle & handle, char const * buffer, unsigned size,
154                                    struct sockaddr const * addr, socklen_t len);
155     };
156
157     /** \brief WritePolicy for unwriteable sockets
158
159         This is different from UndefinedWritePolicy (which is the same as WritePolicyBase). This
160         policy class defines the socket writeability -- it explicitly states, that the socket does
161         not support writing.
162      */
163     struct NotWriteablePolicy : public WritePolicyBase
164     {};
165
166     //\}
167
168 }
169
170
171 //-/////////////////////////////////////////////////////////////////////////////////////////////////
172 //#include "ReadWritePolicy.cci"
173 //#include "ReadWritePolicy.ct"
174 #include "ReadWritePolicy.cti"
175 #endif
176
177 \f
178 // Local Variables:
179 // mode: c++
180 // fill-column: 100
181 // c-file-style: "senf"
182 // indent-tabs-mode: nil
183 // ispell-local-dictionary: "american"
184 // compile-command: "scons -u test"
185 // comment-column: 40
186 // End: