用ACE的Reactor模式实现网络通讯时,ACE内部用WSAEventSelect函数把网络事件与一个事件对象关联起来,目的是为了后面用WaitForMultipleObjects函数统一处理。

前端之家收集整理的这篇文章主要介绍了用ACE的Reactor模式实现网络通讯时,ACE内部用WSAEventSelect函数把网络事件与一个事件对象关联起来,目的是为了后面用WaitForMultipleObjects函数统一处理。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用ACE的Reactor模式实现网络通讯时,ACE内部用 WSAEventSelect 函数把网络事件与一个事件对象关联起来,目的是为了后面用WaitForMultipleObjects函数统一处理。

下面是ACE 的 ACE_WFMO_Reactor::register_handler_i 函数代码,里面调用了WSAEventSelect 函数

  1. int
  2. ACE_WFMO_Reactor::register_handler_i (ACE_HANDLE event_handle,ACE_HANDLE io_handle,ACE_Event_Handler *event_handler,ACE_Reactor_Mask new_masks)
  3. {
  4. // If this is a Winsock 1 system,the underlying event assignment will
  5. // not work,so don't try. Winsock 1 must use ACE_Select_Reactor for
  6. // reacting to socket activity.
  7.  
  8. #if !defined (ACE_HAS_WINSOCK2) || (ACE_HAS_WINSOCK2 == 0)
  9.  
  10. ACE_UNUSED_ARG (event_handle);
  11. ACE_UNUSED_ARG (io_handle);
  12. ACE_UNUSED_ARG (event_handler);
  13. ACE_UNUSED_ARG (new_masks);
  14. ACE_NOTSUP_RETURN (-1);
  15.  
  16. #else
  17.  
  18. // Make sure that the <handle> is valid
  19. if (io_handle == ACE_INVALID_HANDLE)
  20. io_handle = event_handler->get_handle ();
  21.  
  22. if (this->handler_rep_.invalid_handle (io_handle))
  23. {
  24. errno = ERROR_INVALID_HANDLE;
  25. return -1;
  26. }
  27.  
  28. long new_network_events = 0;
  29. bool delete_event = false;
  30. auto_ptr <ACE_Auto_Event> event;
  31.  
  32. // Look up the repository to see if the <event_handler> is already
  33. // there.
  34. ACE_Reactor_Mask old_masks;
  35. int found = this->handler_rep_.modify_network_events_i (io_handle,new_masks,old_masks,new_network_events,event_handle,delete_event,ACE_Reactor::ADD_MASK);
  36.  
  37. // Check to see if the user passed us a valid event; If not then we
  38. // need to create one
  39. if (event_handle == ACE_INVALID_HANDLE)
  40. {
  41. // Note: don't change this since some C++ compilers have
  42. // <auto_ptr>s that don't work properly...
  43. auto_ptr<ACE_Auto_Event> tmp (new ACE_Auto_Event);
  44. event = tmp;
  45. event_handle = event->handle ();
  46. delete_event = true;
  47. }
  48.  
  49. int result = ::WSAEventSelect ((SOCKET) io_handle,new_network_events);
  50. // If we had found the <Event_Handler> there is nothing more to do
  51. if (found)
  52. return result;
  53. else if (result != SOCKET_ERROR &&
  54. this->handler_rep_.bind_i (1,event_handler,io_handle,delete_event) != -1)
  55. {
  56. // The <event_handler> was not found in the repository,add to
  57. // the repository.
  58. if (delete_event)
  59. {
  60. // Clear out the handle in the ACE_Auto_Event so that when
  61. // it is destroyed,the handle isn't closed out from under
  62. // the reactor. After setting it,running down the event
  63. // (via auto_ptr<> event,above) at function return will
  64. // cause an error because it'll try to close an invalid handle.
  65. // To avoid that smashing the errno value,save the errno
  66. // here,explicitly remove the event so the dtor won't do it
  67. // again,then restore errno.
  68. ACE_Errno_Guard guard (errno);
  69. event->handle (ACE_INVALID_HANDLE);
  70. event->remove ();
  71. }
  72. return 0;
  73. }
  74. else
  75. return -1;
  76.  
  77. #endif /* ACE_HAS_WINSOCK2 || ACE_HAS_WINSOCK2 == 0 */
  78.  
  79. }

猜你在找的React相关文章