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