some clean up

This commit is contained in:
julian 2023-05-25 15:29:23 -06:00
parent 72c00cbea7
commit e72e59a50a

View file

@ -26,7 +26,6 @@ class JsonRPC {
void _dataHandler(List<int> data) { void _dataHandler(List<int> data) {
if (_requestQueue.isEmpty) { if (_requestQueue.isEmpty) {
// probably just return although this case should never actually hit // probably just return although this case should never actually hit
// TODO anything else here?
return; return;
} }
@ -52,20 +51,23 @@ class JsonRPC {
void _doneHandler() { void _doneHandler() {
Logging.instance.log( Logging.instance.log(
"JsonRPC doneHandler: " "JsonRPC doneHandler: "
"connection closed to ${_socket?.address}:${_socket?.port}, destroying socket", "connection closed to ${_socket?.address}:$port, destroying socket",
level: LogLevel.Info, level: LogLevel.Info,
); );
_socket?.destroy();
_socket = null; // is this redundant?
// should we also cancel and/or null the subscription?
if (_requestQueue.isNotEmpty) { if (_requestQueue.isNotEmpty) {
Logging.instance.log( Logging.instance.log(
"JsonRPC doneHandler: queue not empty but connection closed, completing pending requests with errors", "JsonRPC doneHandler: queue not empty but connection closed, "
"completing pending requests with errors",
level: LogLevel.Error, level: LogLevel.Error,
); );
_errorPendingRequests(); _requestQueue.clear(
errorMessage: "JsonRPC doneHandler: socket closed "
"before request could complete",
);
} }
disconnect();
} }
Future<void> _onReqCompleted(_JsonRPCRequest req) async { Future<void> _onReqCompleted(_JsonRPCRequest req) async {
@ -91,25 +93,6 @@ class JsonRPC {
); );
} }
void _errorPendingRequests() {
if (_requestQueue.isNotEmpty) {
final req = _requestQueue.next;
if (!(req.isComplete)) {
req.completer.completeError('JsonRPC doneHandler: socket closed before request could complete');
_requestQueue.remove(req).then((ret) {
if (_requestQueue.isNotEmpty) {
_errorPendingRequests();
}
});
}
} else {
Logging.instance.log(
"JsonRPC _errorPendingRequests: done completing pending requests with errors",
level: LogLevel.Info,
);
}
}
Future<dynamic> request(String jsonRpcRequest) async { Future<dynamic> request(String jsonRpcRequest) async {
// todo: handle this better? // todo: handle this better?
// Do we need to check the subscription, too? // Do we need to check the subscription, too?
@ -134,7 +117,7 @@ class JsonRPC {
} else { } else {
Logging.instance.log( Logging.instance.log(
"JsonRPC request: queued request $jsonRpcRequest " "JsonRPC request: queued request $jsonRpcRequest "
"to socket ${_socket?.address}:${_socket?.port}", "to socket ${_socket?.address}:$port",
level: LogLevel.Info, level: LogLevel.Info,
); );
} }
@ -142,10 +125,10 @@ class JsonRPC {
return req.completer.future; return req.completer.future;
} }
Future<void> disconnect() async { void disconnect() {
await _subscription?.cancel(); _subscription?.cancel().then((_) => _subscription = null);
_subscription = null;
_socket?.destroy(); _socket?.destroy();
_socket = null;
} }
Future<void> connect() async { Future<void> connect() async {
@ -186,6 +169,18 @@ class _JsonRPCRequestQueue {
await _m.protect(() async => _rq.remove(req)); await _m.protect(() async => _rq.remove(req));
} }
Future<void> clear({required String errorMessage}) async {
await _m.protect(() async {
for (final req in _rq) {
if (!req.isComplete) {
req.completer.completeError(errorMessage);
}
}
_rq.clear();
});
}
bool get isEmpty => _rq.isEmpty; bool get isEmpty => _rq.isEmpty;
bool get isNotEmpty => _rq.isNotEmpty; bool get isNotEmpty => _rq.isNotEmpty;
int get length => _rq.length; int get length => _rq.length;