cache node information for reconnection purposes when tor toggled

and cancel alive timer when needed (avoids secureSocket not initialized error)
This commit is contained in:
sneurlax 2024-02-05 15:22:18 -06:00
parent dbaf184bb8
commit d48c7cf9f1

View file

@ -56,6 +56,9 @@ class SubscribableElectrumXClient {
bool get isConnected => _isConnected;
bool get useSSL => _useSSL;
// Used to reconnect.
String? _host;
int? _port;
void Function(bool)? onConnectionStatusChanged;
@ -107,6 +110,7 @@ class SubscribableElectrumXClient {
);
// Listen to global event bus for Tor preference changes.
try {
_torPreferenceListener = bus.on<TorPreferenceChangedEvent>().listen(
(event) async {
// Close open socket (if open).
@ -121,8 +125,14 @@ class SubscribableElectrumXClient {
// Clear subscriptions.
_tasks.clear();
// Cancel alive timer
_aliveTimer?.cancel();
},
);
} catch (e, s) {
print(s);
}
}
factory SubscribableElectrumXClient.from({
@ -158,6 +168,10 @@ class SubscribableElectrumXClient {
required String host,
required int port,
}) async {
// Cache node information.
_host = host;
_port = port;
// If we're already connected, disconnect first.
try {
await _socket?.close();
@ -567,19 +581,46 @@ class SubscribableElectrumXClient {
// Check socket is connected.
if (_prefs.useTor) {
if (_socksSocket == null) {
final msg = "SubscribableElectrumXClient._call: "
"SOCKSSocket is not connected. Method $method, params $params.";
try {
if (_host == null || _port == null) {
throw Exception("No host or port provided");
}
// Attempt to conect.
await connect(
host: _host!,
port: _port!,
);
} catch (e, s) {
final msg = "SubscribableElectrumXClient._callWithTimeout: "
"SOCKSSocket not connected and cannot connect. "
"Method $method, params $params."
"\nError: $e\nStack trace: $s";
Logging.instance.log(msg, level: LogLevel.Fatal);
throw Exception(msg);
}
}
} else {
if (_socket == null) {
final msg = "SubscribableElectrumXClient._call: "
"Socket is not connected. Method $method, params $params.";
try {
if (_host == null || _port == null) {
throw Exception("No host or port provided");
}
// Attempt to conect.
await connect(
host: _host!,
port: _port!,
);
} catch (e, s) {
final msg = "SubscribableElectrumXClient._callWithTimeout: "
"Socket not connected and cannot connect. "
"Method $method, params $params.";
Logging.instance.log(msg, level: LogLevel.Fatal);
throw Exception(msg);
}
}
}
final completer = Completer<dynamic>();
_currentRequestID++;