mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 09:47:37 +00:00
stream subscription
This commit is contained in:
parent
81689426e2
commit
4c12e870b1
1 changed files with 33 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:stackwallet/utilities/logger.dart';
|
||||
|
||||
|
@ -18,6 +19,8 @@ class JsonRPC {
|
|||
final Duration connectionTimeout;
|
||||
Socket? socket;
|
||||
|
||||
StreamSubscription<Uint8List>? _subscription;
|
||||
|
||||
Future<dynamic> request(String jsonRpcRequest) async {
|
||||
final completer = Completer<dynamic>();
|
||||
final List<int> responseData = [];
|
||||
|
@ -36,8 +39,10 @@ class JsonRPC {
|
|||
.log("JsonRPC json.decode: $e\n$s", level: LogLevel.Error);
|
||||
completer.completeError(e, s);
|
||||
} finally {
|
||||
Logging.instance
|
||||
.log("JsonRPC dataHandler: not destroying socket ${socket?.address}:${socket?.port}", level: LogLevel.Info);
|
||||
Logging.instance.log(
|
||||
"JsonRPC dataHandler: not destroying socket ${socket?.address}:${socket?.port}",
|
||||
level: LogLevel.Info,
|
||||
);
|
||||
// socket?.destroy();
|
||||
// TODO is this all we need to do?
|
||||
}
|
||||
|
@ -48,15 +53,19 @@ class JsonRPC {
|
|||
Logging.instance
|
||||
.log("JsonRPC errorHandler: $error\n$trace", level: LogLevel.Error);
|
||||
completer.completeError(error, trace);
|
||||
Logging.instance
|
||||
.log("JsonRPC errorHandler: not destroying socket ${socket?.address}:${socket?.port}", level: LogLevel.Info);
|
||||
Logging.instance.log(
|
||||
"JsonRPC errorHandler: not destroying socket ${socket?.address}:${socket?.port}",
|
||||
level: LogLevel.Info,
|
||||
);
|
||||
// socket?.destroy();
|
||||
// TODO do we need to recreate the socket?
|
||||
}
|
||||
|
||||
void doneHandler() {
|
||||
Logging.instance
|
||||
.log("JsonRPC doneHandler: not destroying socket ${socket?.address}:${socket?.port}", level: LogLevel.Info);
|
||||
Logging.instance.log(
|
||||
"JsonRPC doneHandler: not destroying socket ${socket?.address}:${socket?.port}",
|
||||
level: LogLevel.Info,
|
||||
);
|
||||
// socket?.destroy();
|
||||
// TODO is this all we need?
|
||||
}
|
||||
|
@ -67,14 +76,25 @@ class JsonRPC {
|
|||
|
||||
if (useSSL) {
|
||||
socket ??= await SecureSocket.connect(host, port,
|
||||
timeout: connectionTimeout,
|
||||
onBadCertificate: (_) => true);
|
||||
socket!.listen(dataHandler,
|
||||
onError: errorHandler, onDone: doneHandler, cancelOnError: true);
|
||||
timeout: connectionTimeout, onBadCertificate: (_) => true);
|
||||
_subscription ??= socket!.listen(
|
||||
dataHandler,
|
||||
onError: errorHandler,
|
||||
onDone: doneHandler,
|
||||
cancelOnError: true,
|
||||
);
|
||||
} else {
|
||||
socket ??= await Socket.connect(host, port, timeout: connectionTimeout);
|
||||
socket!.listen(dataHandler,
|
||||
onError: errorHandler, onDone: doneHandler, cancelOnError: true);
|
||||
socket ??= await Socket.connect(
|
||||
host,
|
||||
port,
|
||||
timeout: connectionTimeout,
|
||||
);
|
||||
_subscription ??= socket!.listen(
|
||||
dataHandler,
|
||||
onError: errorHandler,
|
||||
onDone: doneHandler,
|
||||
cancelOnError: true,
|
||||
);
|
||||
}
|
||||
|
||||
socket?.write('$jsonRpcRequest\r\n');
|
||||
|
|
Loading…
Reference in a new issue