2 #include "ace/Log_Msg.h"
4 #include "HTBP_Session.h"
5 #include "ace/SOCK_Connector.h"
6 #include "ace/Event_Handler.h"
7 #include "ace/os_include/netinet/os_tcp.h"
10 #include "HTBP_Filter.h"
11 #include "HTBP_ID_Requestor.h"
13 #if !defined (__ACE_INLINE__)
14 #include "HTBP_Session.inl"
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 ACE::HTBP::Session::Session_Map
ACE::HTBP::Session::session_map_
;
20 ACE_UINT32
ACE::HTBP::Session::last_session_id_
= 0;
21 ACE_SYNCH_MUTEX
ACE::HTBP::Session::session_id_lock_
;
23 /// Static method definitions
25 ACE::HTBP::Session::next_session_id ()
27 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, ACE::HTBP::Session::session_id_lock_
, 0);
28 return ++last_session_id_
;
32 ACE::HTBP::Session::add_session (ACE::HTBP::Session
*s
)
34 return session_map_
.bind (s
->session_id(),s
);
38 ACE::HTBP::Session::remove_session (ACE::HTBP::Session
*s
)
40 if (session_map_
.current_size() > 0)
41 return session_map_
.unbind(s
->session_id());
46 ACE::HTBP::Session::find_session (const ACE::HTBP::Session_Id_t
&sid
, ACE::HTBP::Session
*&out
)
48 ACE::HTBP::Session::Map_Entry
*e
= 0;
49 if (session_map_
.find (sid
,e
) == -1)
58 //----------------------------------------------------------------------------
59 ACE::HTBP::Session::Session ()
61 destroy_proxy_addr_ (0),
70 ACE::HTBP::ID_Requestor req
;
71 ACE_TCHAR
* htid
= req
.get_HTID();
72 std::unique_ptr
<ACE_TCHAR
[]> guard (htid
);
73 session_id_
.local_
= ACE_TEXT_ALWAYS_CHAR(htid
);
74 session_id_
.id_
= ACE::HTBP::Session::next_session_id();
75 ACE_NEW (inbound_
, ACE::HTBP::Channel (this));
76 ACE_NEW (outbound_
, ACE::HTBP::Channel (this));
79 ACE::HTBP::Session::Session (const ACE::HTBP::Addr
&peer
,
80 const ACE::HTBP::Addr
&local
,
84 : proxy_addr_ (proxy
),
85 destroy_proxy_addr_ (take_proxy
),
94 session_id_
.peer_
= peer
;
95 session_id_
.local_
= local
;
96 session_id_
.id_
= (sid
== 0) ?
97 ACE::HTBP::Session::next_session_id() : sid
;
99 ACE_NEW (inbound_
,ACE::HTBP::Channel (this));
100 ACE_NEW (outbound_
,ACE::HTBP::Channel (this));
103 ACE::HTBP::Session::Session (const ACE::HTBP::Session_Id_t
&id
,
104 ACE_INET_Addr
*proxy
,
106 : proxy_addr_ (proxy
),
107 destroy_proxy_addr_ (take_proxy
),
117 ACE_NEW (inbound_
, ACE::HTBP::Channel (this));
118 ACE_NEW (outbound_
, ACE::HTBP::Channel (this));
121 ACE::HTBP::Session::Session (const ACE::HTBP::Session
&other
)
123 this->operator=(other
);
127 ACE::HTBP::Session::operator= (const ACE::HTBP::Session
&)
129 // @TODO: figure out why the assignment operator is here if it is
130 // unimplemented? Previously there was an ACE_ASSERT(this == 0)
131 // so apparently something bad had been happening long ago, but I
132 // have no idea what.
136 ACE::HTBP::Session::~Session ()
138 if (destroy_proxy_addr_
)
141 delete this->inbound_
;
142 delete this->outbound_
;
146 ACE::HTBP::Session::close ()
149 this->inbound_
->close();
151 this->outbound_
->close();
152 this->closed_
= true;
153 return ACE::HTBP::Session::remove_session (this);
158 ACE::HTBP::Session::outbound () const
160 if (!this->closed_
&& this->proxy_addr_
)
162 if ( this->outbound_
== 0)
164 ACE::HTBP::Channel::State s
=this->outbound_
->state();
165 return s
== ACE::HTBP::Channel::Init
|| s
== ACE::HTBP::Channel::Ready
? this->outbound_
: 0;
169 ACE::HTBP::Session::reconnect_i (ACE::HTBP::Channel
*s
) const
171 ACE_SOCK_Connector conn
;
172 if (conn
.connect (s
->ace_stream(),*this->proxy_addr_
) == -1)
174 ACE_TCHAR buffer
[128];
175 this->proxy_addr_
->addr_to_string(buffer
, 128, 0);
176 ACE_ERROR ((LM_ERROR
,
177 ACE_TEXT("(%P|%t) ACE::HTBP::Session::reconnect")
178 ACE_TEXT(" failed to %s, %p\n"),
179 buffer
, s
== this->inbound_
?
180 ACE_TEXT("inbound") : ACE_TEXT ("outbound")));
184 #if !defined (ACE_LACKS_TCP_NODELAY)
186 int result
= s
->ace_stream().set_option (ACE_IPPROTO_TCP
,
191 ACE_DEBUG ((LM_DEBUG
, "HTBP::Session::reconnect_i, %p\n", "set_option" ));
192 #endif /* ! ACE_LACKS_TCP_NODELAY */
195 s
->register_notifier(this->reactor_
);
196 if (s
== this->inbound_
)
201 ACE::HTBP::Session::handler ()
203 return this->handler_
;
207 ACE::HTBP::Session::handler (ACE_Event_Handler
* h
)
213 ACE::HTBP::Session::detach (ACE::HTBP::Channel
*ch
)
215 if (this->inbound_
== ch
)
217 else if (this->outbound_
== ch
)
220 ACE_ERROR ((LM_ERROR
,
221 ACE_TEXT("ACE::HTBP::Session::detach ")
222 ACE_TEXT("called with unknown channel\n")));
226 ACE::HTBP::Session::reactor (ACE_Reactor
*r
)
229 this->inbound_
->register_notifier(r
);
230 this->outbound_
->register_notifier(r
);
234 ACE::HTBP::Session::enqueue (ACE_Message_Block
*msg
)
236 this->outbound_queue_
.enqueue_tail(msg
);
237 return msg
->length();
241 ACE::HTBP::Session::flush_outbound_queue ()
244 if (this->outbound_queue_
.message_count() > 0)
246 ACE_Message_Block
*msg
= 0;
249 iovec
[this->outbound_queue_
.message_count()],
251 std::unique_ptr
<iovec
[]> guard (iov
);
252 this->outbound_queue_
.peek_dequeue_head (msg
);
253 for (size_t i
= 0; i
< this->outbound_queue_
.message_count(); i
++)
255 iov
[i
].iov_base
= msg
->rd_ptr();
256 iov
[i
].iov_len
= msg
->length();
259 if (this->outbound_
->state() == ACE::HTBP::Channel::Wait_For_Ack
)
260 this->outbound_
->recv_ack();
261 result
= this->outbound_
->sendv (iov
,this->outbound_queue_
.message_count(),0);
262 while (this->outbound_queue_
.message_count ())
264 this->outbound_queue_
.dequeue_head (msg
);
272 ACE::HTBP::Session::close_inbound () const
274 return this->inbound_
? this->inbound_
->close() : 0;
278 ACE::HTBP::Session::close_outbound () const
280 return this->outbound_
? this->outbound_
->close() : 0;
284 ACE::HTBP::Session::enable (int flags
)
286 this->sock_flags_
|= flags
;
287 int result
= this->inbound_
? this->inbound_
->enable(flags
) : 0;
288 result
|= this->outbound_
? this->outbound_
->enable (flags
) : 0;
293 ACE::HTBP::Session::disable (int flags
)
295 this->sock_flags_
&= ~flags
;
296 int result
= this->inbound_
? this->inbound_
->disable(flags
) : 0;
297 result
|= this->outbound_
? this->outbound_
->disable (flags
) : 0;
302 ACE::HTBP::Session::stream () const
304 return this->stream_
;
308 ACE::HTBP::Session::stream (ACE::HTBP::Stream
*s
)
313 ACE_END_VERSIONED_NAMESPACE_DECL