Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / tao / Strategies / UIOP_Connection_Handler.cpp
blobb8ccf92b0a91f4eee5b741afecaa38b689501bf8
1 #include "tao/Strategies/UIOP_Connection_Handler.h"
3 #if TAO_HAS_UIOP == 1
5 #include "tao/Strategies/UIOP_Transport.h"
6 #include "tao/Strategies/UIOP_Endpoint.h"
7 #include "tao/debug.h"
8 #include "tao/ORB_Core.h"
9 #include "tao/ORB.h"
10 #include "tao/CDR.h"
11 #include "tao/Timeprobe.h"
12 #include "tao/Server_Strategy_Factory.h"
13 #include "tao/Base_Transport_Property.h"
14 #include "tao/Transport_Cache_Manager.h"
15 #include "tao/Resume_Handle.h"
16 #include "tao/Thread_Lane_Resources.h"
17 #include "tao/Protocols_Hooks.h"
19 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
21 TAO_UIOP_Connection_Handler::TAO_UIOP_Connection_Handler (ACE_Thread_Manager *t)
22 : TAO_UIOP_SVC_HANDLER (t, 0 , 0),
23 TAO_Connection_Handler (0)
25 // This constructor should *never* get called, it is just here to
26 // make the compiler happy: the default implementation of the
27 // Creation_Strategy requires a constructor with that signature, we
28 // don't use that implementation, but some (most?) compilers
29 // instantiate it anyway.
30 ACE_ASSERT (0);
34 TAO_UIOP_Connection_Handler::TAO_UIOP_Connection_Handler (TAO_ORB_Core *orb_core)
35 : TAO_UIOP_SVC_HANDLER (orb_core->thr_mgr (), 0, 0),
36 TAO_Connection_Handler (orb_core)
38 TAO_UIOP_Transport* specific_transport = 0;
39 ACE_NEW (specific_transport,
40 TAO_UIOP_Transport (this, orb_core));
42 // store this pointer (indirectly increment ref count)
43 this->transport (specific_transport);
47 TAO_UIOP_Connection_Handler::~TAO_UIOP_Connection_Handler ()
49 delete this->transport ();
50 int const result =
51 this->release_os_resources ();
53 if (result == -1 && TAO_debug_level)
55 TAOLIB_ERROR ((LM_ERROR,
56 ACE_TEXT("TAO (%P|%t) - UIOP_Connection_Handler::")
57 ACE_TEXT("~UIOP_Connection_Handler, ")
58 ACE_TEXT("release_os_resources() failed %m\n")));
62 int
63 TAO_UIOP_Connection_Handler::open_handler (void *v)
65 return this->open (v);
68 int
69 TAO_UIOP_Connection_Handler::open (void*)
71 if (this->shared_open() == -1)
72 return -1;
74 TAO_UIOP_Protocol_Properties protocol_properties;
76 // Initialize values from ORB params.
77 protocol_properties.send_buffer_size_ =
78 this->orb_core ()->orb_params ()->sock_sndbuf_size ();
79 protocol_properties.recv_buffer_size_ =
80 this->orb_core ()->orb_params ()->sock_rcvbuf_size ();
82 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
84 if (tph != 0)
86 try
88 if (this->transport ()->opened_as () == TAO::TAO_CLIENT_ROLE)
90 tph->client_protocol_properties_at_orb_level (protocol_properties);
92 else
94 tph->server_protocol_properties_at_orb_level (protocol_properties);
97 catch (const ::CORBA::Exception&)
99 return -1;
103 if (this->set_socket_option (this->peer (),
104 protocol_properties.send_buffer_size_,
105 protocol_properties.recv_buffer_size_) == -1)
106 return -1;
108 if (this->transport ()->wait_strategy ()->non_blocking ())
110 if (this->peer ().enable (ACE_NONBLOCK) == -1)
111 return -1;
114 // Called by the <Strategy_Acceptor> when the handler is completely
115 // connected.
116 ACE_UNIX_Addr addr;
118 if (this->peer ().get_remote_addr (addr) == -1)
119 return -1;
121 if (TAO_debug_level > 0)
122 TAOLIB_DEBUG ((LM_DEBUG,
123 ACE_TEXT ("TAO (%P|%t) - UIOP_Connection_Handler::open, connection to server ")
124 ACE_TEXT ("<%C> on %d\n"),
125 addr.get_path_name (), this->peer ().get_handle ()));
127 // Set that the transport is now connected, if fails we return -1
128 // Use C-style cast b/c otherwise we get warnings on lots of
129 // compilers
130 if (!this->transport ()->post_open ((size_t) this->get_handle ()))
131 return -1;
133 this->state_changed (TAO_LF_Event::LFS_SUCCESS,
134 this->orb_core ()->leader_follower ());
136 return 0;
140 TAO_UIOP_Connection_Handler::resume_handler ()
142 return ACE_Event_Handler::ACE_APPLICATION_RESUMES_HANDLER;
146 TAO_UIOP_Connection_Handler::close_connection ()
148 return this->close_connection_eh (this);
152 TAO_UIOP_Connection_Handler::handle_input (ACE_HANDLE h)
154 return this->handle_input_eh (h, this);
158 TAO_UIOP_Connection_Handler::handle_output (ACE_HANDLE handle)
160 int const result = this->handle_output_eh (handle, this);
162 if (result == -1)
164 this->close_connection ();
165 return 0;
168 return result;
172 TAO_UIOP_Connection_Handler::handle_timeout (const ACE_Time_Value &,
173 const void *)
175 // Using this to ensure this instance will be deleted (if necessary)
176 // only after reset_state(). Without this, when this refcount==1 -
177 // the call to close() will cause a call to remove_reference() which
178 // will delete this. At that point this->reset_state() is in no
179 // man's territory and that causes SEGV on some platforms (Windows!)
181 TAO_Auto_Reference<TAO_UIOP_Connection_Handler> safeguard (*this);
183 // NOTE: Perhaps not the best solution, as it feels like the upper
184 // layers should be responsible for this?
186 // We don't use this upcall for I/O. This is only used by the
187 // Connector to indicate that the connection timedout. Therefore,
188 // we should call close().
189 int const ret = this->close ();
190 this->reset_state (TAO_LF_Event::LFS_TIMEOUT);
191 return ret;
195 TAO_UIOP_Connection_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
197 ACE_ASSERT (0);
198 return 0;
202 TAO_UIOP_Connection_Handler::close (u_long flags)
204 return this->close_handler (flags);
208 TAO_UIOP_Connection_Handler::release_os_resources ()
210 return this->peer().close ();
214 TAO_UIOP_Connection_Handler::add_transport_to_cache ()
216 ACE_UNIX_Addr addr;
218 // Get the peername.
219 if (this->peer ().get_remote_addr (addr) == -1)
220 return -1;
222 // Construct an UIOP_Endpoint object
223 TAO_UIOP_Endpoint endpoint (addr);
225 // Construct a property object
226 TAO_Base_Transport_Property prop (&endpoint);
228 TAO::Transport_Cache_Manager &cache =
229 this->orb_core ()->lane_resources ().transport_cache ();
231 // Add the handler to Cache
232 return cache.cache_transport (&prop, this->transport ());
236 TAO_UIOP_Connection_Handler::handle_write_ready (const ACE_Time_Value *t)
238 return ACE::handle_write_ready (this->peer ().get_handle (), t);
241 TAO_END_VERSIONED_NAMESPACE_DECL
243 #endif /*TAO_HAS_UIOP == 1*/