47163b05132c001f6a40c7f530425517eaadb588
[senf.git] / Socket / TCPSocketHandle.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 TCPv4SocketHandle and TCPv6SocketHandle public header
25
26     \todo Implement possibly non-blocking connect and SO_ERROR in the
27     protocol interface
28  */
29
30 #ifndef HH_TCPSocketHandle_
31 #define HH_TCPSocketHandle_ 1
32
33 // Custom includes
34 #include "INetProtocol.hh"
35 #include "TCPProtocol.hh"
36 #include "BSDSocketProtocol.hh"
37 #include "FramingPolicy.hh"
38 #include "CommunicationPolicy.hh"
39 #include "ReadWritePolicy.hh"
40 #include "BufferingPolicy.hh"
41 #include "ProtocolClientSocketHandle.hh"
42 #include "ProtocolServerSocketHandle.hh"
43
44 //#include "TCPSocketHandle.mpp"
45 ///////////////////////////////hh.p////////////////////////////////////////
46
47 namespace senf {
48
49     /// \addtogroup concrete_protocol_group
50     /// @{
51
52     typedef MakeSocketPolicy<
53         INet4AddressingPolicy,
54         StreamFramingPolicy,
55         ConnectedCommunicationPolicy,
56         ReadablePolicy,
57         WriteablePolicy,
58         SocketBufferingPolicy
59         >::policy TCPv4Socket_Policy;   ///< Socket Policy of the TCPv4 Protocol
60
61     /** \brief IPv4 TCP Socket Protocol
62         
63         \par Socket Handle typedefs:
64         \ref TCPv4ClientSocketHandle (ProtocolClientSocketHandle), \ref TCPv4ServerSocketHandle
65         (ProtocolServerSocketHandle)
66
67         \par Protocol Interface:
68         ClientSocketHandle::read(), ClientSocketHandle::write(), ClientSocketHandle::bind(),
69         ClientSocketHandle::local(), ClientSocketHandle::connect(), ClientSocketHandle::peer(),
70         ClientSocketHandle::rcvbuf(), ClientSocketHandle::sndbuf()
71
72         \par Address Type:
73         INet4Address
74         
75         TCPv4SocketProtocol provides an internet protocol stream socket based on the TCP protocol
76         and IPv4 addressing.
77
78         This class is utilized as the protocol class of the ProtocolClientSocketHandle and
79         ProtocolServerSocketHandle via the Socket Handle typedefs above.
80
81         \see TCPv6SocketProtocol
82      */
83     class TCPv4SocketProtocol
84         : public ConcreteSocketProtocol<TCPv4Socket_Policy>,
85           public IPv4Protocol, 
86           public TCPProtocol,
87           public BSDSocketProtocol,
88           public AddressableBSDSocketProtocol
89     {
90     public:
91         ///////////////////////////////////////////////////////////////////////////
92         // internal interface
93
94         ///\name Constructors
95         ///@{
96
97         void init_client() const;       ///< Create unconnected client socket
98                                         /**< \note This member is implicitly called from the
99                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
100                                              constructor */
101         void init_client(INet4Address const & address) const;
102                                         ///< Create client socket and connect
103                                         /**< Creates a new client socket and connects to the given
104                                              address. 
105                                              
106                                              \param[in] address remote address to connect to */
107                                         /**< \note This member is implicitly called from the
108                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
109                                              constructor */
110         void init_server() const;       ///< Create server socket
111                                         /**< \note This member is implicitly called from the
112                                              ProtocolServerSocketHandle::ProtocolServerSocketHandle()
113                                              constructor */
114         void init_server(INet4Address const & address, unsigned backlog=1) const;
115                                         ///< Create server socket and listen
116                                         /**< Creates a new server socket, binds to \a address end
117                                              starts listening for new connections with a backlog of
118                                              \a backlog connections. It also enables reuseaddr().
119
120                                              \param[in] address address to listen on
121                                              \param[in] backlog size of the listen backlog */
122                                         /**< \note This member is implicitly called from the
123                                              ProtocolServerSocketHandle::ProtocolServerSocketHandle()
124                                              constructor */
125
126         ///@}
127         ///\name Abstract Interface Implementation
128
129         std::auto_ptr<SocketProtocol> clone() const;
130         
131         ///@}
132     };
133
134     typedef ProtocolClientSocketHandle<TCPv4SocketProtocol> TCPv4ClientSocketHandle;
135     typedef ProtocolServerSocketHandle<TCPv4SocketProtocol> TCPv4ServerSocketHandle;
136
137     typedef MakeSocketPolicy<
138         TCPv4Socket_Policy,
139         INet6AddressingPolicy
140         >::policy TCPv6Socket_Policy;
141
142     /** \brief IPv6 TCP Socket Protocol
143         
144         \par Socket Handle typedefs:
145         \ref TCPv6ClientSocketHandle (ProtocolClientSocketHandle), \ref TCPv6ServerSocketHandle
146         (ProtocolServerSocketHandle)
147
148         \par Protocol Interface:
149         ClientSocketHandle::read(), ClientSocketHandle::write(), ClientSocketHandle::bind(),
150         ClientSocketHandle::local(), ClientSocketHandle::connect(), ClientSocketHandle::peer(),
151         ClientSocketHandle::rcvbuf(), ClientSocketHandle::sndbuf()
152
153         \par Address Type:
154         INet6Address
155         
156         TCPv6SocketProtocol provides an internet protocol stream socket based on the TCP protocol
157         and IPv6 addressing.
158
159         This class is utilized as the protocol class of the ProtocolClientSocketHandle and
160         ProtocolServerSocketHandle via the Socket Handle typedefs above.
161
162         \see TCPv4SocketProtocol
163      */
164     class TCPv6SocketProtocol
165         : public ConcreteSocketProtocol<TCPv6Socket_Policy>, 
166           public IPv6Protocol,
167           public TCPProtocol,
168           public BSDSocketProtocol,
169           public AddressableBSDSocketProtocol
170     {
171     public:
172         ///////////////////////////////////////////////////////////////////////////
173         // internal interface
174
175         ///\name Constructors
176         ///@{
177
178         void init_client() const;       ///< Create unconnected client socket
179                                         /**< \note This member is implicitly called from the
180                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
181                                              constructor */
182         void init_client(INet6SocketAddress const & address) const;
183                                         ///< Create client socket and connect
184                                         /**< Creates a new client socket and connects to the given
185                                              address. 
186                                              
187                                              \param[in] address remote address to connect to */
188                                         /**< \note This member is implicitly called from the
189                                              ProtocolClientSocketHandle::ProtocolClientSocketHandle()
190                                              constructor */
191         void init_server() const;       ///< Create server socket
192                                         /**< \note This member is implicitly called from the
193                                              ProtocolServerSocketHandle::ProtocolServerSocketHandle()
194                                              constructor */
195         void init_server(INet6SocketAddress const & address, unsigned backlog=1) const;
196                                         ///< Create server socket and listen
197                                         /**< Creates a new server socket, binds to \a address end
198                                              starts listening for new connections with a backlog of
199                                              \a backlog connections. It also enables reuseaddr().
200
201                                              \param[in] address address to listen on
202                                              \param[in] backlog size of the listen backlog */
203                                         /**< \note This member is implicitly called from the
204                                              ProtocolServerSocketHandle::ProtocolServerSocketHandle()
205                                              constructor */
206
207         ///@}
208         ///\name Abstract Interface Implementation
209
210         std::auto_ptr<SocketProtocol> clone() const;
211         
212         ///@}
213     };
214
215     typedef ProtocolClientSocketHandle<TCPv6SocketProtocol> TCPv6ClientSocketHandle;
216     typedef ProtocolServerSocketHandle<TCPv6SocketProtocol> TCPv6ServerSocketHandle;
217
218     /// @}
219
220 }
221
222 ///////////////////////////////hh.e////////////////////////////////////////
223 //#include "TCPSocketHandle.cci"
224 //#include "TCPSocketHandle.ct"
225 //#include "TCPSocketHandle.cti"
226 #endif
227
228 \f
229 // Local Variables:
230 // mode: c++
231 // c-file-style: "senf"
232 // fill-column: 100
233 // End: