00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Connection_Handler.h 00006 * 00007 * $Id: Connection_Handler.h 78627 2007-06-28 08:50:01Z johnnyw $ 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 (void); 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 /* 00140 * Hook to add public methods from concrete connection handler 00141 * implementation onto the base connection handler. 00142 */ 00143 00144 //@@ CONNECTION_HANDLER_SPL_PUBLIC_METHODS_ADD_HOOK 00145 00146 protected: 00147 00148 /// Return our TAO_ORB_Core pointer 00149 TAO_ORB_Core *orb_core (void); 00150 00151 /// A common function called at the start of any protocol-specific 00152 /// open. Returns -1 on a failure (although no failure mode is 00153 /// currently defined). 00154 int shared_open (void); 00155 00156 /// Set options on the socket 00157 int set_socket_option (ACE_SOCK &sock, int snd_size, int rcv_size); 00158 00159 //@{ 00160 /** 00161 * @name Helper methods for Event_Handler-based derived classes. 00162 * 00163 * Many (actually all so far) implementations of 00164 * TAO_Connection_Handler are a mixin of TAO_Connection_Handler and 00165 * some form of ACE_Event_Handler. The following methods simplify 00166 * such implementations by capturing the common code in a single 00167 * place. 00168 */ 00169 00170 /// Implement the handle_output() callback 00171 int handle_output_eh (ACE_HANDLE h, ACE_Event_Handler * eh); 00172 00173 /// Implement the handle_input() callback 00174 // We're actually going to pull the code from the protocol-specific 00175 // handlers back into this class, because they ALL look exactly the same. 00176 // If some other protocol comes along and needs to do something different, 00177 // it is always free to override handle_input() as it sees fit. 00178 int handle_input_eh (ACE_HANDLE h, ACE_Event_Handler * eh); 00179 int handle_input_internal (ACE_HANDLE h, ACE_Event_Handler *eh); 00180 00181 /// Implement close_connection() for Connection_Handlers that are 00182 /// also Event_Handlers. 00183 int close_connection_eh (ACE_Event_Handler * eh); 00184 00185 /// Pre-invocation hook for I/O operations (handle_input() & 00186 /// handle_output()) 00187 /** 00188 * See the SSLIOP protocol for an interesting use-case 00189 */ 00190 virtual void pre_io_hook (int & return_value); 00191 00192 /// Post-invocation hook for I/O operations (handle_input() & 00193 /// handle_output()) 00194 /** 00195 * See the SSLIOP protocol for an interesting use-case 00196 */ 00197 virtual void pos_io_hook (int & return_value); 00198 //@} 00199 00200 private: 00201 /// Pointer to the TAO_ORB_Core 00202 TAO_ORB_Core * const orb_core_; 00203 00204 /// Transport object reference 00205 TAO_Transport* transport_; 00206 00207 /// Internal state lock, needs to be separate from the reference 00208 /// count / pending upcalls lock because they interleave. 00209 ACE_Lock * lock_; 00210 00211 /// Stores the connection pending state 00212 bool connection_pending_; 00213 00214 /* 00215 * Hook to add instance members from derived class 00216 * onto base Connection_Handler class. Any further 00217 * additions to this class should go before this 00218 * hook. 00219 */ 00220 //@@ CONNECTION_HANDLER_SPL_PRIVATE_DATA_ADD_HOOK 00221 }; 00222 00223 //@@ CONNECTION_HANDLER_SPL_EXTERN_ADD_HOOK 00224 00225 00226 /** 00227 * @class TAO_Auto_Reference 00228 * 00229 * @brief TAO_Auto_Reference acts as a "smart pointer" for 00230 * reference-countable instances. 00231 * 00232 * It increments the refrence count in the constructor and decrements 00233 * it in the destructor. The only requiement for the template 00234 * parameter is to be a class that provides add_reference() and 00235 * remove_reference(). 00236 */ 00237 template <class T> class TAO_Auto_Reference 00238 : private ACE_Copy_Disabled 00239 { 00240 public: 00241 TAO_Auto_Reference (T& r): ref_ (r) 00242 { 00243 ref_.add_reference (); 00244 } 00245 00246 ~TAO_Auto_Reference () 00247 { 00248 ref_.remove_reference (); 00249 } 00250 00251 private: 00252 T& ref_; 00253 }; 00254 00255 00256 TAO_END_VERSIONED_NAMESPACE_DECL 00257 00258 #if defined (__ACE_INLINE__) 00259 #include "tao/Connection_Handler.inl" 00260 #endif /* __ACE_INLINE__ */ 00261 00262 #include /**/ "ace/post.h" 00263 00264 #endif /*TAO_CONNECTION_HANDLER_H*/