1 // $Id:ServerSocketHandle.hh 218 2007-03-20 14:39:32Z tho $
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.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 ServerSocketHandle public header
27 #ifndef HH_SENF_Socket_ServerSocketHandle_
28 #define HH_SENF_Socket_ServerSocketHandle_ 1
31 #include <boost/static_assert.hpp>
32 #include <boost/call_traits.hpp>
33 #include "SocketHandle.hh"
34 #include "CommunicationPolicy.hh"
35 #include "AddressingPolicy.hh"
37 //#include "ServerSocketHandle.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
42 /// \addtogroup handle_group
45 template <class SPolicy> class ClientSocketHandle;
47 /** \brief Generic SocketHandle with server interface
49 This class provides the server side policy interface of the socket abstraction.
50 ServerSocketHandle defines the complete policy interface. It does not implement any
51 functionality itself however. All calls are forwarded to the following policy classes:
54 <tr><td>senf::ServerSocketHandle::bind</td> <td>AddressingPolicy::bind (\ref senf::AddressingPolicyBase)</td></tr>
55 <tr><td>senf::ServerSocketHandle::listen</td> <td>CommunicationPolicy::listen (\ref senf::CommunicationPolicyBase)</td></tr>
56 <tr><td>senf::ServerSocketHandle::local</td> <td>AddressingPolicy::local (\ref senf::AddressingPolicyBase)</td></tr>
57 <tr><td>senf::ServerSocketHandle::accept</td> <td>CommunicationPolicy::accept (\ref senf::CommunicationPolicyBase)</td></tr>
58 <tr><td>senf::ServerSocketHandle::acceptfrom</td> <td>CommunicationPolicy::accept (\ref senf::CommunicationPolicyBase)</td></tr>
61 A ServerSocketHandle is only meaningful for connection oriented addressable protocols
62 (CommunicationPolicy is ConnectedCommunicationPolicy and AddressingPolicy is not
65 It is important to note, that not all members are always accessible. Which are depends on
66 the \c Policy template argument. If any of the policy axis is left unspecified the
67 corresponding members will not be callable (you will get a compile time error). Even if
68 every policy axis is defined, some members might (and will) not exist if they are
69 meaningless for the protocol of the socket. This depends on the exact policy.
71 To find out, which members are available, you have to check the documentation of the policy
72 classes. You can also find a summary of all members available in the leaf protocol class
75 template <class SPolicy>
76 class ServerSocketHandle
77 : public SocketHandle<SPolicy>
80 ///////////////////////////////////////////////////////////////////////////
83 /// Address type from the addressing policy
84 typedef typename SPolicy::AddressingPolicy::Address Address;
85 /// 'Best' type for passing address as parameter
86 /** Depending on the type of \c Address, this will be either <tt>Address</tt> or <tt>Address
88 href="http://www.boost.org/libs/utility/call_traits.htm">call_traits documentation in
89 the Boost.Utility library.</a>
91 typedef typename boost::call_traits<Address>::param_type AddressParam;
92 /// Corresponding client socket handle with the same policy
93 typedef ClientSocketHandle<SPolicy> ClientHandle;
95 ///////////////////////////////////////////////////////////////////////////
96 ///\name Structors and default members
99 // default default constructor
100 // default copy constructor
101 // default copy assignment
102 // default destructor
105 ServerSocketHandle();
107 // conversion constructors
108 template <class OtherPolicy>
109 ServerSocketHandle(ServerSocketHandle<OtherPolicy> other,
110 typename SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type * = 0);
112 template <class OtherPolicy>
113 typename SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type const &
114 operator=(ServerSocketHandle<OtherPolicy> other);
117 ///////////////////////////////////////////////////////////////////////////
119 ///////////////////////////////////////////////////////////////////////////
120 ///\name Server socket interface
123 /** \brief Set local address
125 For addressable protocols (AddressingPolicy is not NoAddressingPolicy), bind() will set
126 the local address of the socket.
128 \param[in] addr Local socket address to assign
130 \throws senf::SystemException
132 void bind (AddressParam addr);
134 /** \brief Allow clients to connect to this server socket
136 \todo This is very protocol specific, I don't want it in the policy
137 interface. Especially the backlog argument seems quite protocol specific to
138 me. However, we cannot listen() before we bind() so listen() cannot reside in the
139 constructor. We need to find a good solution here.
141 \throws senf::SystemException
143 // Possible solution: Make listen() an abstract method of the protocol interface, make the
144 // backlog parameter into a member living in the body or protocol class and set it using
145 // some accessor. Hmm ... this all seems somehow futile ...
146 void listen (unsigned backlog=0);
148 /** \brief Query local address
150 This member will return the address of the local socket in addressable protocols
151 (AddressingPolicy is not NoAddressingPolicy).
153 There are two Variants of this member, one will return the address by value, the other
154 takes a reference argument to elide the copy operation.
156 \throws senf::SystemException
159 void local (Address & addr);
160 ///< Query local address
161 /**< \see \ref local() */
163 /** \brief Accept new connection
165 If the handle is non-blocking, accept will NOT block. If no connection is available to
166 be returned, accept will return a ClientSocketHandle which is not valid()
168 \throws senf::SystemException
172 \returns handle of new client connection
174 ClientHandle accept ();
175 std::pair<ClientHandle, Address>
176 acceptfrom (); ///< Accept new connection
177 /**< This variant will additionally return the remote
178 address of the client
179 \returns \c std::pair with client handle and client
181 \see \ref accept() */
182 ClientHandle acceptfrom (Address & addr);
183 ///< Accept new connection
184 /**< This variant will additionally return the remote
185 address of the client
186 \param[out] addr address
187 \returns handle of new client connection
188 \see \ref accept() */
192 static ServerSocketHandle cast_static(FileHandle handle);
193 static ServerSocketHandle cast_dynamic(FileHandle handle);
195 // we need to override both since SocketHandle is *not* polymorphic
196 void state(SocketStateMap & map, unsigned lod=0);
197 std::string dumpState(unsigned lod=0);
200 ServerSocketHandle(FileHandle other, bool isChecked);
201 explicit ServerSocketHandle(std::auto_ptr<SocketBody> body);
210 ///////////////////////////////hh.e////////////////////////////////////////
211 //#include "ServerSocketHandle.cci"
212 //#include "ServerSocketHandle.ct"
213 #include "ServerSocketHandle.cti"
220 // c-file-style: "senf"
221 // indent-tabs-mode: nil
222 // ispell-local-dictionary: "american"
223 // compile-command: "scons -u test"
224 // comment-column: 40