00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file Asynch_Connector.h 00006 * 00007 * $Id: Asynch_Connector.h,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $ 00008 * 00009 * @author Alexander Libman <alibman@ihug.com.au> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_ASYNCH_CONNECTOR_H 00014 #define ACE_ASYNCH_CONNECTOR_H 00015 #include "ace/pre.h" 00016 00017 #include "ace/OS.h" 00018 00019 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00020 # pragma once 00021 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00022 00023 #if defined (ACE_WIN32) || defined (ACE_HAS_AIO_CALLS) 00024 // This only works on platforms that support async i/o. 00025 00026 #include "ace/Asynch_IO.h" 00027 #include "ace/INET_Addr.h" 00028 00029 // Forward declarations 00030 class ACE_Message_Block; 00031 00032 /** 00033 * @class ACE_Asynch_Connector 00034 * 00035 * @brief This class is an example of the Connector pattern. This class 00036 * will establish new connections and create new HANDLER objects to handle 00037 * the new connections. 00038 * 00039 * Unlike the ACE_Connector, however, this class is designed to 00040 * be used asynchronously with the ACE Proactor framework. 00041 */ 00042 00043 template <class HANDLER> 00044 class ACE_Asynch_Connector : public ACE_Handler 00045 { 00046 public: 00047 00048 /// A do nothing constructor. 00049 ACE_Asynch_Connector (void); 00050 00051 /// Virtual destruction 00052 virtual ~ACE_Asynch_Connector (void); 00053 00054 /** 00055 * This opens asynch connector 00056 */ 00057 virtual int open (int pass_addresses = 0, 00058 ACE_Proactor *proactor = 0, 00059 int validate_new_connection = 1); 00060 00061 /// This initiates a new asynchronous connect 00062 virtual int connect (const ACE_INET_Addr &remote_sap, 00063 const ACE_INET_Addr &local_sap = 00064 ACE_INET_Addr ((u_short)0), 00065 int reuse_addr = 1, 00066 const void *act = 0); 00067 00068 /** 00069 * This cancels all pending accepts operations that were issued by 00070 * the calling thread. 00071 * 00072 * @note On Windows, this method does not cancel connect operations 00073 * issued by other threads. 00074 * 00075 * @note On POSIX, delegates cancelation to ACE_POSIX_Asynch_Connect. 00076 */ 00077 virtual int cancel (void); 00078 00079 00080 /** 00081 * Template method to validate peer before service is opened. 00082 * This method is called when the connection attempt completes, 00083 * whether it succeeded or failed, if the @a validate_connection 00084 * argument to @c open() was non-zero or the @c validate_new_connection() 00085 * method is called to turn this feature on. The default implementation 00086 * returns 0. Users can (and probably should) reimplement this method 00087 * to learn about the success or failure of the connection attempt. 00088 * If the connection completed successfully, this method can be used to 00089 * perform validation of the peer using it's address, running an 00090 * authentication procedure (such as SSL) or anything else necessary or 00091 * desireable. The return value from this method determines whether or 00092 * not ACE will continue opening the service or abort the connection. 00093 * 00094 * @param result Result of the connection acceptance. Use 00095 * result.success() to determine success or failure of 00096 * the connection attempt. 00097 * @param remote Peer's address. If the connection failed, this object 00098 * is undefined. 00099 * @param local Local address connection was completed from. If the 00100 * connection failed, this object is undefined. 00101 * 00102 * @retval -1 ACE_Asynch_Connector will close the connection, and 00103 * the service will not be opened. 00104 * @retval 0 Service opening will proceeed. 00105 * @return Return value is ignored if the connection attempt failed. 00106 */ 00107 virtual int validate_connection (const ACE_Asynch_Connect::Result& result, 00108 const ACE_INET_Addr &remote, 00109 const ACE_INET_Addr& local); 00110 00111 // 00112 // These are low level tweaking methods 00113 // 00114 00115 /// Set and get flag that indicates if parsing and passing of 00116 /// addresses to the service_handler is necessary. 00117 virtual int pass_addresses (void) const; 00118 virtual void pass_addresses (int new_value); 00119 00120 /// Set and get flag that indicates if address validation is 00121 /// required. 00122 virtual int validate_new_connection (void) const; 00123 virtual void validate_new_connection (int new_value); 00124 00125 protected: 00126 00127 /// This is called when an outstanding accept completes. 00128 virtual void handle_connect (const ACE_Asynch_Connect::Result &result); 00129 00130 00131 /// This parses the address from read buffer. 00132 void parse_address (const ACE_Asynch_Connect::Result &result, 00133 ACE_INET_Addr &remote_address, 00134 ACE_INET_Addr &local_address); 00135 00136 /// Return the asynch Connect object. 00137 ACE_Asynch_Connect & asynch_connect (void); 00138 00139 /** 00140 * This is the template method used to create new handler. 00141 * Subclasses must overwrite this method if a new handler creation 00142 * strategy is required. 00143 */ 00144 virtual HANDLER *make_handler (void); 00145 00146 private: 00147 00148 /// Asynch_Connect used to make life easier :-) 00149 ACE_Asynch_Connect asynch_connect_; 00150 00151 /// Flag that indicates if parsing of addresses is necessary. 00152 int pass_addresses_; 00153 00154 /// Flag that indicates if address validation is required. 00155 int validate_new_connection_; 00156 00157 }; 00158 00159 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00160 #include "ace/Asynch_Connector.cpp" 00161 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00162 00163 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00164 #pragma implementation ("Asynch_Connector.cpp") 00165 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00166 00167 #endif /* ACE_WIN32 || ACE_HAS_AIO_CALLS */ 00168 #include "ace/post.h" 00169 #endif /* ACE_ASYNCH_CONNECTOR_H */
1.2.14 written by Dimitri van Heesch,
© 1997-2002