[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / SOCK_Acceptor.cpp
blobf8ced09b78cd7a5c4b08e30419d870d759c34787
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.
34 int
35 ACE_SOCK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
36 int restart,
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>.
44 if (timeout != 0)
46 if (ACE::handle_timed_accept (handle,
47 timeout,
48 restart) == -1)
49 return -1;
50 else
52 in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
53 ACE_NONBLOCK);
54 // Set the handle into non-blocking mode if it's not already
55 // in it.
56 if (in_blocking_mode
57 && ACE::set_flags (handle,
58 ACE_NONBLOCK) == -1)
59 return -1;
63 return 0;
66 int
67 ACE_SOCK_Acceptor::shared_accept_finish (ACE_SOCK_Stream new_stream,
68 int in_blocking_mode,
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
77 // mode.
78 if (in_blocking_mode)
80 // Save/restore errno.
81 ACE_Errno_Guard error (errno);
83 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
84 // originally.
85 ACE::clr_flags (this->get_handle (),
86 ACE_NONBLOCK);
87 ACE::clr_flags (new_handle,
88 ACE_NONBLOCK);
91 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
92 if (reset_new_handle)
93 // Reset the event association inherited by the new handle.
94 ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
95 #else
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,
108 int restart,
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,
115 restart,
116 in_blocking_mode) == -1)
117 return -1;
118 else
120 // On Win32 the third parameter to <accept> must be a NULL
121 // pointer if we want to ignore the client's address.
122 int *len_ptr = 0;
123 sockaddr *addr = 0;
124 int len = 0;
126 if (remote_addr != 0)
128 len = remote_addr->get_size ();
129 len_ptr = &len;
130 addr = (sockaddr *) remote_addr->get_addr ();
134 new_stream.set_handle (ACE_OS::accept (this->get_handle (),
135 addr,
136 len_ptr));
137 while (new_stream.get_handle () == ACE_INVALID_HANDLE
138 && restart != 0
139 && errno == EINTR
140 && timeout == 0);
142 // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
143 // is known.
144 if (new_stream.get_handle () != ACE_INVALID_HANDLE
145 && remote_addr != 0)
147 remote_addr->set_size (len);
148 if (addr)
149 remote_addr->set_type (addr->sa_family);
153 return this->shared_accept_finish (new_stream,
154 in_blocking_mode,
155 reset_new_handle);
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,
164 int restart,
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,
171 restart,
172 in_blocking_mode) == -1)
173 return -1;
174 else
176 // On Win32 the third parameter to <accept> must be a NULL
177 // pointer if we want to ignore the client's address.
178 int *len_ptr = 0;
179 int len = 0;
180 sockaddr *addr = 0;
182 if (remote_addr != 0)
184 len = remote_addr->get_size ();
185 len_ptr = &len;
186 addr = (sockaddr *) remote_addr->get_addr ();
190 new_stream.set_handle (ACE_OS::accept (this->get_handle (),
191 addr,
192 len_ptr,
193 qos_params));
194 while (new_stream.get_handle () == ACE_INVALID_HANDLE
195 && restart != 0
196 && errno == EINTR
197 && timeout == 0);
199 // Reset the size of the addr, which is only necessary for UNIX
200 // domain sockets.
201 if (new_stream.get_handle () != ACE_INVALID_HANDLE
202 && remote_addr != 0)
203 remote_addr->set_size (len);
206 return this->shared_accept_finish (new_stream,
207 in_blocking_mode,
208 reset_new_handle);
210 #endif // ACE_HAS_WINCE
212 void
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,
222 int protocol_family,
223 int backlog)
225 ACE_TRACE ("ACE_SOCK_Acceptor::shared_open");
226 int error = 0;
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;
242 else
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)
251 error = 1;
253 else
254 #endif
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;
266 else
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)
272 error = 1;
274 else if (ACE_OS::bind (this->get_handle (),
275 reinterpret_cast<sockaddr *> (&local_inet_addr),
276 sizeof local_inet_addr) == -1)
277 error = 1;
279 else if (ACE_OS::bind (this->get_handle (),
280 (sockaddr *) local_sap.get_addr (),
281 local_sap.get_size ()) == -1)
282 error = 1;
284 if (error != 0
285 || ACE_OS::listen (this->get_handle (),
286 backlog) == -1)
288 ACE_Errno_Guard g (errno); // Preserve across close() below.
289 error = 1;
290 this->close ();
293 return error ? -1 : 0;
297 ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
298 ACE_Protocol_Info *protocolinfo,
299 ACE_SOCK_GROUP g,
300 u_long flags,
301 int reuse_addr,
302 int protocol_family,
303 int backlog,
304 int protocol)
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,
312 protocol_family,
313 protocol,
314 protocolinfo,
316 flags,
317 reuse_addr) == -1)
318 return -1;
319 else
320 return this->shared_open (local_sap,
321 protocol_family,
322 backlog);
325 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
326 ACE_Protocol_Info *protocolinfo,
327 ACE_SOCK_GROUP g,
328 u_long flags,
329 int reuse_addr,
330 int protocol_family,
331 int backlog,
332 int protocol)
334 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
335 if (this->open (local_sap,
336 protocolinfo,
338 flags,
339 reuse_addr,
340 protocol_family,
341 backlog,
342 protocol) == -1)
343 ACE_ERROR ((LM_ERROR,
344 ACE_TEXT ("%p\n"),
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,
352 int reuse_addr,
353 int protocol_family,
354 int backlog,
355 int protocol)
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;
365 #else
366 protocol_family = PF_INET;
367 #endif /* ACE_HAS_IPV6 */
370 if (ACE_SOCK::open (SOCK_STREAM,
371 protocol_family,
372 protocol,
373 reuse_addr) == -1)
374 return -1;
375 else
376 return this->shared_open (local_sap,
377 protocol_family,
378 backlog);
381 // General purpose routine for performing server ACE_SOCK creation.
383 ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
384 int reuse_addr,
385 int protocol_family,
386 int backlog,
387 int protocol)
389 ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
390 if (this->open (local_sap,
391 reuse_addr,
392 protocol_family,
393 backlog,
394 protocol) == -1)
395 ACE_ERROR ((LM_ERROR,
396 ACE_TEXT ("%p\n"),
397 ACE_TEXT ("ACE_SOCK_Acceptor")));
401 ACE_SOCK_Acceptor::close (void)
403 return ACE_SOCK::close ();
406 ACE_END_VERSIONED_NAMESPACE_DECL