Socket null handling (#1610)

* return null if in connection failure state

* reconnect on connection failure

* better connection handling

* probably not necessary but just incase

* connection handling updates

* add cancelOnError: true
This commit is contained in:
Matthew Fosse 2024-08-17 19:28:45 -04:00 committed by GitHub
parent 83ef61e928
commit 7c9b72483a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 36 deletions

View file

@ -66,6 +66,7 @@ class ElectrumClient {
try {
await socket?.close();
socket = null;
} catch (_) {}
try {
@ -90,33 +91,40 @@ class ElectrumClient {
}
_setConnectionStatus(ConnectionStatus.connected);
socket!.listen((Uint8List event) {
try {
final msg = utf8.decode(event.toList());
final messagesList = msg.split("\n");
for (var message in messagesList) {
if (message.isEmpty) {
continue;
socket!.listen(
(Uint8List event) {
try {
final msg = utf8.decode(event.toList());
final messagesList = msg.split("\n");
for (var message in messagesList) {
if (message.isEmpty) {
continue;
}
_parseResponse(message);
}
_parseResponse(message);
} catch (e) {
print(e.toString());
}
} catch (e) {
print(e.toString());
}
}, onError: (Object error) {
final errorMsg = error.toString();
print(errorMsg);
unterminatedString = '';
},
onError: (Object error) {
socket = null;
final errorMsg = error.toString();
print(errorMsg);
unterminatedString = '';
final currentHost = socket?.address.host;
final isErrorForCurrentHost = errorMsg.contains(" ${currentHost} ");
final currentHost = socket?.address.host;
final isErrorForCurrentHost = errorMsg.contains(" ${currentHost} ");
if (currentHost != null && isErrorForCurrentHost)
_setConnectionStatus(ConnectionStatus.failed);
}, onDone: () {
unterminatedString = '';
if (host == socket?.address.host) _setConnectionStatus(ConnectionStatus.disconnected);
});
if (currentHost != null && isErrorForCurrentHost)
_setConnectionStatus(ConnectionStatus.failed);
},
onDone: () {
socket = null;
unterminatedString = '';
if (host == socket?.address.host) _setConnectionStatus(ConnectionStatus.disconnected);
},
cancelOnError: true,
);
keepAlive();
}

View file

@ -218,10 +218,7 @@ abstract class ElectrumWalletBase
if (electrumClient.isConnected) {
syncStatus = SyncedSyncStatus();
} else {
if (electrumClient.uri != null) {
await electrumClient.connectToUri(electrumClient.uri!, useSSL: electrumClient.useSSL);
startSync();
}
syncStatus = NotConnectedSyncStatus();
}
}
}
@ -265,6 +262,7 @@ abstract class ElectrumWalletBase
Future<Isolate>? _isolate;
void Function(FlutterErrorDetails)? _onError;
Timer? _reconnectTimer;
Timer? _autoSaveTimer;
static const int _autoSaveInterval = 30;
@ -1980,13 +1978,6 @@ abstract class ElectrumWalletBase
break;
case ConnectionStatus.failed:
syncStatus = LostConnectionSyncStatus();
// wait for 5 seconds and then try to reconnect:
Future.delayed(Duration(seconds: 5), () {
electrumClient.connectToUri(
node!.uri,
useSSL: node!.useSSL ?? false,
);
});
break;
case ConnectionStatus.connecting:
syncStatus = ConnectingSyncStatus();
@ -1996,7 +1987,11 @@ abstract class ElectrumWalletBase
}
void _syncStatusReaction(SyncStatus syncStatus) async {
if (syncStatus is NotConnectedSyncStatus) {
if (syncStatus is SyncingSyncStatus) {
return;
}
if (syncStatus is NotConnectedSyncStatus || syncStatus is LostConnectionSyncStatus) {
// Needs to re-subscribe to all scripthashes when reconnected
_scripthashesUpdateSubject = {};
@ -2004,7 +1999,8 @@ abstract class ElectrumWalletBase
_isTryingToConnect = true;
Future.delayed(Duration(seconds: 10), () {
_reconnectTimer?.cancel();
_reconnectTimer = Timer(Duration(seconds: 10), () {
if (this.syncStatus is! SyncedSyncStatus && this.syncStatus is! SyncedTipSyncStatus) {
this.electrumClient.connectToUri(
node!.uri,