Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / Strategies / SHMIOP_Connection_Handler.cpp
blobec18a6ab65498499eefbc40b9e90531248d5ebe8
1 #include "tao/Strategies/SHMIOP_Connection_Handler.h"
3 #if defined (TAO_HAS_SHMIOP) && (TAO_HAS_SHMIOP != 0)
5 #include "tao/Timeprobe.h"
6 #include "tao/debug.h"
7 #include "tao/ORB_Core.h"
8 #include "tao/ORB.h"
9 #include "tao/CDR.h"
10 #include "tao/Server_Strategy_Factory.h"
11 #include "tao/Base_Transport_Property.h"
12 #include "tao/Transport_Cache_Manager.h"
13 #include "tao/Thread_Lane_Resources.h"
14 #include "tao/Strategies/SHMIOP_Endpoint.h"
15 #include "tao/Resume_Handle.h"
16 #include "tao/Protocols_Hooks.h"
18 #include "ace/os_include/netinet/os_tcp.h"
19 #include "ace/os_include/os_netdb.h"
21 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
23 TAO_SHMIOP_Connection_Handler::TAO_SHMIOP_Connection_Handler (ACE_Thread_Manager *t)
24 : TAO_SHMIOP_SVC_HANDLER (t, 0 , 0),
25 TAO_Connection_Handler (0)
27 // This constructor should *never* get called, it is just here to
28 // make the compiler happy: the default implementation of the
29 // Creation_Strategy requires a constructor with that signature, we
30 // don't use that implementation, but some (most?) compilers
31 // instantiate it anyway.
32 ACE_ASSERT (0);
36 TAO_SHMIOP_Connection_Handler::TAO_SHMIOP_Connection_Handler (TAO_ORB_Core *orb_core)
37 : TAO_SHMIOP_SVC_HANDLER (orb_core->thr_mgr (), 0, 0),
38 TAO_Connection_Handler (orb_core)
40 TAO_SHMIOP_Transport* specific_transport = 0;
41 ACE_NEW (specific_transport,
42 TAO_SHMIOP_Transport(this, orb_core));
44 // store this pointer (indirectly increment ref count)
45 this->transport (specific_transport);
49 TAO_SHMIOP_Connection_Handler::~TAO_SHMIOP_Connection_Handler ()
51 delete this->transport ();
52 int const result =
53 this->release_os_resources ();
55 if (result == -1 && TAO_debug_level)
57 TAOLIB_ERROR ((LM_ERROR,
58 ACE_TEXT("TAO (%P|%t) - SHMIOP_Connection_Handler::")
59 ACE_TEXT("~SHMIOP_Connection_Handler, ")
60 ACE_TEXT("release_os_resources() failed %m\n")));
65 int
66 TAO_SHMIOP_Connection_Handler::open_handler (void *v)
68 return this->open (v);
71 int
72 TAO_SHMIOP_Connection_Handler::open (void*)
74 if (this->shared_open() == -1)
75 return -1;
77 TAO_SHMIOP_Protocol_Properties protocol_properties;
79 // Initialize values from ORB params.
80 protocol_properties.send_buffer_size_ =
81 this->orb_core ()->orb_params ()->sock_sndbuf_size ();
82 protocol_properties.recv_buffer_size_ =
83 this->orb_core ()->orb_params ()->sock_rcvbuf_size ();
84 protocol_properties.no_delay_ =
85 this->orb_core ()->orb_params ()->nodelay ();
87 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
89 if (tph != 0)
91 try
93 if (this->transport ()->opened_as () == TAO::TAO_CLIENT_ROLE)
95 tph->client_protocol_properties_at_orb_level (protocol_properties);
97 else
99 tph->server_protocol_properties_at_orb_level (protocol_properties);
102 catch (const ::CORBA::Exception&)
104 return -1;
108 if (this->set_socket_option (this->peer (),
109 protocol_properties.send_buffer_size_,
110 protocol_properties.recv_buffer_size_) == -1)
111 return -1;
113 #if !defined (ACE_LACKS_TCP_NODELAY)
114 if (this->peer ().set_option (ACE_IPPROTO_TCP,
115 TCP_NODELAY,
116 (void *) &protocol_properties.no_delay_,
117 sizeof (protocol_properties.no_delay_)) == -1)
118 return -1;
120 #endif /* ! ACE_LACKS_TCP_NODELAY */
122 if (this->transport ()->wait_strategy ()->non_blocking ())
124 if (this->peer ().enable (ACE_NONBLOCK) == -1)
125 return -1;
128 // Called by the <Strategy_Acceptor> when the handler is
129 // completely connected.
130 ACE_INET_Addr addr;
132 ACE_TCHAR local_as_string[MAXHOSTNAMELEN + 16];
134 // Get the peername.
135 if (this->peer ().get_remote_addr (addr) == -1)
136 return -1;
138 // Verify that we can resolve the peer hostname.
139 else if (addr.addr_to_string (local_as_string, sizeof (local_as_string)) == -1)
140 return -1;
142 if (TAO_debug_level > 0)
144 TAOLIB_DEBUG ((LM_DEBUG,
145 ACE_TEXT ("TAO (%P|%t) - SHMIOP connection from client")
146 ACE_TEXT ("<%s> on %d\n"),
147 local_as_string, this->peer ().get_handle ()));
150 // Set that the transport is now connected, if fails we return -1
151 // Use C-style cast b/c otherwise we get warnings on lots of
152 // compilers
153 if (!this->transport ()->post_open ((size_t) this->get_handle ()))
154 return -1;
156 // Not needed, anyway
157 this->state_changed (TAO_LF_Event::LFS_SUCCESS,
158 this->orb_core ()->leader_follower ());
160 return 0;
164 TAO_SHMIOP_Connection_Handler::resume_handler ()
166 return ACE_Event_Handler::ACE_APPLICATION_RESUMES_HANDLER;
170 TAO_SHMIOP_Connection_Handler::close_connection ()
172 return this->close_connection_eh (this);
176 TAO_SHMIOP_Connection_Handler::handle_input (ACE_HANDLE h)
178 return this->handle_input_eh (h, this);
182 TAO_SHMIOP_Connection_Handler::handle_output (ACE_HANDLE handle)
184 int result =
185 this->handle_output_eh (handle, this);
187 if (result == -1)
189 this->close_connection ();
190 return 0;
193 return result;
197 TAO_SHMIOP_Connection_Handler::handle_timeout (const ACE_Time_Value &,
198 const void *)
200 // Using this to ensure this instance will be deleted (if necessary)
201 // only after reset_state(). Without this, when this refcount==1 -
202 // the call to close() will cause a call to remove_reference() which
203 // will delete this. At that point this->reset_state() is in no
204 // man's territory and that causes SEGV on some platforms (Windows!)
206 TAO_Auto_Reference<TAO_SHMIOP_Connection_Handler> safeguard (*this);
208 // NOTE: Perhaps not the best solution, as it feels like the upper
209 // layers should be responsible for this?
211 // We don't use this upcall for I/O. This is only used by the
212 // Connector to indicate that the connection timedout. Therefore,
213 // we should call close().
214 int ret = this->close ();
215 this->reset_state (TAO_LF_Event::LFS_TIMEOUT);
216 return ret;
220 TAO_SHMIOP_Connection_Handler::handle_close (ACE_HANDLE,
221 ACE_Reactor_Mask)
223 ACE_ASSERT (0);
224 return 0;
228 TAO_SHMIOP_Connection_Handler::close (u_long flags)
230 return this->close_handler (flags);
234 TAO_SHMIOP_Connection_Handler::release_os_resources ()
236 return this->peer().close ();
240 TAO_SHMIOP_Connection_Handler::add_transport_to_cache ()
242 ACE_INET_Addr addr;
244 // Get the peername.
245 if (this->peer ().get_remote_addr (addr) == -1)
246 return -1;
248 // Construct an SHMIOP_Endpoint object
249 TAO_SHMIOP_Endpoint endpoint (
250 addr,
251 this->orb_core()->orb_params()->use_dotted_decimal_addresses ());
253 // Construct a property object
254 TAO_Base_Transport_Property prop (&endpoint);
256 TAO::Transport_Cache_Manager &cache =
257 this->orb_core ()->lane_resources ().transport_cache ();
259 // Add the handler to Cache
260 return cache.cache_transport (&prop, this->transport ());
264 TAO_SHMIOP_Connection_Handler::handle_write_ready (const ACE_Time_Value *t)
266 return ACE::handle_write_ready (this->peer ().get_handle (), t);
269 TAO_END_VERSIONED_NAMESPACE_DECL
271 #endif /*(TAO_HAS_SHMIOP) && (TAO_HAS_SHMIOP != 0) */