ACE_Event_Handler:事件響應入口


  1:ACE_Event_Handler類

  頭文件“Event_Handler.h”

  在ACE Reactor框架中,ACE_Event_Handler是所有事件處理器的基類。ACE_Event_Handler提供了一組事件處理的掛鈎方法,理解和掌握這些掛鈎方法的觸發條件和使用方法,是ACE Reactor編程裝B道路的重點。先看一下ACE_Event_Handler提供的關鍵方法:

  2:舉個栗子

  1:A從B那里下了訂單,要求B每天送一份貨物至A提供的地址。該過程類似於在ACE_Reactor::register_handler()上注冊一個事件,注冊的事件類型相當於收貨地址(Reactor會根據注冊的事件類型調用對應的掛鈎方法),register_handler的第一個參數是ACE_Event_Handler指針,第二個參數是注冊的事件類型,見下圖:

  register_handler注冊了哪個事件,當該類型事件發生時,ACE_Reactor就會調用對應的掛鈎方法,比如READ_MASK對應handle_input方法。沒有注冊的事件類型是不會觸發對應方法的。PS:ACE_Event_Handler::RWE_MASK等價於READ+WRITE+EXCEPT。

ACE_Reactor::instance()->register_handler(this,ACE_Event_Handler::RWE_MASK);

  2:僅有收貨地址是不夠的,還需要指定收貨人。由於ACE_Event_Handler是虛基類,我們需要在子類中提供收貨人實體。假設需要接收的物品為數據流,可以定義實體ACE_SOCK_Stream   m_Peer。將ACE_SOCK_Stream收貨人信息通知給某寶的方法是get_handle,使用方法如下:

ACE_HANDLE CClass::get_handle (void) const
{
    return m_Peer.get_handle ();
};

  3:某君A收到了貨物,試用了后覺得還不錯,B繼續送;或者通知B不要再送該類物品。

  對於情況1,在ACE_Event_Handler的handle_*()方法中返回大於等於0的int值。對於情況2,在handle_*()方法中返回-1,ACE_Reactor將會通知handle_close()方法進行后續處理,並清除掉步驟1中register_handler對應的mask值。handle_*()各類方法的原型函數如下:

/// Called when input events occur (e.g., connection or data).
  virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);

/// Called when output events are possible (e.g., when flow control
  /// abates or non-blocking connection completes).
  virtual int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);

/// Called when an exceptional events occur (e.g., SIGURG).
  virtual int handle_exception (ACE_HANDLE fd = ACE_INVALID_HANDLE);

/**
   * Called when timer expires.  @a current_time represents the current
   * time that the Event_Handler was selected for timeout
   * dispatching and @a act is the asynchronous completion token that
   * was passed in when <schedule_timer> was invoked.
   */
  virtual int handle_timeout (const ACE_Time_Value &current_time,
                              const void *act = 0);

  /// Called when a process exits.
  virtual int handle_exit (ACE_Process *);

/// Called when a handle_*() method returns -1 or when the
  /// remove_handler() method is called on an ACE_Reactor.  The
  /// @a close_mask indicates which event has triggered the
  /// handle_close() method callback on a particular @a handle.
  virtual int handle_close (ACE_HANDLE handle,
                            ACE_Reactor_Mask close_mask);

  /// Called when object is signaled by OS (either via UNIX signals or
  /// when a Win32 object becomes signaled).
  virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);

  ps1:handle_close()的返回值將會被忽略。

  ps2:handle_close()在兩種情況下被觸發:1,handle_*()方法返回-1;2,調用ACE_Reactor::remove_handler()。

  4:A從B那里解除合約訂單,調用ACE_Reactor::register_handler()相反的操作ACE_Reactor::remove_handler()。如果不希望remove_handler會觸發handle_close()方法,可以在mask值加上DONT_CALL標識位:

ACE_Reactor::instance()->remove_handler(this,ACE_Event_Handler::RWE_MASK | ACE_Event_Handler::DONT_CALL);

 讀書筆記:C++ Network Programming Volume2 Systematic Reuse with ACE and Frameworks


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM