1 #ifndef ACE_FTP_SESSION_CPP
2 #define ACE_FTP_SESSION_CPP
4 #include "ace/INet/INet_Log.h"
5 #include "ace/INet/FTP_Session.h"
6 #include "ace/INet/String_IOStream.h"
7 #include "ace/INet/IOS_util.h"
8 #include "ace/INET_Addr.h"
9 #include "ace/Event_Handler.h"
10 #include "ace/Connector.h"
11 #include "ace/String_Base.h"
12 #include "ace/Truncate.h"
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
22 template <ACE_SYNCH_DECL
>
23 Session_T
<ACE_SYNCH_USE
>::Session_T ()
28 ftp_timeout_ (DEFAULT_TIMEOUT
),
29 cannot_reconnect_ (false),
33 INET_TRACE ("ACE_FTP_Session - ctor");
36 template <ACE_SYNCH_DECL
>
37 Session_T
<ACE_SYNCH_USE
>::Session_T (const ACE_Time_Value
& timeout
)
42 ftp_timeout_ (timeout
),
43 cannot_reconnect_ (false),
47 INET_TRACE ("ACE_FTP_Session - ctor");
50 template <ACE_SYNCH_DECL
>
51 Session_T
<ACE_SYNCH_USE
>::~Session_T ()
53 INET_TRACE ("ACE_FTP_Session - dtor");
57 template <ACE_SYNCH_DECL
>
58 bool Session_T
<ACE_SYNCH_USE
>::is_connected () const
60 return this->connection_
&& this->connection_
->is_connected ();
63 template <ACE_SYNCH_DECL
>
64 bool Session_T
<ACE_SYNCH_USE
>::is_new_connection () const
66 return this->new_connect_
;
69 template <ACE_SYNCH_DECL
>
70 void Session_T
<ACE_SYNCH_USE
>::set_host (const ACE_CString
& host
, u_short port
)
72 if (!this->is_connected ())
79 template <ACE_SYNCH_DECL
>
80 void Session_T
<ACE_SYNCH_USE
>::set_host (const ACE_CString
& host
)
82 if (!this->is_connected ())
88 template <ACE_SYNCH_DECL
>
89 void Session_T
<ACE_SYNCH_USE
>::set_port (u_short port
)
91 if (!this->is_connected ())
97 template <ACE_SYNCH_DECL
>
98 const ACE_CString
& Session_T
<ACE_SYNCH_USE
>::get_host () const
103 template <ACE_SYNCH_DECL
>
104 u_short Session_T
<ACE_SYNCH_USE
>::get_port () const
109 template <ACE_SYNCH_DECL
>
110 bool Session_T
<ACE_SYNCH_USE
>::connect (bool use_reactor
)
112 INET_TRACE ("ACE_FTP_Session::connect");
114 typedef ACE_Connector
<connection_type
, ACE_SOCK_CONNECTOR
> connector_type
;
118 unsigned long f_reactor
= use_reactor
? ACE_Synch_Options::USE_REACTOR
: 0;
119 ACE_Synch_Options
sync_opt (ACE_Synch_Options::USE_TIMEOUT
| f_reactor
,
121 connector_type connector
;
123 connection_type
* new_connection
= 0;
124 ACE_NEW_RETURN (new_connection
,
125 connection_type(sync_opt
),
127 if (connector
.connect (new_connection
,
128 ACE_INET_Addr (this->port_
,
129 this->host_
.c_str ()),
130 ACE_Synch_Options (0,this->ftp_timeout_
)) == -1)
132 INET_ERROR (1, (LM_ERROR
, DLINFO
133 ACE_TEXT ("(%d) ACE_FTP_Session::connect - ")
134 ACE_TEXT ("failed to connect; host=%C, port=%d"),
135 ACE_OS::last_error (), this->host_
.c_str (), this->port_
));
136 // as the connection was dynamically allocated
137 // the connector causes it to be destroyed after
138 // the connection failure
142 this->connection_
= new_connection
;
143 this->connection_
->reference_counting_policy ().value (
144 ACE_Event_Handler::Reference_Counting_Policy::ENABLED
);
146 ACE_NEW_NORETURN (this->sock_stream_
,
147 sock_stream_type (this->connection_
));
148 if (this->sock_stream_
)
150 this->new_connect_
= true;
151 this->cannot_reconnect_
= false;
152 this->reactive_
= use_reactor
;
163 template <ACE_SYNCH_DECL
>
164 bool Session_T
<ACE_SYNCH_USE
>::connect (connection_type
* connection
)
166 INET_TRACE ("ACE_FTP_Session::connect(connection)");
170 if (connection
->is_connected ())
172 ACE_INET_Addr remote
;
173 connection
->peer ().get_remote_addr (remote
);
174 this->host_
= remote
.get_host_name ();
175 this->port_
= remote
.get_port_number ();
179 typedef ACE_Connector
<connection_type
, ACE_SOCK_CONNECTOR
> connector_type
;
181 connector_type connector
;
182 if (connector
.connect (connection
,
183 ACE_INET_Addr (this->host_
.c_str (),
186 INET_ERROR (1, (LM_ERROR
, DLINFO
187 ACE_TEXT ("(%d) ACE_FTP_Session::connect(connection) - ")
188 ACE_TEXT ("failed to connect; host=%C, port=%d"),
189 ACE_OS::last_error (), this->host_
.c_str (), this->port_
));
194 this->connection_
= connection
;
195 this->connection_
->add_reference ();
197 ACE_NEW_NORETURN (this->sock_stream_
,
198 sock_stream_type (this->connection_
));
200 if (this->sock_stream_
)
202 this->new_connect_
= true;
203 this->cannot_reconnect_
= true;
213 template <ACE_SYNCH_DECL
>
214 bool Session_T
<ACE_SYNCH_USE
>::send_request (Request
& request
)
216 INET_TRACE ("ACE_FTP_Session::send_request");
218 if (!this->is_connected ())
220 if (this->cannot_reconnect_
|| !this->connect(this->reactive_
))
222 if (!this->cannot_reconnect_
)
223 INET_ERROR (1, (LM_ERROR
, DLINFO
224 ACE_TEXT ("(%d) FTP_Session::send_request - ")
225 ACE_TEXT ("reconnect failed\n"),
226 ACE_OS::last_error ()));
231 this->new_connect_
= false;
233 request
.write (*this->sock_stream_
);
235 return this->is_connected () && this->sock_stream_
->good ();
238 template <ACE_SYNCH_DECL
>
239 bool Session_T
<ACE_SYNCH_USE
>::receive_response (Response
& response
)
241 INET_TRACE ("ACE_FTP_Session::receive_response");
243 this->sock_stream_
->flush ();
246 return response
.read (*this->sock_stream_
);
249 template <ACE_SYNCH_DECL
>
250 bool Session_T
<ACE_SYNCH_USE
>::send_interrupt ()
252 INET_TRACE ("ACE_FTP_Session::send_interrupt");
254 if (this->is_connected ())
256 this->sock_stream_
->put (ACE_Utils::truncate_cast
<char> (int (INTERRUPT
)));
257 this->sock_stream_
->sync ();
258 return this->sock_stream_
->good ();
264 template <ACE_SYNCH_DECL
>
265 void Session_T
<ACE_SYNCH_USE
>::close ()
267 INET_TRACE ("ACE_FTP_Session::close");
269 if (this->connection_
)
271 if (this->sock_stream_
)
273 delete this->sock_stream_
;
274 this->sock_stream_
= 0;
276 // this should be the last referece and removing it
277 // causes the connection to be destroyed
278 this->connection_
->remove_reference ();
279 this->connection_
= 0;
283 template <ACE_SYNCH_DECL
>
284 bool Session_T
<ACE_SYNCH_USE
>::supports_ftp_extensions () const
286 return this->has_ftp_ext_
;
289 template <ACE_SYNCH_DECL
>
290 void Session_T
<ACE_SYNCH_USE
>::set_ftp_extension_support (bool f
)
292 this->has_ftp_ext_
= f
;
295 template <ACE_SYNCH_DECL
>
296 bool Session_T
<ACE_SYNCH_USE
>::is_reactive () const
298 return this->reactive_
;
301 template <ACE_SYNCH_DECL
>
302 const ACE_Time_Value
& Session_T
<ACE_SYNCH_USE
>::timeout () const
304 return this->ftp_timeout_
;
307 template <ACE_SYNCH_DECL
>
308 void Session_T
<ACE_SYNCH_USE
>::get_local_addr (ACE_INET_Addr
& loc_addr
) const
310 if (this->is_connected ())
311 this->connection_
->peer ().get_local_addr (loc_addr
);
317 ACE_END_VERSIONED_NAMESPACE_DECL
319 #endif /* ACE_FTP_SESSION_CPP */