Skip to content

Commit

Permalink
Merge pull request #22 from atsign-foundation/fix-socket-closed-while…
Browse files Browse the repository at this point in the history
…-other-side-still-writing

fix: correctly handle situation where a socket has been closed but the other side is still writing to it
  • Loading branch information
XavierChanth authored Nov 12, 2024
2 parents a0e4285 + 75b738b commit 4128bcf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
## 2.3.1

- fix: correctly handle situation where a socket has been closed but the other
side is still writing to it

## 2.3.0

- feat: Add `authTimeout` property to SocketConnector. Previously this was
- hard-coded to 5 seconds which is a bit restrictive for slow network
hard-coded to 5 seconds which is a bit restrictive for slow network
connections. It now defaults to 30 seconds but may be set at time of
construction
- feat: added `authTimeout` parameter to `serverToServer`; this is provided
Expand Down
20 changes: 17 additions & 3 deletions lib/src/socket_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,17 @@ class SocketConnector {
StreamController<Uint8List> sc = StreamController<Uint8List>();
side.farSide!.sink = sc;
Stream<List<int>> transformed = side.transformer!(sc.stream);
transformed.listen(side.farSide!.socket.add);
transformed.listen((event) {
try {
side.farSide!.socket.add(event);
} catch (e) {
_log('Failed to write to side ${side.farSide!.name} - closing',
force: true);
_destroySide(side.farSide!);
}
});
}
side.stream.listen((Uint8List data) async {
side.stream.listen((Uint8List data) {
if (logTraffic) {
final message = String.fromCharCodes(data);
if (side.isSideA) {
Expand All @@ -154,7 +162,13 @@ class SocketConnector {
'B -> A : ${message.replaceAll(RegExp('[\x00-\x1F\x7F-\xFF]'), '*')}'));
}
}
side.farSide!.sink.add(data);
try {
side.farSide!.sink.add(data);
} catch (e) {
_log('Failed to write to side ${side.farSide!.name} - closing',
force: true);
_destroySide(side.farSide!);
}
}, onDone: () {
_log('stream.onDone on side ${side.name}');
_destroySide(side);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: socket_connector
description: Package for joining sockets together to create socket relays.

version: 2.3.0
version: 2.3.1
repository: https://github.com/cconstab/socket_connector

environment:
Expand Down

0 comments on commit 4128bcf

Please sign in to comment.