Skip to content

Commit

Permalink
Timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualmarc committed Feb 8, 2020
1 parent 41f9dbb commit 3b643f3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Rename files and folders
- Delete files
- Error correction for file download & upload
- Timeout for response from server

## 0.2.0

Expand Down
2 changes: 2 additions & 0 deletions lib/src/commands/fileupload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ class FileUpload {
_socket.readResponse();

_log.log('File Uploaded!');

_socket.readResponse(true);
}
}
6 changes: 4 additions & 2 deletions lib/src/ftpclient_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ class FTPClient {
/// [user]: Username (Defaults to anonymous)
/// [pass]: Password if not anonymous login
/// [debug]: Enable Debug Logging
/// [timeout]: Timeout in secods to wait for responses
FTPClient(String host,
{int port = 21,
String user = 'anonymous',
String pass = '',
bool debug = false}) {
bool debug = false,
int timeout = 30}) {
_user = user;
_pass = pass;

Expand All @@ -38,7 +40,7 @@ class FTPClient {
_log = NoOpLogger();
}

_socket = FTPSocket(host, port, _log);
_socket = FTPSocket(host, port, _log, timeout);
}

/// Connect to the FTP Server
Expand Down
8 changes: 7 additions & 1 deletion lib/src/ftpsocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ class FTPSocket {
final String host;
final int port;
final DebugLog _log;
final int _timeout;

RawSynchronousSocket _socket;

FTPSocket(this.host, this.port, this._log);
FTPSocket(this.host, this.port, this._log, this._timeout);

/// Read the FTP Server response from the Stream
///
/// Blocks until data is received!
String readResponse([bool bOptional = false]) {
int iToRead = 0;
StringBuffer buffer = StringBuffer();
int iStart = DateTime.now().millisecondsSinceEpoch;

do {
if (iToRead > 0) {
Expand All @@ -31,6 +33,10 @@ class FTPSocket {
iToRead = _socket.available();

if (iToRead == 0 && buffer.length == 0) {
int iCurrent = DateTime.now().millisecondsSinceEpoch;
if (iCurrent - iStart > _timeout * 1000) {
throw FTPException('Timeout reached for Receive', '');
}
sleep(Duration(milliseconds: 100));
}
} while (iToRead > 0 || (buffer.length == 0 && !bOptional));
Expand Down

0 comments on commit 3b643f3

Please sign in to comment.