00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Connection_Handler.h 00006 * 00007 * $Id: Connection_Handler.h 86578 2009-08-31 09:50:31Z vzykov $ 00008 * 00009 * @author Balachandran Natarajan <bala@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef TAO_CONNECTION_HANDLER_H 00014 #define TAO_CONNECTION_HANDLER_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include "ace/Copy_Disabled.h" 00019 #include "tao/LF_CH_Event.h" 00020 00021 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00022 # pragma once 00023 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00024 00025 #include "tao/Basic_Types.h" 00026 #include "ace/Event_Handler.h" 00027 00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00029 class ACE_SOCK; 00030 class ACE_Lock; 00031 class ACE_Event_Handler; 00032 ACE_END_VERSIONED_NAMESPACE_DECL 00033 00034 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00035 00036 class TAO_ORB_Core; 00037 class TAO_Transport; 00038 00039 /* 00040 * Hook to specialize the connection handler with the 00041 * concrete connection handler implementation. 00042 */ 00043 //@@ CONNECTION_HANDLER_SPL_INCLUDE_FORWARD_DECL_ADD_HOOK 00044 00045 /** 00046 * @class TAO_Connection_Handler 00047 * 00048 * @brief TAO_Connection_Handler 00049 * 00050 * This class is an abstraction for the connection handlers. The 00051 * connections handler in every protocol can derive from this 00052 * class as well as the ACE_Svc_Handler specialised for the 00053 * right protocol. This way, most of the common code for the 00054 * different protocols would be in this implementation. 00055 */ 00056 class TAO_Export TAO_Connection_Handler : public TAO_LF_CH_Event 00057 { 00058 public: 00059 00060 /// Constructor 00061 TAO_Connection_Handler (void); 00062 00063 /// Constructor 00064 TAO_Connection_Handler (TAO_ORB_Core *orb_core); 00065 00066 /// Destructor 00067 virtual ~TAO_Connection_Handler (void); 00068 00069 /// Return the underlying transport object 00070 TAO_Transport *transport (void); 00071 00072 /// Set the underlying transport object 00073 void transport (TAO_Transport* transport); 00074 00075 /// Is the handler closed or timed out? 00076 bool is_closed (void) const; 00077 00078 /// Is the handler open? 00079 bool is_open (void) const; 00080 00081 /// Closed due to timeout? 00082 bool is_timeout (void) const; 00083 00084 /// Is the handler in the process of being connected? 00085 bool is_connecting (void) const; 00086 00087 /// Close the underlying connection. 00088 /** 00089 * Used by the ORB to actively close connections that are idle, 00090 * stale or somehow are determined to be broken before the Reactor 00091 * does. 00092 * 00093 * @return Return 0 if the connection was already closed, non-zero 00094 * otherwise. 00095 */ 00096 virtual int close_connection (void) = 0; 00097 00098 /// The event handler calls, here so that other objects who hold a 00099 /// reference to this object can call the event handler methods. 00100 virtual int handle_input (ACE_HANDLE fd) = 0; 00101 00102 /// This method is invoked from the svc () method of the Svc_Handler 00103 /// Object. 00104 int svc_i (void); 00105 00106 /// A open () hook 00107 /** 00108 * See Thread_Per_Connection_Handler for a use case 00109 */ 00110 virtual int open_handler (void *) = 0; 00111 00112 /// A close() hook, called by the Transport Connector when they want to close 00113 /// this handler 00114 virtual int close_handler (u_long flags = 0); 00115 00116 /// When waiting for an asynchronous connection to complete an 00117 /// additional reference must be maintained, related to bugzilla 00118 /// #2417. However once the connection is successfully established, 00119 /// this reference must be removed. Using connection_pending allows 00120 /// the connection handler to know that it is opening as a result of 00121 /// a delayed asynch connection rather than an immediate synch 00122 /// connection, which has no additional reference needs. 00123 void connection_pending (void); 00124 00125 /// A pending connection may be canceled due to an error detected 00126 /// while the initiating thread is still in the Connector. 00127 void cancel_pending_connection (void); 00128 00129 /// Set the Diff-Serv codepoint on outgoing packets. Only has 00130 /// effect for remote protocols (e.g., IIOP); no effect for local 00131 /// protocols (UIOP). Default implementation is for local 00132 /// protocols. Remote protocols must overwrite implementation. 00133 virtual int set_dscp_codepoint (CORBA::Boolean set_network_priority); 00134 virtual int set_dscp_codepoint (CORBA::Long dscp_codepoint); 00135 00136 /// Release the OS resources related to this handler. 00137 virtual int release_os_resources (void); 00138 00139 virtual int handle_write_ready (const ACE_Time_Value *timeout); 00140 00141 /* 00142 * Hook to add public methods from concrete connection handler 00143 * implementation onto the base connection handler. 00144 */ 00145 00146 //@@ CONNECTION_HANDLER_SPL_PUBLIC_METHODS_ADD_HOOK 00147 00148 protected: 00149 00150 /// Return our TAO_ORB_Core pointer 00151 TAO_ORB_Core *orb_core (void); 00152 00153 /// A common function called at the start of any protocol-specific 00154 /// open. Returns -1 on a failure (although no failure mode is 00155 /// currently defined). 00156 int shared_open (void); 00157 00158 /// Set options on the socket 00159 int set_socket_option (ACE_SOCK &sock, int snd_size, int rcv_size); 00160 00161 //@{ 00162 /** 00163 * @name Helper methods for Event_Handler-based derived classes. 00164 * 00165 * Many (actually all so far) implementations of 00166 * TAO_Connection_Handler are a mixin of TAO_Connection_Handler and 00167 * some form of ACE_Event_Handler. The following methods simplify 00168 * such implementations by capturing the common code in a single 00169 * place. 00170 */ 00171 00172 /// Implement the handle_output() callback 00173 int handle_output_eh (ACE_HANDLE h, ACE_Event_Handler * eh); 00174 00175 /// Implement the handle_input() callback 00176 // We're actually going to pull the code from the protocol-specific 00177 // handlers back into this class, because they ALL look exactly the same. 00178 // If some other protocol comes along and needs to do something different, 00179 // it is always free to override handle_input() as it sees fit. 00180 int handle_input_eh (ACE_HANDLE h, ACE_Event_Handler * eh); 00181 int handle_input_internal (ACE_HANDLE h, ACE_Event_Handler *eh); 00182 00183 /// Implement close_connection() for Connection_Handlers that are 00184 /// also Event_Handlers. 00185 int close_connection_eh (ACE_Event_Handler * eh); 00186 00187 /// Pre-invocation hook for I/O operations (handle_input() & 00188 /// handle_output()) 00189 /** 00190 * See the SSLIOP protocol for an interesting use-case 00191 */ 00192 virtual void pre_io_hook (int & return_value); 00193 00194 /// Post-invocation hook for I/O operations (handle_input() & 00195 /// handle_output()) 00196 /** 00197 * See the SSLIOP protocol for an interesting use-case 00198 */ 00199 virtual void pos_io_hook (int & return_value); 00200 //@} 00201 00202 private: 00203 ACE_UNIMPLEMENTED_FUNC (void operator= (const TAO_Connection_Handler &)) 00204 ACE_UNIMPLEMENTED_FUNC (TAO_Connection_Handler (const TAO_Connection_Handler &)) 00205 00206 private: 00207 /// Pointer to the TAO_ORB_Core 00208 TAO_ORB_Core * const orb_core_; 00209 00210 /// Transport object reference 00211 TAO_Transport* transport_; 00212 00213 /// Stores the connection pending state 00214 bool connection_pending_; 00215 00216 /// Once closed make sure the transport is not added back to the cache. 00217 /// This is distinct from the leader-follower state so it cannot be reset. 00218 bool is_closed_; 00219 00220 /* 00221 * Hook to add instance members from derived class 00222 * onto base Connection_Handler class. Any further 00223 * additions to this class should go before this 00224 * hook. 00225 */ 00226 //@@ CONNECTION_HANDLER_SPL_PRIVATE_DATA_ADD_HOOK 00227 }; 00228 00229 //@@ CONNECTION_HANDLER_SPL_EXTERN_ADD_HOOK 00230 00231 00232 /** 00233 * @class TAO_Auto_Reference 00234 * 00235 * @brief TAO_Auto_Reference acts as a "smart pointer" for 00236 * reference-countable instances. 00237 * 00238 * It increments the refrence count in the constructor and decrements 00239 * it in the destructor. The only requiement for the template 00240 * parameter is to be a class that provides add_reference() and 00241 * remove_reference(). 00242 */ 00243 template <class T> class TAO_Auto_Reference 00244 : private ACE_Copy_Disabled 00245 { 00246 public: 00247 TAO_Auto_Reference (T& r): ref_ (r) 00248 { 00249 ref_.add_reference (); 00250 } 00251 00252 ~TAO_Auto_Reference () 00253 { 00254 ref_.remove_reference (); 00255 } 00256 00257 private: 00258 T& ref_; 00259 }; 00260 00261 00262 TAO_END_VERSIONED_NAMESPACE_DECL 00263 00264 #if defined (__ACE_INLINE__) 00265 #include "tao/Connection_Handler.inl" 00266 #endif /* __ACE_INLINE__ */ 00267 00268 #include /**/ "ace/post.h" 00269 00270 #endif /*TAO_CONNECTION_HANDLER_H*/