 |
Changeset 3447
- Timestamp:
- 04/20/08 07:35:06
(8 months ago)
- Author:
- lmartin92
- Message:
--
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3411 |
r3447 |
|
| 5 | 5 | * License: $(BSD Liscense) |
|---|
| 6 | 6 | */ |
|---|
| | 7 | |
|---|
| | 8 | //name the module |
|---|
| 7 | 9 | module tango.net.ftp.FtpClient; |
|---|
| 8 | 10 | |
|---|
| | 11 | //import all needed files |
|---|
| 9 | 12 | private { |
|---|
| 10 | 13 | import tango.net.ftp.Telnet; |
|---|
| … | … | |
| 231 | 234 | char[] currFile_ = ""; |
|---|
| 232 | 235 | SocketConduit dataSocket_; |
|---|
| | 236 | TimeSpan timeout = TimeSpan.millis(5000); |
|---|
| 233 | 237 | |
|---|
| 234 | 238 | public FtpFeature[] supportedFeatures() { |
|---|
| … | … | |
| 282 | 286 | body { |
|---|
| 283 | 287 | |
|---|
| 284 | | if(socket !is null) { |
|---|
| 285 | | socket.close(); |
|---|
| | 288 | if(socket_ !is null) { |
|---|
| | 289 | socket_.close(); |
|---|
| 286 | 290 | } |
|---|
| 287 | 291 | |
|---|
| … | … | |
| 312 | 316 | |
|---|
| 313 | 317 | public void close() { |
|---|
| 314 | | if(socket !is null) { |
|---|
| | 318 | if(socket_ !is null) { |
|---|
| 315 | 319 | try { |
|---|
| 316 | 320 | sendCommand("QUIT"); |
|---|
| … | … | |
| 320 | 324 | } |
|---|
| 321 | 325 | |
|---|
| 322 | | socket.close(); |
|---|
| | 326 | socket_.close(); |
|---|
| 323 | 327 | |
|---|
| 324 | 328 | delete supportedFeatures_; |
|---|
| 325 | | delete socket; |
|---|
| | 329 | delete socket_; |
|---|
| 326 | 330 | } |
|---|
| 327 | 331 | } |
|---|
| … | … | |
| 540 | 544 | |
|---|
| 541 | 545 | public void sendCommand(char[] command, char[][] parameters...) { |
|---|
| 542 | | assert(this.socket !is null); |
|---|
| | 546 | assert(this.socket_ !is null); |
|---|
| 543 | 547 | |
|---|
| 544 | 548 | char[] socketCommand = command; |
|---|
| … | … | |
| 577 | 581 | |
|---|
| 578 | 582 | public FtpResponse readResponse() { |
|---|
| 579 | | assert(this.socket !is null); |
|---|
| | 583 | assert(this.socket_ !is null); |
|---|
| 580 | 584 | |
|---|
| 581 | 585 | // Pick a time at which we stop reading. It can't take too long, but it could take a bit for the whole response. |
|---|
| … | … | |
| 729 | 733 | // Connecting to the same host. |
|---|
| 730 | 734 | IPv4Address |
|---|
| 731 | | remote = cast(IPv4Address) this.socket.socket.remoteAddress(); |
|---|
| | 735 | remote = cast(IPv4Address) this.socket_.socket.remoteAddress(); |
|---|
| 732 | 736 | assert(remote !is null); |
|---|
| 733 | 737 | |
|---|
| … | … | |
| 748 | 752 | |
|---|
| 749 | 753 | IPv4Address |
|---|
| 750 | | remote = cast(IPv4Address) this.socket.socket.remoteAddress(); |
|---|
| | 754 | remote = cast(IPv4Address) this.socket_.socket.remoteAddress(); |
|---|
| 751 | 755 | assert(remote !is null); |
|---|
| 752 | 756 | |
|---|
| … | … | |
| 809 | 813 | return this.isSupported(command); |
|---|
| 810 | 814 | } |
|---|
| 811 | | } |
|---|
| | 815 | |
|---|
| | 816 | /// Prepare a data socket for use. |
|---|
| | 817 | /// |
|---|
| | 818 | /// This modifies the socket in some cases. |
|---|
| | 819 | /// |
|---|
| | 820 | /// Params: |
|---|
| | 821 | /// data = the data listener socket |
|---|
| | 822 | protected void prepareDataSocket(inout SocketConduit data) { |
|---|
| | 823 | switch(this.inf_.type) { |
|---|
| | 824 | default: |
|---|
| | 825 | exception("unknown connection type"); |
|---|
| | 826 | |
|---|
| | 827 | case FtpConnectionType.active: |
|---|
| | 828 | Socket new_data = null; |
|---|
| | 829 | |
|---|
| | 830 | SocketSet set = new SocketSet(); |
|---|
| | 831 | scope(exit) |
|---|
| | 832 | delete set; |
|---|
| | 833 | |
|---|
| | 834 | // At end_time, we bail. |
|---|
| | 835 | Time end_time = Clock.now + this.timeout; |
|---|
| | 836 | |
|---|
| | 837 | while(Clock.now < end_time) { |
|---|
| | 838 | set.reset(); |
|---|
| | 839 | set.add(data.socket); |
|---|
| | 840 | |
|---|
| | 841 | // Can we accept yet? |
|---|
| | 842 | int code = Socket.select(set, null, null, this.timeout); |
|---|
| | 843 | if(code == -1 || code == 0) |
|---|
| | 844 | break; |
|---|
| | 845 | |
|---|
| | 846 | new_data = data.socket.accept(); |
|---|
| | 847 | break; |
|---|
| | 848 | } |
|---|
| | 849 | |
|---|
| | 850 | if(new_data is null) |
|---|
| | 851 | throw new FtpException("CLIENT: No connection from server", |
|---|
| | 852 | "420"); |
|---|
| | 853 | |
|---|
| | 854 | // We don't need the listener anymore. |
|---|
| | 855 | data.socket.shutdown(SocketShutdown.BOTH); |
|---|
| | 856 | data.detach(); |
|---|
| | 857 | |
|---|
| | 858 | // This is the actual socket. |
|---|
| | 859 | data.connect(new_data.localAddress()); |
|---|
| | 860 | break; |
|---|
| | 861 | |
|---|
| | 862 | case FtpConnectionType.passive: |
|---|
| | 863 | break; |
|---|
| | 864 | } |
|---|
| | 865 | } |
|---|
| | 866 | } |
|---|
| | 867 | |
|---|
| | 868 | debug(UnitTest) { |
|---|
| | 869 | import tango.io.Stdout; |
|---|
| | 870 | |
|---|
| | 871 | void main() { |
|---|
| | 872 | Stdout("Testing the new parts in FtpClient.d").newline.flush; |
|---|
| | 873 | FTPConnection conn = new FTPConnection("ftp.digitalmars.com"); |
|---|
| | 874 | Stdout("Connecting works!").newline.flush; |
|---|
| | 875 | } |
|---|
| | 876 | } |
|---|
| r3393 |
r3447 |
|
| 14 | 14 | |
|---|
| 15 | 15 | /// The Socket Conduit that is used to send commands. |
|---|
| 16 | | SocketConduit socket; |
|---|
| | 16 | SocketConduit socket_; |
|---|
| | 17 | char[8 * 1024] dst; //for reading from the socket_ |
|---|
| 17 | 18 | |
|---|
| 18 | 19 | abstract void exception(char[] message); |
|---|
| … | … | |
| 30 | 31 | /// buf = the bytes to send |
|---|
| 31 | 32 | void sendData(void[] buf) { |
|---|
| 32 | | socket.write(buf); |
|---|
| | 33 | socket_.write(buf); |
|---|
| 33 | 34 | } |
|---|
| 34 | 35 | |
|---|
| … | … | |
| 37 | 38 | /// Returns: the line read |
|---|
| 38 | 39 | char[] readLine() { |
|---|
| 39 | | void[8 * 1024] dst; |
|---|
| 40 | | socket.read(dst); |
|---|
| 41 | | return dst; |
|---|
| | 40 | socket_.read(dst); |
|---|
| | 41 | char[] toReturn = dst.dup; |
|---|
| | 42 | return toReturn; |
|---|
| 42 | 43 | } |
|---|
| 43 | 44 | |
|---|
| … | … | |
| 48 | 49 | /// returns the SocketConduit this class is using |
|---|
| 49 | 50 | SocketConduit findAvailableServer(char[] hostname, int port) { |
|---|
| 50 | | socket = new SocketConduit(); |
|---|
| 51 | | socket.connect(new InternetAddress(hostname, port)); |
|---|
| | 51 | socket_ = new SocketConduit(); |
|---|
| | 52 | socket_.connect(new InternetAddress(hostname, port)); |
|---|
| 52 | 53 | |
|---|
| 53 | | return socket; |
|---|
| | 54 | return socket_; |
|---|
| 54 | 55 | } |
|---|
| 55 | 56 | |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic