2 ## Generic data connection package
5 package Net::FTP::dataconn;
16 our $VERSION = '3.08';
18 $Net::FTP::IOCLASS or die "please load Net::FTP before Net::FTP::dataconn";
19 our @ISA = $Net::FTP::IOCLASS;
23 ${*$data}{'net_ftp_bytesread'} = 0;
29 my $ftp = ${*$data}{'net_ftp_cmd'};
31 # no need to abort if we have finished the xfer
33 if ${*$data}{'net_ftp_eof'};
35 # for some reason if we continuously open RETR connections and not
36 # read a single byte, then abort them after a while the server will
37 # close our connection, this prevents the unexpected EOF on the
38 # command channel -- GMB
39 if (exists ${*$data}{'net_ftp_bytesread'}
40 && (${*$data}{'net_ftp_bytesread'} == 0))
43 my $timeout = $data->timeout;
44 $data->can_read($timeout) && sysread($data, $buf, 1);
47 ${*$data}{'net_ftp_eof'} = 1; # fake
49 $ftp->abort; # this will close me
55 my $ftp = ${*$data}{'net_ftp_cmd'};
57 $data->SUPER::close();
59 delete ${*$ftp}{'net_ftp_dataconn'}
61 && exists ${*$ftp}{'net_ftp_dataconn'}
62 && $data == ${*$ftp}{'net_ftp_dataconn'};
68 my $ftp = ${*$data}{'net_ftp_cmd'};
70 if (exists ${*$data}{'net_ftp_bytesread'} && !${*$data}{'net_ftp_eof'}) {
72 eval { local($SIG{__DIE__}); $data->read($junk, 1, 0) };
73 return $data->abort unless ${*$data}{'net_ftp_eof'};
78 return unless defined $ftp;
80 $ftp->response() == CMD_OK
81 && $ftp->message =~ /unique file name:\s*(\S*)\s*\)/
82 && (${*$ftp}{'net_ftp_unique'} = $1);
84 $ftp->status == CMD_OK;
89 my ($data, $timeout, $do_read) = @_;
90 my ($rin, $rout, $win, $wout, $tout, $nfound);
92 vec($rin = '', fileno($data), 1) = 1;
94 ($win, $rin) = ($rin, $win) unless $do_read;
97 $nfound = select($rout = $rin, $wout = $win, undef, $tout = $timeout);
110 _select(@_[0, 1], 1);
115 _select(@_[0, 1], 0);
122 ${*$ftp}{'net_ftp_cmd'};
129 ${*$ftp}{'net_ftp_bytesread'} || 0;
138 Net::FTP::dataconn - FTP Client data connection class
142 Some of the methods defined in C<Net::FTP> return an object which will
143 be derived from this class. The dataconn class itself is derived from
144 the C<IO::Socket::INET> class, so any normal IO operations can be performed.
145 However the following methods are defined in the dataconn class and IO should
146 be performed using these.
150 =item read ( BUFFER, SIZE [, TIMEOUT ] )
152 Read C<SIZE> bytes of data from the server and place it into C<BUFFER>, also
153 performing any <CRLF> translation necessary. C<TIMEOUT> is optional, if not
154 given, the timeout value from the command connection will be used.
156 Returns the number of bytes read before any <CRLF> translation.
158 =item write ( BUFFER, SIZE [, TIMEOUT ] )
160 Write C<SIZE> bytes of data from C<BUFFER> to the server, also
161 performing any <CRLF> translation necessary. C<TIMEOUT> is optional, if not
162 given, the timeout value from the command connection will be used.
164 Returns the number of bytes written before any <CRLF> translation.
168 Returns the number of bytes read so far.
172 Abort the current data transfer.
176 Close the data connection and get a response from the FTP server. Returns
177 I<true> if the connection was closed successfully and the first digit of
178 the response from the server was a '2'.