PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/dep/acelite/ace/SOCK_Acceptor.cpp

https://github.com/chucho/FaceCore
C++ | 406 lines | 325 code | 54 blank | 27 comment | 82 complexity | a21e9eae5075147e33a6d9d81c7f3577 MD5 | raw file
  1. // $Id: SOCK_Acceptor.cpp 91286 2010-08-05 09:04:31Z johnnyw $
  2. #include "ace/SOCK_Acceptor.h"
  3. #include "ace/Log_Msg.h"
  4. #include "ace/OS_Errno.h"
  5. #include "ace/OS_NS_string.h"
  6. #include "ace/OS_NS_sys_socket.h"
  7. #include "ace/os_include/os_fcntl.h"
  8. #if !defined (__ACE_INLINE__)
  9. #include "ace/SOCK_Acceptor.inl"
  10. #endif /* __ACE_INLINE__ */
  11. #if !defined (ACE_HAS_WINCE)
  12. #include "ace/OS_QoS.h"
  13. #endif // ACE_HAS_WINCE
  14. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  15. ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Acceptor)
  16. // Do nothing routine for constructor.
  17. ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (void)
  18. {
  19. ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
  20. }
  21. // Performs the timed accept operation.
  22. int
  23. ACE_SOCK_Acceptor::shared_accept_start (ACE_Time_Value *timeout,
  24. bool restart,
  25. int &in_blocking_mode) const
  26. {
  27. ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_start");
  28. ACE_HANDLE handle = this->get_handle ();
  29. // Handle the case where we're doing a timed <accept>.
  30. if (timeout != 0)
  31. {
  32. if (ACE::handle_timed_accept (handle,
  33. timeout,
  34. restart) == -1)
  35. return -1;
  36. else
  37. {
  38. in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
  39. ACE_NONBLOCK);
  40. // Set the handle into non-blocking mode if it's not already
  41. // in it.
  42. if (in_blocking_mode
  43. && ACE::set_flags (handle,
  44. ACE_NONBLOCK) == -1)
  45. return -1;
  46. }
  47. }
  48. return 0;
  49. }
  50. int
  51. ACE_SOCK_Acceptor::shared_accept_finish (ACE_SOCK_Stream new_stream,
  52. int in_blocking_mode,
  53. bool reset_new_handle) const
  54. {
  55. ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_finish ()");
  56. ACE_HANDLE new_handle = new_stream.get_handle ();
  57. // Check to see if we were originally in blocking mode, and if so,
  58. // set the <new_stream>'s handle and <this> handle to be in blocking
  59. // mode.
  60. if (in_blocking_mode)
  61. {
  62. // Save/restore errno.
  63. ACE_Errno_Guard error (errno);
  64. // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
  65. // originally.
  66. ACE::clr_flags (this->get_handle (),
  67. ACE_NONBLOCK);
  68. ACE::clr_flags (new_handle,
  69. ACE_NONBLOCK);
  70. }
  71. #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
  72. if (reset_new_handle)
  73. // Reset the event association inherited by the new handle.
  74. ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
  75. #else
  76. ACE_UNUSED_ARG (reset_new_handle);
  77. #endif /* ACE_WIN32 */
  78. return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
  79. }
  80. // General purpose routine for accepting new connections.
  81. int
  82. ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream &new_stream,
  83. ACE_Addr *remote_addr,
  84. ACE_Time_Value *timeout,
  85. bool restart,
  86. bool reset_new_handle) const
  87. {
  88. ACE_TRACE ("ACE_SOCK_Acceptor::accept");
  89. int in_blocking_mode = 0;
  90. if (this->shared_accept_start (timeout,
  91. restart,
  92. in_blocking_mode) == -1)
  93. return -1;
  94. else
  95. {
  96. // On Win32 the third parameter to <accept> must be a NULL
  97. // pointer if we want to ignore the client's address.
  98. int *len_ptr = 0;
  99. sockaddr *addr = 0;
  100. int len = 0;
  101. if (remote_addr != 0)
  102. {
  103. len = remote_addr->get_size ();
  104. len_ptr = &len;
  105. addr = (sockaddr *) remote_addr->get_addr ();
  106. }
  107. do
  108. new_stream.set_handle (ACE_OS::accept (this->get_handle (),
  109. addr,
  110. len_ptr));
  111. while (new_stream.get_handle () == ACE_INVALID_HANDLE
  112. && restart
  113. && errno == EINTR
  114. && timeout == 0);
  115. // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
  116. // is known.
  117. if (new_stream.get_handle () != ACE_INVALID_HANDLE
  118. && remote_addr != 0)
  119. {
  120. remote_addr->set_size (len);
  121. if (addr)
  122. remote_addr->set_type (addr->sa_family);
  123. }
  124. }
  125. return this->shared_accept_finish (new_stream,
  126. in_blocking_mode,
  127. reset_new_handle);
  128. }
  129. #if !defined (ACE_HAS_WINCE)
  130. int
  131. ACE_SOCK_Acceptor::accept (ACE_SOCK_Stream &new_stream,
  132. ACE_Accept_QoS_Params qos_params,
  133. ACE_Addr *remote_addr,
  134. ACE_Time_Value *timeout,
  135. bool restart,
  136. bool reset_new_handle) const
  137. {
  138. ACE_TRACE ("ACE_SOCK_Acceptor::accept");
  139. int in_blocking_mode = 0;
  140. if (this->shared_accept_start (timeout,
  141. restart,
  142. in_blocking_mode) == -1)
  143. return -1;
  144. else
  145. {
  146. // On Win32 the third parameter to <accept> must be a NULL
  147. // pointer if we want to ignore the client's address.
  148. int *len_ptr = 0;
  149. int len = 0;
  150. sockaddr *addr = 0;
  151. if (remote_addr != 0)
  152. {
  153. len = remote_addr->get_size ();
  154. len_ptr = &len;
  155. addr = (sockaddr *) remote_addr->get_addr ();
  156. }
  157. do
  158. new_stream.set_handle (ACE_OS::accept (this->get_handle (),
  159. addr,
  160. len_ptr,
  161. qos_params));
  162. while (new_stream.get_handle () == ACE_INVALID_HANDLE
  163. && restart
  164. && errno == EINTR
  165. && timeout == 0);
  166. // Reset the size of the addr, which is only necessary for UNIX
  167. // domain sockets.
  168. if (new_stream.get_handle () != ACE_INVALID_HANDLE
  169. && remote_addr != 0)
  170. remote_addr->set_size (len);
  171. }
  172. return this->shared_accept_finish (new_stream,
  173. in_blocking_mode,
  174. reset_new_handle);
  175. }
  176. #endif // ACE_HAS_WINCE
  177. void
  178. ACE_SOCK_Acceptor::dump (void) const
  179. {
  180. #if defined (ACE_HAS_DUMP)
  181. ACE_TRACE ("ACE_SOCK_Acceptor::dump");
  182. #endif /* ACE_HAS_DUMP */
  183. }
  184. int
  185. ACE_SOCK_Acceptor::shared_open (const ACE_Addr &local_sap,
  186. int protocol_family,
  187. int backlog)
  188. {
  189. ACE_TRACE ("ACE_SOCK_Acceptor::shared_open");
  190. int error = 0;
  191. #if defined (ACE_HAS_IPV6)
  192. if (protocol_family == PF_INET6)
  193. {
  194. sockaddr_in6 local_inet6_addr;
  195. ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
  196. 0,
  197. sizeof local_inet6_addr);
  198. if (local_sap == ACE_Addr::sap_any)
  199. {
  200. local_inet6_addr.sin6_family = AF_INET6;
  201. local_inet6_addr.sin6_port = 0;
  202. local_inet6_addr.sin6_addr = in6addr_any;
  203. }
  204. else
  205. local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
  206. // We probably don't need a bind_port written here.
  207. // There are currently no supported OS's that define
  208. // ACE_LACKS_WILDCARD_BIND.
  209. if (ACE_OS::bind (this->get_handle (),
  210. reinterpret_cast<sockaddr *> (&local_inet6_addr),
  211. sizeof local_inet6_addr) == -1)
  212. error = 1;
  213. }
  214. else
  215. #endif
  216. if (protocol_family == PF_INET)
  217. {
  218. sockaddr_in local_inet_addr;
  219. ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
  220. 0,
  221. sizeof local_inet_addr);
  222. if (local_sap == ACE_Addr::sap_any)
  223. {
  224. local_inet_addr.sin_port = 0;
  225. }
  226. else
  227. local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
  228. if (local_inet_addr.sin_port == 0)
  229. {
  230. if (ACE::bind_port (this->get_handle (),
  231. ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
  232. error = 1;
  233. }
  234. else if (ACE_OS::bind (this->get_handle (),
  235. reinterpret_cast<sockaddr *> (&local_inet_addr),
  236. sizeof local_inet_addr) == -1)
  237. error = 1;
  238. }
  239. else if (ACE_OS::bind (this->get_handle (),
  240. (sockaddr *) local_sap.get_addr (),
  241. local_sap.get_size ()) == -1)
  242. error = 1;
  243. if (error != 0
  244. || ACE_OS::listen (this->get_handle (),
  245. backlog) == -1)
  246. {
  247. ACE_Errno_Guard g (errno); // Preserve across close() below.
  248. error = 1;
  249. this->close ();
  250. }
  251. return error ? -1 : 0;
  252. }
  253. int
  254. ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
  255. ACE_Protocol_Info *protocolinfo,
  256. ACE_SOCK_GROUP g,
  257. u_long flags,
  258. int reuse_addr,
  259. int protocol_family,
  260. int backlog,
  261. int protocol)
  262. {
  263. ACE_TRACE ("ACE_SOCK_Acceptor::open");
  264. if (protocol_family == PF_UNSPEC)
  265. protocol_family = local_sap.get_type ();
  266. if (ACE_SOCK::open (SOCK_STREAM,
  267. protocol_family,
  268. protocol,
  269. protocolinfo,
  270. g,
  271. flags,
  272. reuse_addr) == -1)
  273. return -1;
  274. else
  275. return this->shared_open (local_sap,
  276. protocol_family,
  277. backlog);
  278. }
  279. ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
  280. ACE_Protocol_Info *protocolinfo,
  281. ACE_SOCK_GROUP g,
  282. u_long flags,
  283. int reuse_addr,
  284. int protocol_family,
  285. int backlog,
  286. int protocol)
  287. {
  288. ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
  289. if (this->open (local_sap,
  290. protocolinfo,
  291. g,
  292. flags,
  293. reuse_addr,
  294. protocol_family,
  295. backlog,
  296. protocol) == -1)
  297. ACE_ERROR ((LM_ERROR,
  298. ACE_TEXT ("%p\n"),
  299. ACE_TEXT ("ACE_SOCK_Acceptor")));
  300. }
  301. // General purpose routine for performing server ACE_SOCK creation.
  302. int
  303. ACE_SOCK_Acceptor::open (const ACE_Addr &local_sap,
  304. int reuse_addr,
  305. int protocol_family,
  306. int backlog,
  307. int protocol)
  308. {
  309. ACE_TRACE ("ACE_SOCK_Acceptor::open");
  310. if (local_sap != ACE_Addr::sap_any)
  311. protocol_family = local_sap.get_type ();
  312. else if (protocol_family == PF_UNSPEC)
  313. {
  314. #if defined (ACE_HAS_IPV6)
  315. protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
  316. #else
  317. protocol_family = PF_INET;
  318. #endif /* ACE_HAS_IPV6 */
  319. }
  320. if (ACE_SOCK::open (SOCK_STREAM,
  321. protocol_family,
  322. protocol,
  323. reuse_addr) == -1)
  324. return -1;
  325. else
  326. return this->shared_open (local_sap,
  327. protocol_family,
  328. backlog);
  329. }
  330. // General purpose routine for performing server ACE_SOCK creation.
  331. ACE_SOCK_Acceptor::ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
  332. int reuse_addr,
  333. int protocol_family,
  334. int backlog,
  335. int protocol)
  336. {
  337. ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
  338. if (this->open (local_sap,
  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")));
  346. }
  347. int
  348. ACE_SOCK_Acceptor::close (void)
  349. {
  350. return ACE_SOCK::close ();
  351. }
  352. ACE_END_VERSIONED_NAMESPACE_DECL