第3月第13天 cpp模版 Iterator模式 proactor
1.模版除了传参,还可以自动创建。而传指针只是传参而已。
template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY = ACE_Default_Time_Policy> class ACE_Timer_List_T : public ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> { public: /// Type of iterator typedef ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> Iterator; /// Iterator is a friend friend class ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>; typedef ACE_Timer_Node_T<TYPE> Node; /// Type inherited from typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> Base_Timer_Queue; typedef ACE_Free_List<Node> FreeList; // = Initialization and termination methods. /** * Default constructor. @a upcall_functor is the instance of the * FUNCTOR to be used by the list. If @a upcall_functor is 0, a * default FUNCTOR will be created. @a freelist is the freelist of * timer nodes. If 0, then a default freelist will be created. */ ACE_Timer_List_T (FUNCTOR* upcall_functor = 0, FreeList* freelist = 0, TIME_POLICY const & time_policy = TIME_POLICY());
template<typename TYPE, typename FUNCTOR> class ACE_Timer_Queue_Upcall_Base : public ACE_Abstract_Timer_Queue<TYPE> , private ACE_Copy_Disabled { public: // Constructor explicit ACE_Timer_Queue_Upcall_Base(FUNCTOR * upcall_functor = 0); /// Destructor virtual ~ACE_Timer_Queue_Upcall_Base (void); /// Accessor to the upcall functor FUNCTOR & upcall_functor (void); protected: /// Upcall functor FUNCTOR *upcall_functor_; /// To delete or not to delete is the question? bool const delete_upcall_functor_; }; template <class TYPE, class FUNCTOR> ACE_INLINE ACE_Timer_Queue_Upcall_Base<TYPE, FUNCTOR>::ACE_Timer_Queue_Upcall_Base (FUNCTOR * upcall_functor) : ACE_Abstract_Timer_Queue<TYPE>() , ACE_Copy_Disabled() , upcall_functor_(upcall_functor) , delete_upcall_functor_ (upcall_functor == 0) { ACE_TRACE ("ACE_Timer_Queue_Upcall_Base::ACE_Timer_Queue_Upcall_Base"); if (upcall_functor != 0) { return; } ACE_NEW (upcall_functor_, FUNCTOR); }
2.Iterator模式
template <class TYPE> class ACE_Timer_Queue_Iterator_T { public: // = Initialization and termination methods. /// Constructor. ACE_Timer_Queue_Iterator_T (void); /// Destructor. virtual ~ACE_Timer_Queue_Iterator_T (void); /// Positions the iterator at the earliest node in the Timer Queue virtual void first (void) = 0; /// Positions the iterator at the next node in the Timer Queue virtual void next (void) = 0; /// Returns true when there are no more nodes in the sequence virtual bool isdone (void) const = 0; /// Returns the node at the current position in the sequence virtual ACE_Timer_Node_T<TYPE> *item (void) = 0; };
实现 ACE_Timer_Hash_T, ACE_Timer_Heap_T , ACE_Timer_List_T, ACE_Timer_Wheel_T
http://blog.csdn.net/u010700335/article/details/41214839
http://blogs.readthedocs.io/zh_CN/latest/Timer_mgr.html
3.proactor
1)
proactor关联了很多异步操作。Asynch_IO里的异步操作会调用proactor->create_asynch 。比如:ACE_Asynch_Accept::open => proactor->create_asynch_accept () => ACE_WIN32_Asynch_Accept
ACE_Asynch_Read_Stream::open => proactor->create_asynch_read_stream () => ACE_WIN32_Asynch_Read_Stream。
Receiver调用this->rs_.read (*mb, mb->size ()- 1) => ACE_Asynch_Read_Stream::read => ACE_WIN32_Asynch_Read_Stream::read。
有数据时proactor会调用 ACE_WIN32_Asynch_Read_Stream_Result::complete => handler->handle_read_stream (result)就回到Receiver。
ACE_Asynch_Read_Stream_Impl * ACE_Proactor::create_asynch_read_stream (void) { return this->implementation ()->create_asynch_read_stream (); } ACE_Asynch_Write_Stream_Impl * ACE_Proactor::create_asynch_write_stream (void) { return this->implementation ()->create_asynch_write_stream (); } ACE_Asynch_Read_Dgram_Impl * ACE_Proactor::create_asynch_read_dgram (void) { return this->implementation ()->create_asynch_read_dgram (); } ACE_Asynch_Write_Dgram_Impl * ACE_Proactor::create_asynch_write_dgram (void) { return this->implementation ()->create_asynch_write_dgram (); } ACE_Asynch_Read_File_Impl * ACE_Proactor::create_asynch_read_file (void) { return this->implementation ()->create_asynch_read_file (); } ACE_Asynch_Write_File_Impl * ACE_Proactor::create_asynch_write_file (void) { return this->implementation ()->create_asynch_write_file (); } ACE_Asynch_Accept_Impl * ACE_Proactor::create_asynch_accept (void) { return this->implementation ()->create_asynch_accept (); } ACE_Asynch_Connect_Impl * ACE_Proactor::create_asynch_connect (void) { return this->implementation ()->create_asynch_connect (); } ACE_Asynch_Transmit_File_Impl * ACE_Proactor::create_asynch_transmit_file (void) { return this->implementation ()->create_asynch_transmit_file (); }
2)ACE_Asynch_Acceptor
ACE_Asynch_Acceptor an example of the Acceptor Pattern.
proactor需要定义ACE_Asynch_Acceptor。ACE_Asynch_Acceptor包含asynch_accept_对象,asynch_accept_对象的open方法创建了implementation_,implementation_的win32实现ACE_WIN32_Asynch_Accept又定义了proactor。
ACE_WIN32_Asynch_Accept::accept会创建ACE_WIN32_Asynch_Accept_Result,ACE_WIN32_Asynch_Accept_Result继承ACE_WIN32_Asynch_Result。ACE_WIN32_Asynch_Result::post_completion 就会调用win32_proactor。ACE_WIN32_Proactor::handle_events就会得到ACE_WIN32_Asynch_Result了。
ACE_WIN32_Asynch_Result包含handler_,这个handler_就是ACE_Asynch_Acceptor对象。ACE_Asynch_Acceptor的handle_accept函数被调用时就会根据模板创建HANDLER *new_handler,然后调用open。
http://blog.csdn.net/ghosthjt/article/details/7624184
ACE_Asynch_Acceptor<Receiver> acceptor; int Rc = -1 ; if ( host == NULL ) // Acceptor { // Simplify , initial read with zero size Rc = acceptor.open (ACE_INET_Addr (port),0,1); }
void Receiver::open (ACE_HANDLE handle, ACE_Message_Block &message_block) { ...
template <class HANDLER> class ACE_Asynch_Acceptor : public ACE_Handler { public: /// A do nothing constructor. ACE_Asynch_Acceptor (void); /// Virtual destruction virtual ~ACE_Asynch_Acceptor (void); ... /// Asynch_Accept used to make life easier :-) ACE_Asynch_Accept asynch_accept_;
class ACE_Export ACE_WIN32_Asynch_Accept : public virtual ACE_Asynch_Accept_Impl, public ACE_WIN32_Asynch_Operation { public: /// Constructor. ACE_WIN32_Asynch_Accept (ACE_WIN32_Proactor *win32_proactor); /** * This starts off an asynchronous accept. The asynchronous accept * call also allows any initial data to be returned to the * <handler>. Upto <bytes_to_read> will be read and stored in the * <message_block>. The <accept_handle> will be used for the * <accept> call. If (<accept_handle> == INVALID_HANDLE), a new * handle will be created. * * <message_block> must be specified. This is because the address of * the new connection is placed at the end of this buffer. */ int accept (ACE_Message_Block &message_block, size_t bytes_to_read, ACE_HANDLE accept_handle, const void *act, int priority, int signal_number = 0); /// Destructor. ~ACE_WIN32_Asynch_Accept (void); // Methods belong to ACE_WIN32_Asynch_Operation base class. These // methods are defined here to avoid VC++ warnings. They route the // call to the ACE_WIN32_Asynch_Operation base class. /** * Initializes the factory with information which will be used with * each asynchronous call. If (<handle> == ACE_INVALID_HANDLE), * <ACE_Handler::handle> will be called on the <handler> to get the * correct handle. */ int open (ACE_Handler &handler, ACE_HANDLE handle, const void *completion_key, ACE_Proactor *proactor); /** * This cancels all pending accepts operations that were issued by * the calling thread. The function does not cancel asynchronous * operations issued by other threads. */ int cancel (void); /// Return the underlying proactor. ACE_Proactor* proactor (void) const; };
class ACE_Export ACE_Asynch_Accept : public ACE_Asynch_Operation { public: /// A do nothing constructor. ACE_Asynch_Accept (void); /// Destructor. virtual ~ACE_Asynch_Accept (void); /** * Initializes the factory with information which will be used with * each asynchronous call. If (<handle> == ACE_INVALID_HANDLE), * <ACE_Handler::handle> will be called on the <handler> to get the * correct handle. */ int open (ACE_Handler &handler, ACE_HANDLE handle = ACE_INVALID_HANDLE, const void *completion_key = 0, ACE_Proactor *proactor = 0); /** * This starts off an asynchronous accept. The asynchronous accept * call also allows any initial data to be returned to the * <handler>. Upto <bytes_to_read> will be read and stored in the * <message_block>. The <accept_handle> will be used for the * <accept> call. If (<accept_handle> == INVALID_HANDLE), a new * handle will be created. Priority of the * operation is specified by <priority>. On POSIX4-Unix, this is * supported. Works like <nice> in Unix. Negative values are not * allowed. 0 means priority of the operation same as the process * priority. 1 means priority of the operation is one less than * process. And so forth. On Win32, this is a no-op. * * <message_block> must be specified. This is because the address of * the new connection is placed at the end of this buffer. * <signal_number> is the POSIX4 real-time signal number to be used * for the operation. <signal_number> ranges from ACE_SIGRTMIN to * ACE_SIGRTMAX. This argument is a no-op on non-POSIX4 systems. */ int accept (ACE_Message_Block &message_block, size_t bytes_to_read, ACE_HANDLE accept_handle = ACE_INVALID_HANDLE, const void *act = 0, int priority = 0, int signal_number = ACE_SIGRTMIN); /// Return the underlying implementation class. // (this should be protected...) virtual ACE_Asynch_Operation_Impl *implementation (void) const; protected: /// Delegation/implementation class that all methods will be /// forwarded to. ACE_Asynch_Accept_Impl *implementation_;
int ACE_Asynch_Accept::open (ACE_Handler &handler, ACE_HANDLE handle, const void *completion_key, ACE_Proactor *proactor) { // Get a proactor for/from the user. proactor = this->get_proactor (proactor, handler); // Now let us get the implementation initialized. if ((this->implementation_ = proactor->create_asynch_accept ()) == 0) return -1; // Call the <open> method of the base class. return ACE_Asynch_Operation::open (handler, handle, completion_key, proactor); }