Skip to content

Commit

Permalink
Socket null handling (#1610)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fossephate authored Aug 17, 2024
1 parent 83ef61e commit 7c9b724
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
54 changes: 31 additions & 23 deletions cw_bitcoin/lib/electrum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ElectrumClient {

try {
await socket?.close();
socket = null;
} catch (_) {}

try {
Expand All @@ -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();
}
Expand Down
22 changes: 9 additions & 13 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -265,6 +262,7 @@ abstract class ElectrumWalletBase
Future<Isolate>? _isolate;

void Function(FlutterErrorDetails)? _onError;
Timer? _reconnectTimer;
Timer? _autoSaveTimer;
static const int _autoSaveInterval = 30;

Expand Down Expand Up @@ -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();
Expand All @@ -1996,15 +1987,20 @@ 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 = {};

if (_isTryingToConnect) return;

_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,
Expand Down

0 comments on commit 7c9b724

Please sign in to comment.