Skip to content

Commit

Permalink
feat: allow to specify connection timeout in high level API and incre…
Browse files Browse the repository at this point in the history
…ase default timeout
  • Loading branch information
robert-virkus committed Nov 23, 2023
1 parent f15e37c commit fb4c0f0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
21 changes: 14 additions & 7 deletions lib/src/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,11 @@ class MailClient {
/// Connects and authenticates with the specified incoming mail server.
///
/// Also compare [disconnect].
Future<void> connect() async {
///
/// Specify a [timeout] for the connection, defaults to 20 seconds.
Future<void> connect({Duration timeout = const Duration(seconds: 20)}) async {
await _prepareConnect();
await _incomingMailClient.connect();
await _incomingMailClient.connect(timeout: timeout);
_isConnected = true;
}

Expand Down Expand Up @@ -1678,7 +1680,7 @@ abstract class _IncomingMailClient {

Id? get serverId => null;

Future<void> connect();
Future<void> connect({Duration timeout = const Duration(seconds: 20)});

Future<void> disconnect();

Expand Down Expand Up @@ -2065,13 +2067,14 @@ class _IncomingImapClient extends _IncomingMailClient {
}

@override
Future<void> connect() async {
Future<void> connect({Duration timeout = const Duration(seconds: 20)}) async {
final serverConfig = _config.serverConfig;
final isSecure = serverConfig.socketType == SocketType.ssl;
await _imapClient.connectToServer(
serverConfig.hostname!,
serverConfig.port!,
isSecure: isSecure,
timeout: timeout,
);
if (!isSecure) {
if (_imapClient.serverInfo.supportsStartTls &&
Expand Down Expand Up @@ -3096,11 +3099,15 @@ class _IncomingPopClient extends _IncomingMailClient {
final PopClient _popClient;

@override
Future<void> connect() async {
Future<void> connect({Duration timeout = const Duration(seconds: 20)}) async {
final serverConfig = _config.serverConfig;
final isSecure = serverConfig.socketType == SocketType.ssl;
await _popClient.connectToServer(serverConfig.hostname!, serverConfig.port!,
isSecure: isSecure);
await _popClient.connectToServer(
serverConfig.hostname!,
serverConfig.port!,
isSecure: isSecure,
timeout: timeout,
);
if (!isSecure) {
//TODO check POP3 server capabilities first
if (serverConfig.socketType != SocketType.plainNoStartTls) {
Expand Down
12 changes: 9 additions & 3 deletions lib/src/private/util/client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@ abstract class ClientBase {
/// Connects to the specified server.
///
/// Specify [isSecure] if you do not want to connect to a secure service.
///
/// Specify [timeout] to specify a different timeout for the connection.
/// This defaults to 20 seconds.
Future<ConnectionInfo> connectToServer(
String host,
int port, {
bool isSecure = true,
Duration timeout = const Duration(seconds: 10),
Duration timeout = const Duration(seconds: 20),
}) async {
logApp('connecting to server $host:$port - '
'secure: $isSecure, timeout: $timeout');
logApp(
'connecting to server $host:$port - '
'secure: $isSecure, timeout: $timeout',
);
connectionInfo = ConnectionInfo(host, port, isSecure: isSecure);
final socket = isSecure
? await SecureSocket.connect(
Expand All @@ -110,6 +115,7 @@ abstract class ClientBase {
_greetingsCompleter = Completer<ConnectionInfo>();
_isServerGreetingDone = false;
connect(socket);

return _greetingsCompleter.future;
}

Expand Down
14 changes: 10 additions & 4 deletions lib/src/private/util/discover_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,16 @@ class DiscoverHelper {
try {
// ignore: close_sinks
final socket = info.isSecure
? await SecureSocket.connect(info.host, info.port,
timeout: const Duration(seconds: 10))
: await Socket.connect(info.host, info.port,
timeout: const Duration(seconds: 10));
? await SecureSocket.connect(
info.host,
info.port,
timeout: const Duration(seconds: 10),
)
: await Socket.connect(
info.host,
info.port,
timeout: const Duration(seconds: 10),
);
info.socket = socket;
if (isLogEnabled) {
print('success at ${info.host}:${info.port}');
Expand Down

0 comments on commit fb4c0f0

Please sign in to comment.