WIN32_Proactor.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    WIN32_Proactor.h
00006  *
00007  *  WIN32_Proactor.h,v 4.32 2006/01/20 15:05:21 jwillemsen Exp
00008  *
00009  *  @author Irfan Pyarali (irfan@cs.wustl.edu)
00010  *  @author Tim Harrison (harrison@cs.wustl.edu)
00011  *  @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
00012  *  @author Roger Tragin <r.tragin@computer.org>
00013  *  @author Alexander Libman <alibman@ihug.com.au>
00014  */
00015 //=============================================================================
00016 
00017 #ifndef ACE_WIN32_PROACTOR_H
00018 #define ACE_WIN32_PROACTOR_H
00019 #include /**/ "ace/pre.h"
00020 
00021 #include "ace/config-all.h"
00022 
00023 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00024 #pragma once
00025 #endif /* ACE_LACKS_PRAGMA_ONCE */
00026 
00027 #if (defined (ACE_WIN32) && !defined (ACE_HAS_WINCE))
00028 // WIN32 implementation of the Proactor.
00029 
00030 #include "ace/WIN32_Asynch_IO.h"
00031 #include "ace/Event_Handler.h"
00032 
00033 #include "ace/Proactor_Impl.h"
00034 #include "ace/Asynch_Pseudo_Task.h"
00035 #include "ace/Auto_Event.h"
00036 
00037 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00038 
00039 // Forward declarations.
00040 class ACE_WIN32_Asynch_Result;
00041 class ACE_WIN32_Proactor_Timer_Handler;
00042 
00043 /**
00044  * @class ACE_WIN32_Proactor
00045  *
00046  * @brief A manager for asynchronous event demultiplexing on Win32.
00047  *
00048  * See the Proactor pattern description at
00049  * http://www.cs.wustl.edu/~schmidt/PDF/proactor.pdf for more
00050  * details.
00051  */
00052 class ACE_Export ACE_WIN32_Proactor : public ACE_Proactor_Impl
00053 {
00054   friend class ACE_WIN32_Asynch_Connect;
00055 
00056 public:
00057   /// A do nothing constructor.
00058   ACE_WIN32_Proactor (size_t number_of_threads = 0,
00059                       int used_with_reactor_event_loop = 0);
00060 
00061   /// Virtual destruction.
00062   virtual ~ACE_WIN32_Proactor (void);
00063 
00064   /// Close the IO completion port.
00065   virtual int close (void);
00066 
00067   /// This method adds the @a handle to the I/O completion port. This
00068   /// function is a no-op function for Unix systems.
00069   virtual int register_handle (ACE_HANDLE handle,
00070                                const void *completion_key);
00071 
00072   /**
00073    * Dispatch a single set of events.  If @a wait_time elapses before
00074    * any events occur, return 0.  Return 1 on success i.e., when a
00075    * completion is dispatched, non-zero (-1) on errors and errno is
00076    * set accordingly.
00077    */
00078   virtual int handle_events (ACE_Time_Value &wait_time);
00079 
00080   /**
00081    * Block indefinitely until at least one event is dispatched.
00082    * Dispatch a single set of events. Return 1 on success i.e., when a
00083    * completion is dispatched, non-zero (-1) on errors and errno is
00084    * set accordingly.
00085    */
00086   virtual int handle_events (void);
00087 
00088   /**
00089    * Post a result to the completion port of the Proactor.  If errors
00090    * occur, the result will be deleted by this method.  If successful,
00091    * the result will be deleted by the Proactor when the result is
00092    * removed from the completion port.  Therefore, the result should
00093    * have been dynamically allocated and should be orphaned by the
00094    * user once this method is called.
00095    */
00096   virtual int post_completion (ACE_WIN32_Asynch_Result *result);
00097 
00098   /// Add wakeup dispatch threads (reinit).
00099   int wake_up_dispatch_threads (void);
00100 
00101   /// Close all dispatch threads.
00102   int close_dispatch_threads (int wait);
00103 
00104   /// Get number of thread used as a parameter to @c CreateIoCompletionPort.
00105   size_t number_of_threads (void) const;
00106 
00107   /// Set number of thread used as a parameter to @c CreateIoCompletionPort.
00108   void number_of_threads (size_t threads);
00109 
00110   /// Get the event handle.
00111   virtual ACE_HANDLE get_handle (void) const;
00112 
00113   virtual ACE_Asynch_Read_Stream_Impl *create_asynch_read_stream (void);
00114   virtual ACE_Asynch_Write_Stream_Impl *create_asynch_write_stream (void);
00115   virtual ACE_Asynch_Read_File_Impl *create_asynch_read_file (void);
00116   virtual ACE_Asynch_Write_File_Impl *create_asynch_write_file (void);
00117   virtual ACE_Asynch_Read_Dgram_Impl *create_asynch_read_dgram (void);
00118   virtual ACE_Asynch_Write_Dgram_Impl *create_asynch_write_dgram (void);
00119   virtual ACE_Asynch_Accept_Impl *create_asynch_accept (void);
00120   virtual ACE_Asynch_Connect_Impl *create_asynch_connect (void);
00121   virtual ACE_Asynch_Transmit_File_Impl *create_asynch_transmit_file (void);
00122 
00123   // Methods used to create Asynch_IO_Result objects. We create the right
00124   // objects here in these methods.
00125 
00126   virtual ACE_Asynch_Read_Stream_Result_Impl *create_asynch_read_stream_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00127                                                                                 ACE_HANDLE handle,
00128                                                                                 ACE_Message_Block &message_block,
00129                                                                                 size_t bytes_to_read,
00130                                                                                 const void* act,
00131                                                                                 ACE_HANDLE event,
00132                                                                                 int priority,
00133                                                                                 int signal_number = 0);
00134 
00135   virtual ACE_Asynch_Write_Stream_Result_Impl *create_asynch_write_stream_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00136                                                                                   ACE_HANDLE handle,
00137                                                                                   ACE_Message_Block &message_block,
00138                                                                                   size_t bytes_to_write,
00139                                                                                   const void* act,
00140                                                                                   ACE_HANDLE event,
00141                                                                                   int priority,
00142                                                                                   int signal_number = 0);
00143 
00144   virtual ACE_Asynch_Read_File_Result_Impl *create_asynch_read_file_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00145                                                                             ACE_HANDLE handle,
00146                                                                             ACE_Message_Block &message_block,
00147                                                                             size_t bytes_to_read,
00148                                                                             const void* act,
00149                                                                             u_long offset,
00150                                                                             u_long offset_high,
00151                                                                             ACE_HANDLE event,
00152                                                                             int priority,
00153                                                                             int signal_number = 0);
00154 
00155   virtual ACE_Asynch_Write_File_Result_Impl *create_asynch_write_file_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00156                                                                               ACE_HANDLE handle,
00157                                                                               ACE_Message_Block &message_block,
00158                                                                               size_t bytes_to_write,
00159                                                                               const void* act,
00160                                                                               u_long offset,
00161                                                                               u_long offset_high,
00162                                                                               ACE_HANDLE event,
00163                                                                               int priority,
00164                                                                               int signal_number = 0);
00165 
00166   /// Create the correct implementation class for ACE_Asynch_Read_Dgram::Result.
00167   virtual ACE_Asynch_Read_Dgram_Result_Impl *create_asynch_read_dgram_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00168                                                                               ACE_HANDLE handle,
00169                                                                               ACE_Message_Block *message_block,
00170                                                                               size_t bytes_to_read,
00171                                                                               int flags,
00172                                                                               int protocol_family,
00173                                                                               const void* act,
00174                                                                               ACE_HANDLE event,
00175                                                                               int priority,
00176                                                                               int signal_number = 0);
00177 
00178   /// Create the correct implementation class for ACE_Asynch_Write_Dgram::Result.
00179   virtual ACE_Asynch_Write_Dgram_Result_Impl *create_asynch_write_dgram_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00180                                                                                 ACE_HANDLE handle,
00181                                                                                 ACE_Message_Block *message_block,
00182                                                                                 size_t bytes_to_write,
00183                                                                                 int flags,
00184                                                                                 const void* act,
00185                                                                                 ACE_HANDLE event,
00186                                                                                 int priority,
00187                                                                                 int signal_number = 0);
00188 
00189   virtual ACE_Asynch_Accept_Result_Impl *create_asynch_accept_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00190                                                                       ACE_HANDLE listen_handle,
00191                                                                       ACE_HANDLE accept_handle,
00192                                                                       ACE_Message_Block &message_block,
00193                                                                       size_t bytes_to_read,
00194                                                                       const void* act,
00195                                                                       ACE_HANDLE event,
00196                                                                       int priority,
00197                                                                       int signal_number = 0);
00198 
00199   virtual ACE_Asynch_Connect_Result_Impl *create_asynch_connect_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00200                                                                         ACE_HANDLE connect_handle,
00201                                                                         const void *act,
00202                                                                         ACE_HANDLE event,
00203                                                                         int priority,
00204                                                                         int signal_number = 0);
00205 
00206 
00207   virtual ACE_Asynch_Transmit_File_Result_Impl *create_asynch_transmit_file_result (const ACE_Handler::Proxy_Ptr &handler_proxy,
00208                                                                                     ACE_HANDLE socket,
00209                                                                                     ACE_HANDLE file,
00210                                                                                     ACE_Asynch_Transmit_File::Header_And_Trailer *header_and_trailer,
00211                                                                                     size_t bytes_to_write,
00212                                                                                     u_long offset,
00213                                                                                     u_long offset_high,
00214                                                                                     size_t bytes_per_send,
00215                                                                                     u_long flags,
00216                                                                                     const void *act,
00217                                                                                     ACE_HANDLE event,
00218                                                                                     int priority,
00219                                                                                     int signal_number = 0);
00220 
00221   /// Create a timer result object which can be used with the Timer
00222   /// mechanism of the Proactor.
00223   virtual ACE_Asynch_Result_Impl *create_asynch_timer (const ACE_Handler::Proxy_Ptr &handler_proxy,
00224                                                        const void *act,
00225                                                        const ACE_Time_Value &tv,
00226                                                        ACE_HANDLE event,
00227                                                        int priority,
00228                                                        int signal_number = 0);
00229 
00230 protected:
00231   /// Task to process pseudo-asynchronous operations
00232   ACE_Asynch_Pseudo_Task & get_asynch_pseudo_task (void);
00233 
00234   /// Called when object is signaled by OS (either via UNIX signals or
00235   /// when a Win32 object becomes signaled).
00236   virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);
00237 
00238   /// Called when object is removed from the ACE_Reactor.
00239   virtual int handle_close (ACE_HANDLE handle,
00240                             ACE_Reactor_Mask close_mask);
00241 
00242   /**
00243    * Dispatch a single set of events.  If @a milli_seconds elapses
00244    * before any events occur, return 0. Return 1 if a completion is
00245    * dispatched. Return -1 on errors.
00246    */
00247   virtual int handle_events (unsigned long milli_seconds);
00248 
00249   /// Protect against structured exceptions caused by user code when
00250   /// dispatching handles.
00251   void application_specific_code (ACE_WIN32_Asynch_Result *asynch_result,
00252                                   size_t bytes_transferred,
00253                                   const void *completion_key,
00254                                   u_long error);
00255 
00256   /**
00257    * Post @a how_many completions to the completion port so that all
00258    * threads can wake up. This is used in conjunction with the
00259    * run_event_loop().
00260    */
00261   virtual int post_wakeup_completions (int how_many);
00262 
00263   /// Handle for the completion port. Unix doesnt have completion
00264   /// ports.
00265   ACE_HANDLE completion_port_;
00266 
00267   /// This number is passed to the @c CreateIOCompletionPort system
00268   /// call.
00269   DWORD number_of_threads_;
00270 
00271   /// This event is used in conjunction with Reactor when we try to
00272  /// integrate the event loops of Reactor and the Proactor.
00273   ACE_Auto_Event event_;
00274 
00275   /// Flag that indicates whether we are used in conjunction with
00276   /// Reactor.
00277   int used_with_reactor_event_loop_;
00278 
00279   /// Handler to handle the wakeups. This works in conjunction with the
00280   /// ACE_Proactor::run_event_loop().
00281   ACE_Handler wakeup_handler_;
00282 
00283   /// Pseudo-task for asynch connect ( NT/2000)
00284   /// In future should removed in XP with ConnectEx support
00285   ACE_Asynch_Pseudo_Task pseudo_task_;
00286 };
00287 
00288 /**
00289  * @class ACE_WIN32_Asynch_Timer
00290  *
00291  * @brief This class is posted to the completion port when a timer
00292  * expires. When the complete method of this object is
00293  * called, the <handler>'s handle_timeout method will be
00294  * called.
00295  */
00296 class ACE_WIN32_Asynch_Timer : public ACE_WIN32_Asynch_Result
00297 {
00298   /// The factory method for this class is with the POSIX_Proactor
00299   /// class.
00300   friend class ACE_WIN32_Proactor;
00301 
00302 protected:
00303   /// Constructor.
00304   ACE_WIN32_Asynch_Timer (const ACE_Handler::Proxy_Ptr &handler_proxy,
00305                           const void *act,
00306                           const ACE_Time_Value &tv,
00307                           ACE_HANDLE event = ACE_INVALID_HANDLE,
00308                           int priority = 0,
00309                           int signal_number = 0);
00310 
00311   /// This method calls the <handler>'s handle_timeout method.
00312   virtual void complete (size_t bytes_transferred,
00313                          int success,
00314                          const void *completion_key,
00315                          u_long error = 0);
00316 
00317   /// Time value requested by caller
00318   ACE_Time_Value time_;
00319 };
00320 
00321 ACE_END_VERSIONED_NAMESPACE_DECL
00322 
00323 #endif /* ACE_WIN32 */
00324 #include /**/ "ace/post.h"
00325 #endif /* ACE_PROACTOR_H */

Generated on Thu Nov 9 09:42:10 2006 for ACE by doxygen 1.3.6