mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-03 17:40:43 +00:00
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:
parent
83ef61e928
commit
7c9b72483a
2 changed files with 40 additions and 36 deletions
|
@ -66,6 +66,7 @@ class ElectrumClient {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await socket?.close();
|
await socket?.close();
|
||||||
|
socket = null;
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -90,7 +91,8 @@ class ElectrumClient {
|
||||||
}
|
}
|
||||||
_setConnectionStatus(ConnectionStatus.connected);
|
_setConnectionStatus(ConnectionStatus.connected);
|
||||||
|
|
||||||
socket!.listen((Uint8List event) {
|
socket!.listen(
|
||||||
|
(Uint8List event) {
|
||||||
try {
|
try {
|
||||||
final msg = utf8.decode(event.toList());
|
final msg = utf8.decode(event.toList());
|
||||||
final messagesList = msg.split("\n");
|
final messagesList = msg.split("\n");
|
||||||
|
@ -103,7 +105,9 @@ class ElectrumClient {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print(e.toString());
|
print(e.toString());
|
||||||
}
|
}
|
||||||
}, onError: (Object error) {
|
},
|
||||||
|
onError: (Object error) {
|
||||||
|
socket = null;
|
||||||
final errorMsg = error.toString();
|
final errorMsg = error.toString();
|
||||||
print(errorMsg);
|
print(errorMsg);
|
||||||
unterminatedString = '';
|
unterminatedString = '';
|
||||||
|
@ -113,10 +117,14 @@ class ElectrumClient {
|
||||||
|
|
||||||
if (currentHost != null && isErrorForCurrentHost)
|
if (currentHost != null && isErrorForCurrentHost)
|
||||||
_setConnectionStatus(ConnectionStatus.failed);
|
_setConnectionStatus(ConnectionStatus.failed);
|
||||||
}, onDone: () {
|
},
|
||||||
|
onDone: () {
|
||||||
|
socket = null;
|
||||||
unterminatedString = '';
|
unterminatedString = '';
|
||||||
if (host == socket?.address.host) _setConnectionStatus(ConnectionStatus.disconnected);
|
if (host == socket?.address.host) _setConnectionStatus(ConnectionStatus.disconnected);
|
||||||
});
|
},
|
||||||
|
cancelOnError: true,
|
||||||
|
);
|
||||||
|
|
||||||
keepAlive();
|
keepAlive();
|
||||||
}
|
}
|
||||||
|
|
|
@ -218,10 +218,7 @@ abstract class ElectrumWalletBase
|
||||||
if (electrumClient.isConnected) {
|
if (electrumClient.isConnected) {
|
||||||
syncStatus = SyncedSyncStatus();
|
syncStatus = SyncedSyncStatus();
|
||||||
} else {
|
} else {
|
||||||
if (electrumClient.uri != null) {
|
syncStatus = NotConnectedSyncStatus();
|
||||||
await electrumClient.connectToUri(electrumClient.uri!, useSSL: electrumClient.useSSL);
|
|
||||||
startSync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -265,6 +262,7 @@ abstract class ElectrumWalletBase
|
||||||
Future<Isolate>? _isolate;
|
Future<Isolate>? _isolate;
|
||||||
|
|
||||||
void Function(FlutterErrorDetails)? _onError;
|
void Function(FlutterErrorDetails)? _onError;
|
||||||
|
Timer? _reconnectTimer;
|
||||||
Timer? _autoSaveTimer;
|
Timer? _autoSaveTimer;
|
||||||
static const int _autoSaveInterval = 30;
|
static const int _autoSaveInterval = 30;
|
||||||
|
|
||||||
|
@ -1980,13 +1978,6 @@ abstract class ElectrumWalletBase
|
||||||
break;
|
break;
|
||||||
case ConnectionStatus.failed:
|
case ConnectionStatus.failed:
|
||||||
syncStatus = LostConnectionSyncStatus();
|
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;
|
break;
|
||||||
case ConnectionStatus.connecting:
|
case ConnectionStatus.connecting:
|
||||||
syncStatus = ConnectingSyncStatus();
|
syncStatus = ConnectingSyncStatus();
|
||||||
|
@ -1996,7 +1987,11 @@ abstract class ElectrumWalletBase
|
||||||
}
|
}
|
||||||
|
|
||||||
void _syncStatusReaction(SyncStatus syncStatus) async {
|
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
|
// Needs to re-subscribe to all scripthashes when reconnected
|
||||||
_scripthashesUpdateSubject = {};
|
_scripthashesUpdateSubject = {};
|
||||||
|
|
||||||
|
@ -2004,7 +1999,8 @@ abstract class ElectrumWalletBase
|
||||||
|
|
||||||
_isTryingToConnect = true;
|
_isTryingToConnect = true;
|
||||||
|
|
||||||
Future.delayed(Duration(seconds: 10), () {
|
_reconnectTimer?.cancel();
|
||||||
|
_reconnectTimer = Timer(Duration(seconds: 10), () {
|
||||||
if (this.syncStatus is! SyncedSyncStatus && this.syncStatus is! SyncedTipSyncStatus) {
|
if (this.syncStatus is! SyncedSyncStatus && this.syncStatus is! SyncedTipSyncStatus) {
|
||||||
this.electrumClient.connectToUri(
|
this.electrumClient.connectToUri(
|
||||||
node!.uri,
|
node!.uri,
|
||||||
|
|
Loading…
Reference in a new issue