Skip to content

Commit

Permalink
Fix crash on reconnect & ignore rtt topic ID (#8)
Browse files Browse the repository at this point in the history
* Ensure proper cleanup when reconnecting

* wait for rtt to connect before continuing

* ignore rtt message topic ID

* update version and changelog
  • Loading branch information
mjansen4857 authored Dec 22, 2023
1 parent 75f1f42 commit cb432be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.2

- Fix crash on reconnecting to server
- Ignore rtt protocol topic ID

## 1.3.1

- Fix connections being timed out too quickly
Expand Down
40 changes: 26 additions & 14 deletions lib/src/nt4_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class NT4Client {
int _timeoutInterval = _pingTimeoutMsV40;

WebSocketChannel? _mainWebsocket;
StreamSubscription<dynamic>? _mainWebsocketSub;
WebSocketChannel? _rttWebsocket;
StreamSubscription<dynamic>? _rttWebsocketSub;

/// Create an NT4 client. This will connect to an NT4 server running at
/// [serverBaseAddress]. This should be either 'localhost' if running a
Expand Down Expand Up @@ -450,14 +452,14 @@ class NT4Client {
_useRTT = true;
_pingInterval = _pingIntervalMsV41;
_timeoutInterval = _pingTimeoutMsV41;
_rttConnect();
await _rttConnect();
} else {
_useRTT = false;
_pingInterval = _pingIntervalMsV40;
_timeoutInterval = _pingTimeoutMsV40;
}

_mainWebsocket!.stream.listen(
_mainWebsocketSub = _mainWebsocket!.stream.listen(
(data) {
if (!_serverConnectionActive) {
_lastAnnouncedValues.clear();
Expand All @@ -470,10 +472,11 @@ class NT4Client {
},
onDone: _wsOnClose,
onError: (err) {},
cancelOnError: true,
);

NT4Topic timeTopic = NT4Topic(
name: "Time",
name: 'Time',
type: NT4TypeStr.typeInt,
id: -1,
pubUID: -1,
Expand All @@ -500,7 +503,7 @@ class NT4Client {
}
}

void _rttConnect() async {
Future<void> _rttConnect() async {
if (!_useRTT || _rttConnectionActive) {
return;
}
Expand All @@ -523,32 +526,34 @@ class NT4Client {

_rttConnectionActive = true;

_rttWebsocket!.stream.listen(
_rttWebsocketSub = _rttWebsocket!.stream.listen(
(data) {
if (data is! List<int>) {
return;
}

var msg = Unpacker.fromList(data).unpackList();

int topicID = msg[0] as int;
// rtt socket will only send timestamps, we can ignore the topic ID
int timestampUS = msg[1] as int;
var value = msg[3];

if (value is! int) {
return;
}

if (topicID == -1) {
_wsHandleRecieveTimestamp(timestampUS, value);
}
_wsHandleRecieveTimestamp(timestampUS, value);
},
onDone: _rttOnClose,
onError: (error) {},
cancelOnError: true,
);
}

void _rttOnClose() {
_rttWebsocket?.sink.close();
void _rttOnClose() async {
await _rttWebsocketSub?.cancel();
_rttWebsocketSub = null;
await _rttWebsocket?.sink.close();
_rttWebsocket = null;

_lastReceivedTime = 0;
Expand All @@ -557,9 +562,16 @@ class NT4Client {
_useRTT = false;
}

void _wsOnClose() {
_mainWebsocket?.sink.close();
_rttWebsocket?.sink.close();
void _wsOnClose() async {
_pingTimer?.cancel();
_pongTimer?.cancel();

await _mainWebsocketSub?.cancel();
_mainWebsocketSub = null;
await _mainWebsocket?.sink.close();
await _rttWebsocketSub?.cancel();
_rttWebsocketSub = null;
await _rttWebsocket?.sink.close();

_mainWebsocket = null;
_rttWebsocket = null;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nt4
description: Dart implementation of the WPILib NT4 protocol
version: 1.3.1
version: 1.3.2
repository: https://github.com/mjansen4857/nt4

environment:
Expand Down

0 comments on commit cb432be

Please sign in to comment.