1 // $Id: SOCK_Acceptor.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/SOCK_Acceptor.h"
5 #include "ace/Log_Msg.h"
6 #include "ace/OS_Errno.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/OS_NS_sys_socket.h"
9 #include "ace/os_include/os_fcntl.h"
11 #if !defined (__ACE_INLINE__)
12 #include "ace/SOCK_Acceptor.inl"
13 #endif /* __ACE_INLINE__ */
15 #if !defined (ACE_HAS_WINCE)
16 #include "ace/OS_QoS.h"
17 #endif // ACE_HAS_WINCE
19 ACE_RCSID(ace
, SOCK_Acceptor
, "$Id: SOCK_Acceptor.cpp 80826 2008-03-04 14:51:23Z wotte $")
21 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
23 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Acceptor
)
25 // Do nothing routine for constructor.
27 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (void)
29 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
32 // Performs the timed accept operation.
35 ACE_SOCK_Acceptor::shared_accept_start (ACE_Time_Value
*timeout
,
37 int &in_blocking_mode
) const
39 ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_start");
41 ACE_HANDLE handle
= this->get_handle ();
43 // Handle the case where we're doing a timed <accept>.
46 if (ACE::handle_timed_accept (handle
,
52 in_blocking_mode
= ACE_BIT_DISABLED (ACE::get_flags (handle
),
54 // Set the handle into non-blocking mode if it's not already
57 && ACE::set_flags (handle
,
67 ACE_SOCK_Acceptor::shared_accept_finish (ACE_SOCK_Stream new_stream
,
69 int reset_new_handle
) const
71 ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_finish ()");
73 ACE_HANDLE new_handle
= new_stream
.get_handle ();
75 // Check to see if we were originally in blocking mode, and if so,
76 // set the <new_stream>'s handle and <this> handle to be in blocking
80 // Save/restore errno.
81 ACE_Errno_Guard
error (errno
);
83 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
85 ACE::clr_flags (this->get_handle (),
87 ACE::clr_flags (new_handle
,
91 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
93 // Reset the event association inherited by the new handle.
94 ::WSAEventSelect ((SOCKET
) new_handle
, 0, 0);
96 ACE_UNUSED_ARG (reset_new_handle
);
97 #endif /* ACE_WIN32 */
99 return new_handle
== ACE_INVALID_HANDLE
? -1 : 0;
102 // General purpose routine for accepting new connections.
105 ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream
&new_stream
,
106 ACE_Addr
*remote_addr
,
107 ACE_Time_Value
*timeout
,
109 int reset_new_handle
) const
111 ACE_TRACE ("ACE_SOCK_Acceptor::accept");
113 int in_blocking_mode
= 0;
114 if (this->shared_accept_start (timeout
,
116 in_blocking_mode
) == -1)
120 // On Win32 the third parameter to <accept> must be a NULL
121 // pointer if we want to ignore the client's address.
126 if (remote_addr
!= 0)
128 len
= remote_addr
->get_size ();
130 addr
= (sockaddr
*) remote_addr
->get_addr ();
134 new_stream
.set_handle (ACE_OS::accept (this->get_handle (),
137 while (new_stream
.get_handle () == ACE_INVALID_HANDLE
142 // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
144 if (new_stream
.get_handle () != ACE_INVALID_HANDLE
147 remote_addr
->set_size (len
);
149 remote_addr
->set_type (addr
->sa_family
);
153 return this->shared_accept_finish (new_stream
,
158 #if !defined (ACE_HAS_WINCE)
160 ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream
&new_stream
,
161 ACE_Accept_QoS_Params qos_params
,
162 ACE_Addr
*remote_addr
,
163 ACE_Time_Value
*timeout
,
165 int reset_new_handle
) const
167 ACE_TRACE ("ACE_SOCK_Acceptor::accept");
169 int in_blocking_mode
= 0;
170 if (this->shared_accept_start (timeout
,
172 in_blocking_mode
) == -1)
176 // On Win32 the third parameter to <accept> must be a NULL
177 // pointer if we want to ignore the client's address.
182 if (remote_addr
!= 0)
184 len
= remote_addr
->get_size ();
186 addr
= (sockaddr
*) remote_addr
->get_addr ();
190 new_stream
.set_handle (ACE_OS::accept (this->get_handle (),
194 while (new_stream
.get_handle () == ACE_INVALID_HANDLE
199 // Reset the size of the addr, which is only necessary for UNIX
201 if (new_stream
.get_handle () != ACE_INVALID_HANDLE
203 remote_addr
->set_size (len
);
206 return this->shared_accept_finish (new_stream
,
210 #endif // ACE_HAS_WINCE
213 ACE_SOCK_Acceptor::dump (void) const
215 #if defined (ACE_HAS_DUMP)
216 ACE_TRACE ("ACE_SOCK_Acceptor::dump");
217 #endif /* ACE_HAS_DUMP */
221 ACE_SOCK_Acceptor::shared_open (const ACE_Addr
&local_sap
,
225 ACE_TRACE ("ACE_SOCK_Acceptor::shared_open");
228 #if defined (ACE_HAS_IPV6)
229 if (protocol_family
== PF_INET6
)
231 sockaddr_in6 local_inet6_addr
;
232 ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr
),
234 sizeof local_inet6_addr
);
236 if (local_sap
== ACE_Addr::sap_any
)
238 local_inet6_addr
.sin6_family
= AF_INET6
;
239 local_inet6_addr
.sin6_port
= 0;
240 local_inet6_addr
.sin6_addr
= in6addr_any
;
243 local_inet6_addr
= *reinterpret_cast<sockaddr_in6
*> (local_sap
.get_addr ());
245 // We probably don't need a bind_port written here.
246 // There are currently no supported OS's that define
247 // ACE_LACKS_WILDCARD_BIND.
248 if (ACE_OS::bind (this->get_handle (),
249 reinterpret_cast<sockaddr
*> (&local_inet6_addr
),
250 sizeof local_inet6_addr
) == -1)
255 if (protocol_family
== PF_INET
)
257 sockaddr_in local_inet_addr
;
258 ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr
),
260 sizeof local_inet_addr
);
262 if (local_sap
== ACE_Addr::sap_any
)
264 local_inet_addr
.sin_port
= 0;
267 local_inet_addr
= *reinterpret_cast<sockaddr_in
*> (local_sap
.get_addr ());
268 if (local_inet_addr
.sin_port
== 0)
270 if (ACE::bind_port (this->get_handle (),
271 ACE_NTOHL (ACE_UINT32 (local_inet_addr
.sin_addr
.s_addr
))) == -1)
274 else if (ACE_OS::bind (this->get_handle (),
275 reinterpret_cast<sockaddr
*> (&local_inet_addr
),
276 sizeof local_inet_addr
) == -1)
279 else if (ACE_OS::bind (this->get_handle (),
280 (sockaddr
*) local_sap
.get_addr (),
281 local_sap
.get_size ()) == -1)
285 || ACE_OS::listen (this->get_handle (),
288 ACE_Errno_Guard
g (errno
); // Preserve across close() below.
293 return error
? -1 : 0;
297 ACE_SOCK_Acceptor::open (const ACE_Addr
&local_sap
,
298 ACE_Protocol_Info
*protocolinfo
,
306 ACE_TRACE ("ACE_SOCK_Acceptor::open");
308 if (protocol_family
== PF_UNSPEC
)
309 protocol_family
= local_sap
.get_type ();
311 if (ACE_SOCK::open (SOCK_STREAM
,
320 return this->shared_open (local_sap
,
325 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr
&local_sap
,
326 ACE_Protocol_Info
*protocolinfo
,
334 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
335 if (this->open (local_sap
,
343 ACE_ERROR ((LM_ERROR
,
345 ACE_TEXT ("ACE_SOCK_Acceptor")));
348 // General purpose routine for performing server ACE_SOCK creation.
351 ACE_SOCK_Acceptor::open (const ACE_Addr
&local_sap
,
357 ACE_TRACE ("ACE_SOCK_Acceptor::open");
359 if (local_sap
!= ACE_Addr::sap_any
)
360 protocol_family
= local_sap
.get_type ();
361 else if (protocol_family
== PF_UNSPEC
)
363 #if defined (ACE_HAS_IPV6)
364 protocol_family
= ACE::ipv6_enabled () ? PF_INET6
: PF_INET
;
366 protocol_family
= PF_INET
;
367 #endif /* ACE_HAS_IPV6 */
370 if (ACE_SOCK::open (SOCK_STREAM
,
376 return this->shared_open (local_sap
,
381 // General purpose routine for performing server ACE_SOCK creation.
383 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr
&local_sap
,
389 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
390 if (this->open (local_sap
,
395 ACE_ERROR ((LM_ERROR
,
397 ACE_TEXT ("ACE_SOCK_Acceptor")));
401 ACE_SOCK_Acceptor::close (void)
403 return ACE_SOCK::close ();
406 ACE_END_VERSIONED_NAMESPACE_DECL