fix: report isClosed from ProxySocket and use it to reconnect (#2617)

This commit is contained in:
cyan
2025-10-30 02:16:18 +01:00
committed by GitHub
parent 55bae6c581
commit 00b99a9fa0
5 changed files with 21 additions and 23 deletions
+4 -7
View File
@@ -35,7 +35,6 @@ class SocketTask {
class ElectrumClient {
ElectrumClient()
: _id = 0,
_isConnected = false,
_tasks = {},
_errors = {},
unterminatedString = '';
@@ -43,7 +42,7 @@ class ElectrumClient {
static const connectionTimeout = Duration(seconds: 5);
static const aliveTimerDuration = Duration(seconds: 4);
bool get isConnected => _isConnected && socket != null;
bool get isConnected => socket != null && socket?.isClosed == false;
ProxySocket? socket;
void Function(ConnectionStatus)? onConnectionStatusChange;
int _id;
@@ -51,7 +50,6 @@ class ElectrumClient {
Map<String, SocketTask> get tasks => _tasks;
final Map<String, String> _errors;
ConnectionStatus _connectionStatus = ConnectionStatus.disconnected;
bool _isConnected;
Timer? _aliveTimer;
String unterminatedString;
@@ -451,7 +449,7 @@ class ElectrumClient {
Future<dynamic> call(
{required String method, List<Object> params = const [], Function(int)? idCallback}) async {
if (!_isConnected || socket == null) return null;
if (!isConnected) return null;
final completer = Completer<dynamic>();
_id += 1;
@@ -466,7 +464,7 @@ class ElectrumClient {
Future<dynamic> callWithTimeout(
{required String method, List<Object> params = const [], int timeout = 5000}) async {
try {
if (!_isConnected || socket == null) return null;
if (!isConnected) return null;
final completer = Completer<dynamic>();
_id += 1;
@@ -564,8 +562,7 @@ class ElectrumClient {
void _setConnectionStatus(ConnectionStatus status) {
onConnectionStatusChange?.call(status);
_connectionStatus = status;
_isConnected = status == ConnectionStatus.connected;
if (!_isConnected) {
if (!isConnected) {
try {
socket?.destroy();
} catch (_) {}
@@ -42,6 +42,7 @@ abstract class ProxySocket {
Future<void> close();
void destroy();
void write(String data);
bool get isClosed;
StreamSubscription<List<int>> listen(Function(Uint8List event) onData, {Function (Object error)? onError, Function ()? onDone, bool cancelOnError = true});
ProxyAddress get address;
}
+6 -6
View File
@@ -10,15 +10,15 @@ class ProxySocketInsecure implements ProxySocket {
ProxySocketInsecure(this.socket);
bool _isClosed = false;
bool isClosed = false;
ProxyAddress get address => ProxyAddress(host: socket.remoteAddress.host, port: socket.remotePort);
@override
Future<void> close() async {
try {
if (_isClosed) return;
_isClosed = true;
if (isClosed) return;
isClosed = true;
return socket.close();
} catch (e) {
printV("ProxySocketInsecure: close: $e");
@@ -29,8 +29,8 @@ class ProxySocketInsecure implements ProxySocket {
@override
void destroy() async {
try {
if (_isClosed) return;
_isClosed = true;
if (isClosed) return;
isClosed = true;
socket.destroy();
} catch (e) {
printV("ProxySocketInsecure: destroy: $e");
@@ -41,7 +41,7 @@ class ProxySocketInsecure implements ProxySocket {
@override
void write(String data) {
try {
if (_isClosed) {
if (isClosed) {
printV("ProxySocketInsecure: write: socket is closed");
return;
}
+6 -6
View File
@@ -8,7 +8,7 @@ import 'package:cw_core/utils/proxy_socket/abstract.dart';
class ProxySocketSecure implements ProxySocket {
final SecureSocket socket;
bool _isClosed = false;
bool isClosed = false;
ProxySocketSecure(this.socket);
@@ -17,8 +17,8 @@ class ProxySocketSecure implements ProxySocket {
@override
Future<void> close() async {
try {
if (_isClosed) return;
_isClosed = true;
if (isClosed) return;
isClosed = true;
return socket.close();
} catch (e) {
printV("ProxySocketSecure: close: $e");
@@ -29,8 +29,8 @@ class ProxySocketSecure implements ProxySocket {
@override
void destroy() async {
try {
if (_isClosed) return;
_isClosed = true;
if (isClosed) return;
isClosed = true;
socket.destroy();
} catch (e) {
printV("ProxySocketSecure: destroy: $e");
@@ -41,7 +41,7 @@ class ProxySocketSecure implements ProxySocket {
@override
void write(String data) {
try {
if (_isClosed) {
if (isClosed) {
printV("ProxySocketSecure: write: socket is closed");
return;
}
+4 -4
View File
@@ -7,7 +7,7 @@ import 'package:socks_socket/socks_socket.dart';
class ProxySocketSocks implements ProxySocket {
final SOCKSSocket socket;
bool _isClosed = false;
bool isClosed = false;
ProxySocketSocks(this.socket);
@override
@@ -16,8 +16,8 @@ class ProxySocketSocks implements ProxySocket {
@override
Future<void> close() async {
try {
if (_isClosed) return;
_isClosed = true;
if (isClosed) return;
isClosed = true;
await socket.close();
} catch (e) {
printV("ProxySocketSocks: close: $e");
@@ -31,7 +31,7 @@ class ProxySocketSocks implements ProxySocket {
@override
void write(String data) {
try {
if (_isClosed) {
if (isClosed) {
printV("ProxySocketSocks: write: socket is closed");
return;
}