ACE_Select_Reactor

前端之家收集整理的这篇文章主要介绍了ACE_Select_Reactor前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. template <class ACE_SELECT_REACTOR_TOKEN> int
  2. ACE_Select_Reactor_T<ACE_SELECT_REACTOR_TOKEN>::register_handler
  3. (ACE_Event_Handler *handler,ACE_Reactor_Mask mask)
  4. {
  5. ACE_TRACE ("ACE_Select_Reactor_T::register_handler");
  6. ACE_MT (ACE_GUARD_RETURN (ACE_SELECT_REACTOR_TOKEN,ace_mon,this->token_,-1));
  7. return this->register_handler_i (handler->get_handle (),handler,mask);
  8. }
  1. template <class ACE_SELECT_REACTOR_TOKEN> int
  2. ACE_Select_Reactor_T<ACE_SELECT_REACTOR_TOKEN>::register_handler_i
  3. (ACE_HANDLE handle,ACE_Event_Handler *event_handler,ACE_Reactor_Mask mask)
  4. {
  5. ACE_TRACE ("ACE_Select_Reactor_T::register_handler_i");
  6.  
  7. // Insert the <handle,event_handle> tuple into the Handler
  8. // Repository.
  9. return this->handler_rep_.bind (handle,event_handler,mask);
  10. }

  1. class ACE_Select_Reactor_Impl
  2. {
  3. ...
  4. /// Table that maps <ACE_HANDLEs> to <ACE_Event_Handler *>'s.
  5. ACE_Select_Reactor_Handler_Repository handler_rep_;
  6. ...
  7. };

  1. class ACE_Export ACE_Select_Reactor_Handler_Repository
  2. {
  3. public:
  4. friend class ACE_Select_Reactor_Handler_Repository_Iterator;
  5.  
  6. typedef ACE_HANDLE key_type;
  7. typedef ACE_Event_Handler * value_type;
  8.  
  9. // = The mapping from <HANDLES> to <Event_Handlers>.
  10. #ifdef ACE_WIN32
  11. /**
  12. * The NT version implements this via a hash map
  13. * @c ACE_Event_Handler*. Since NT implements @c ACE_HANDLE
  14. * as a void * we can't directly index into this array. Therefore,* we must explicitly map @c ACE_HANDLE to @c ACE_Event_Handler.
  15. */
  16. typedef ACE_Hash_Map_Manager_Ex<key_type,value_type,ACE_Hash<key_type>,std::equal_to<key_type>,ACE_Null_Mutex> map_type;
  17.  
  18. typedef map_type::size_type max_handlep1_type;
  19. #else
  20. /**
  21. * The UNIX version implements this via a dynamically allocated
  22. * array of @c ACE_Event_Handler* that is indexed directly using
  23. * the @c ACE_HANDLE value.
  24. */
  25. typedef ACE_Array_Base<value_type> map_type;
  26. typedef ACE_HANDLE max_handlep1_type;
  27. #endif /* ACE_WIN32 */
  28.  
  29. typedef map_type::size_type size_type;
  30.  
  31. // = Initialization and termination methods.
  32. /// Default "do-nothing" constructor.
  33. ACE_Select_Reactor_Handler_Repository (ACE_Select_Reactor_Impl &);
  34.  
  35. /// Initialize a repository of the appropriate @a size.
  36. /**
  37. * On Unix platforms,the size parameter should be as large as the
  38. * maximum number of file descriptors allowed for a given process.
  39. * This is necessary since a file descriptor is used to directly
  40. * index the array of event handlers maintained by the Reactor's
  41. * handler repository. Direct indexing is used for efficiency
  42. * reasons.
  43. */
  44. int open (size_type size);
  45.  
  46. /// Close down the repository.
  47. int close (void);
  48.  
  49. // = Search structure operations.
  50.  
  51. /**
  52. * Return the @c ACE_Event_Handler* associated with @c ACE_HANDLE.
  53. */
  54. ACE_Event_Handler * find (ACE_HANDLE handle);
  55.  
  56. /// Bind the ACE_Event_Handler * to the ACE_HANDLE with the
  57. /// appropriate ACE_Reactor_Mask settings.
  58. int bind (ACE_HANDLE,ACE_Event_Handler *,ACE_Reactor_Mask);
  59.  
  60. /// Remove the binding of ACE_HANDLE in accordance with the @a mask.
  61. int unbind (ACE_HANDLE,ACE_Reactor_Mask mask);
  62.  
  63. /// Remove all the <ACE_HANDLE,ACE_Event_Handler> tuples.
  64. int unbind_all (void);
  65.  
  66. // = Sanity checking.
  67.  
  68. // Check the @a handle to make sure it's a valid @c ACE_HANDLE that
  69. // is within the range of legal handles (i.e.,>= 0 && < max_size_).
  70. bool invalid_handle (ACE_HANDLE handle);
  71.  
  72. // Check the @a handle to make sure it's a valid @c ACE_HANDLE that
  73. // within the range of currently registered handles (i.e.,>= 0 && <
  74. // @c max_handlep1_).
  75. bool handle_in_range (ACE_HANDLE handle);
  76.  
  77. // = Accessors.
  78. /// Returns the current table size.
  79. size_type size (void) const;
  80.  
  81. /// Maximum ACE_HANDLE value,plus 1.
  82. max_handlep1_type max_handlep1 (void) const;
  83.  
  84. /// Dump the state of an object.
  85. void dump (void) const;
  86.  
  87. /// Declare the dynamic allocation hooks.
  88. ACE_ALLOC_HOOK_DECLARE;
  89.  
  90. private:
  91.  
  92. /// Remove the binding of @a handle corresponding to position @a pos
  93. /// in accordance with the @a mask.
  94. int unbind (ACE_HANDLE handle,map_type::iterator pos,ACE_Reactor_Mask mask);
  95.  
  96. /**
  97. * @return @c iterator corresponding @c ACE_Event_Handler*
  98. * associated with @c ACE_HANDLE.
  99. */
  100. map_type::iterator find_eh (ACE_HANDLE handle);
  101.  
  102. private:
  103. /// Reference to our @c Select_Reactor.
  104. ACE_Select_Reactor_Impl &select_reactor_;
  105.  
  106. #ifndef ACE_WIN32
  107. /// The highest currently active handle,plus 1 (ranges between 0 and
  108. /// @c max_size_.
  109. max_handlep1_type max_handlep1_;
  110. #endif /* !ACE_WIN32 */
  111.  
  112. /// Underlying table of event handlers.
  113. map_type event_handlers_;
  114. };
  1. // Bind the <ACE_Event_Handler *> to the <ACE_HANDLE>.
  2. int
  3. ACE_Select_Reactor_Handler_Repository::bind (ACE_HANDLE handle,ACE_Reactor_Mask mask)
  4. {
  5. ACE_TRACE ("ACE_Select_Reactor_Handler_Repository::bind");
  6.  
  7. if (event_handler == 0)
  8. return -1;
  9.  
  10. if (handle == ACE_INVALID_HANDLE)
  11. handle = event_handler->get_handle ();
  12.  
  13. if (this->invalid_handle (handle))
  14. return -1;
  15.  
  16. // Is this handle already in the Reactor?
  17. bool existing_handle = false;
  18.  
  19. #if defined (ACE_WIN32)
  20.  
  21. map_type::ENTRY * entry = 0;
  22.  
  23. int const result =
  24. this->event_handlers_.bind (handle,entry);
  25.  
  26. if (result == -1)
  27. {
  28. return -1;
  29. }
  30. else if (result == 1) // Entry already exists.
  31. {
  32. // Cannot use a different handler for an existing handle.
  33. if (event_handler != entry->item ())
  34. {
  35. return -1;
  36. }
  37. else
  38. {
  39. // Remember that this handle is already registered in the
  40. // Reactor.
  41. existing_handle = true;
  42. }
  43. }
  44.  
  45. #else
  46.  
  47. // Check if this handle is already registered.
  48. ACE_Event_Handler * const current_handler =
  49. this->event_handlers_[handle];
  50.  
  51. if (current_handler)
  52. {
  53. // Cannot use a different handler for an existing handle.
  54. if (current_handler != event_handler)
  55. return -1;
  56.  
  57. // Remember that this handle is already registered in the
  58. // Reactor.
  59. existing_handle = true;
  60. }
  61.  
  62. this->event_handlers_[handle] = event_handler;
  63.  
  64. if (this->max_handlep1_ < handle + 1)
  65. this->max_handlep1_ = handle + 1;
  66.  
  67. #endif /* ACE_WIN32 */
  68.  
  69. if (this->select_reactor_.is_suspended_i (handle))
  70. {
  71. this->select_reactor_.bit_ops (handle,mask,this->select_reactor_.suspend_set_,ACE_Reactor::ADD_MASK);
  72. }
  73. else
  74. {
  75. this->select_reactor_.bit_ops (handle,this->select_reactor_.wait_set_,ACE_Reactor::ADD_MASK);
  76.  
  77. // Note the fact that we've changed the state of the <wait_set_>,// which is used by the dispatching loop to determine whether it can
  78. // keep going or if it needs to reconsult select().
  79. // this->select_reactor_.state_changed_ = 1;
  80. }
  81.  
  82. // If new entry,call add_reference() if needed.
  83. if (!existing_handle)
  84. event_handler->add_reference ();
  85.  
  86. return 0;
  87. }
  88.  
  89. // Remove the binding of <ACE_HANDLE>.
  90.  
  91. int
  92. ACE_Select_Reactor_Handler_Repository::unbind (
  93. ACE_HANDLE handle,ACE_Reactor_Mask mask)
  94. {
  95. ACE_TRACE ("ACE_Select_Reactor_Handler_Repository::unbind");
  96.  
  97. // Retrieve event handler before unbinding it from the map. The
  98. // iterator pointing to it will no longer be valid once the handler
  99. // is unbound.
  100. ACE_Event_Handler * const event_handler =
  101. (pos == this->event_handlers_.end ()
  102. ? 0
  103. : ACE_SELECT_REACTOR_EVENT_HANDLER (pos));
  104.  
  105. // Clear out the <mask> bits in the Select_Reactor's wait_set.
  106. this->select_reactor_.bit_ops (handle,ACE_Reactor::CLR_MASK);
  107.  
  108. // And suspend_set.
  109. this->select_reactor_.bit_ops (handle,ACE_Reactor::CLR_MASK);
  110.  
  111. // Note the fact that we've changed the state of the <wait_set_>,// which is used by the dispatching loop to determine whether it can
  112. // keep going or if it needs to reconsult select().
  113. // this->select_reactor_.state_changed_ = 1;
  114.  
  115. // If there are no longer any outstanding events on this <handle>
  116. // then we can totally shut down the Event_Handler.
  117.  
  118. bool const has_any_wait_mask =
  119. (this->select_reactor_.wait_set_.rd_mask_.is_set (handle)
  120. || this->select_reactor_.wait_set_.wr_mask_.is_set (handle)
  121. || this->select_reactor_.wait_set_.ex_mask_.is_set (handle));
  122. bool const has_any_suspend_mask =
  123. (this->select_reactor_.suspend_set_.rd_mask_.is_set (handle)
  124. || this->select_reactor_.suspend_set_.wr_mask_.is_set (handle)
  125. || this->select_reactor_.suspend_set_.ex_mask_.is_set (handle));
  126.  
  127. bool complete_removal = false;
  128.  
  129. if (!has_any_wait_mask && !has_any_suspend_mask)
  130. {
  131. #if defined (ACE_WIN32)
  132. if (event_handler != 0 && this->event_handlers_.unbind (pos) == -1)
  133. return -1; // Should not happen!
  134. #else
  135. this->event_handlers_[handle] = 0;
  136.  
  137. if (this->max_handlep1_ == handle + 1)
  138. {
  139. // We've deleted the last entry,so we need to figure out
  140. // the last valid place in the array that is worth looking
  141. // at.
  142. ACE_HANDLE const wait_rd_max =
  143. this->select_reactor_.wait_set_.rd_mask_.max_set ();
  144. ACE_HANDLE const wait_wr_max =
  145. this->select_reactor_.wait_set_.wr_mask_.max_set ();
  146. ACE_HANDLE const wait_ex_max =
  147. this->select_reactor_.wait_set_.ex_mask_.max_set ();
  148.  
  149. ACE_HANDLE const suspend_rd_max =
  150. this->select_reactor_.suspend_set_.rd_mask_.max_set ();
  151. ACE_HANDLE const suspend_wr_max =
  152. this->select_reactor_.suspend_set_.wr_mask_.max_set ();
  153. ACE_HANDLE const suspend_ex_max =
  154. this->select_reactor_.suspend_set_.ex_mask_.max_set ();
  155.  
  156. // Compute the maximum of six values.
  157. this->max_handlep1_ = wait_rd_max;
  158. if (this->max_handlep1_ < wait_wr_max)
  159. this->max_handlep1_ = wait_wr_max;
  160. if (this->max_handlep1_ < wait_ex_max)
  161. this->max_handlep1_ = wait_ex_max;
  162.  
  163. if (this->max_handlep1_ < suspend_rd_max)
  164. this->max_handlep1_ = suspend_rd_max;
  165. if (this->max_handlep1_ < suspend_wr_max)
  166. this->max_handlep1_ = suspend_wr_max;
  167. if (this->max_handlep1_ < suspend_ex_max)
  168. this->max_handlep1_ = suspend_ex_max;
  169.  
  170. ++this->max_handlep1_;
  171. }
  172.  
  173. #endif /* ACE_WIN32 */
  174.  
  175. // The handle has been completely removed.
  176. complete_removal = true;
  177. }
  178.  
  179. if (event_handler == 0)
  180. return -1;
  181.  
  182. bool const requires_reference_counting =
  183. event_handler->reference_counting_policy ().value () ==
  184. ACE_Event_Handler::Reference_Counting_Policy::ENABLED;
  185.  
  186. // Close down the <Event_Handler> unless we've been instructed not
  187. // to.
  188. if (ACE_BIT_ENABLED (mask,ACE_Event_Handler::DONT_CALL) == 0)
  189. (void) event_handler->handle_close (handle,mask);
  190.  
  191. // Call remove_reference() if the removal is complete and reference
  192. // counting is needed.
  193. if (complete_removal && requires_reference_counting)
  194. {
  195. (void) event_handler->remove_reference ();
  196. }
  197.  
  198. return 0;
  199. }

猜你在找的React相关文章