3ab6635f39f1652aa19776727b950d802794a1b9
[senf.git] / Socket / ReadWritePolicy.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 ReadPolicy and WritePolicy public header
25
26     \todo ReadWritePolicy.test.cc
27  */
28
29 #ifndef HH_ReadWritePolicy_
30 #define HH_ReadWritePolicy_ 1
31
32 // Custom includes
33 #include "SocketPolicy.hh"
34 #include "ClientSocketHandle.hh"
35 #include "CommunicationPolicy.hh"
36
37 //#include "ReadWritePolicy.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
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         template <class Policy>
62         static unsigned readfrom(ClientSocketHandle<Policy> handle, char * buffer, unsigned size,
63                                  typename Policy::AddressingPolicy::Address & address,
64                                  typename IfCommunicationPolicyIs<
65                                      Policy,UnconnectedCommunicationPolicy>::type * = 0);
66                                         ///< read data from socket returning peer address
67                                         /**< \param[in] handle socket handle to read from
68                                              \param[in] buffer address of buffer to write data to
69                                              \param[in] size size of buffer
70                                              \param[out] address peer address
71                                              \returns number of bytes read */
72
73     private:
74         static unsigned do_readfrom(FileHandle handle, char * buffer, unsigned size,
75                                     struct ::sockaddr * addr, socklen_t len);
76     };
77
78     /** \brief ReadPolicy for unreadable sockets
79
80         This is different from UndefinedReadPolicy (which is the same as ReadPolicyBase). This
81         policy class defines the socket readability -- it explicitly states, that the socket does
82         not support reading.
83      */
84     struct NotReadablePolicy : public ReadPolicyBase
85     {};
86
87     /** \brief WritePolicy for writeable sockets
88
89         This policy provides support for writable sockets via the standard UNIX write/sendto system
90         calls. The concrete semantics of the write calls depend on the framing policy of the socket.
91      */
92     struct WriteablePolicy : public WritePolicyBase
93     {
94         template <class Policy>
95         static unsigned write(ClientSocketHandle<Policy> handle, char const * buffer, unsigned size,
96                               typename IfCommunicationPolicyIs<
97                                   Policy,ConnectedCommunicationPolicy>::type * = 0);
98                                         ///< write data to socket
99                                         /**< This member is only enabled if the socket uses
100                                              connected communication. Otherwise the communication
101                                              partner must be specified explicitly using the sendto
102                                              call
103
104                                              \param[in] handle socket handle to write data to
105                                              \param[in] buffer address of buffer to send
106                                              \param[in] size number of bytes to write
107                                              \returns number of bytes written */
108         template <class Policy>
109         static unsigned writeto(ClientSocketHandle<Policy> handle,
110                                 typename boost::call_traits<
111                                     typename Policy::AddressingPolicy::Address>::param_type addr,
112                                 char const * buffer, unsigned size,
113                                 typename IfCommunicationPolicyIs<
114                                     Policy,UnconnectedCommunicationPolicy>::type * = 0);
115                                         ///< write data to socket sending to given peer
116                                         /**< This member is only enabled if the socket uses
117                                              unconnected communication. Otherwise no target may be
118                                              specified since it is implied in the connection.
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                                              \param[in] address peer to send data to
124                                              \returns number of bytes written
125                                           */
126
127     private:
128         static unsigned do_write(FileHandle handle, char const * buffer, unsigned size);
129         static unsigned do_writeto(FileHandle handle, char const * buffer, unsigned size,
130                                    struct sockaddr const * addr, socklen_t len);
131     };
132
133     /** \brief WritePolicy for unwriteable sockets
134
135         This is different from UndefinedWritePolicy (which is the same as WritePolicyBase). This
136         policy class defines the socket writeability -- it explicitly states, that the socket does
137         not support writing.
138      */
139     struct NotWriteablePolicy : public WritePolicyBase
140     {};
141
142     /// @}
143
144 }
145
146
147 ///////////////////////////////hh.e////////////////////////////////////////
148 //#include "ReadWritePolicy.cci"
149 //#include "ReadWritePolicy.ct"
150 #include "ReadWritePolicy.cti"
151 #endif
152
153 \f
154 // Local Variables:
155 // mode: c++
156 // fill-column: 100
157 // c-file-style: "senf"
158 // indent-tabs-mode: nil
159 // ispell-local-dictionary: "american"
160 // End: