Skip to content

Commit

Permalink
Merge pull request #12 from atsign-foundation/gkc/enhance-server-to-s…
Browse files Browse the repository at this point in the history
…ocket

feat: Enhance serverToSocket to enable different transformers on each socket pair
  • Loading branch information
gkc authored Mar 21, 2024
2 parents dd10aa6 + 88fddf5 commit 3eb2429
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.2.0

- feat: Enhance serverToSocket adding optional parameter `beforeJoining` which
will be called before a socket pair is actually joined together. Thus,
different transformers can be used for each socket pair.

## 2.1.0
- Added `multi` parameter to `SocketConnector.serverToSocket` - whether to
create new connections on the "B" side every time there is a new "A" side
Expand Down
11 changes: 10 additions & 1 deletion lib/src/socket_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ class SocketConnector {
/// to the bound server port [portA]
/// - [onConnect] is called when [portA] has got a new connection and a
/// corresponding outbound socket has been created to [addressB]:[portB]
/// and the two have been joined together
/// - [beforeJoining] is called when [portA] has got a new connection and a
/// corresponding outbound socket has been created to [addressB]:[portB]
/// but **before** they are joined together. This allows the code which
/// called [serverToSocket] to take additional steps (such as setting new
/// transformers rather than the ones which were provided initially)
static Future<SocketConnector> serverToSocket(
{
/// Defaults to [InternetAddress.anyIPv4]
Expand All @@ -400,7 +406,9 @@ class SocketConnector {
Duration timeout = SocketConnector.defaultTimeout,
IOSink? logger,
bool multi = false,
Function(Socket sideA, Socket sideB)? onConnect}) async {
@Deprecated("use beforeJoining instead")
Function(Socket socketA, Socket socketB)? onConnect,
Function(Side sideA, Side sideB)? beforeJoining}) async {
IOSink logSink = logger ?? stderr;
addressA ??= InternetAddress.anyIPv4;

Expand Down Expand Up @@ -428,6 +436,7 @@ class SocketConnector {
// connect to the side 'B' address and port
Socket sideBSocket = await Socket.connect(addressB, portB);
Side sideB = Side(sideBSocket, false, transformer: transformBtoA);
beforeJoining?.call(sideA, sideB);
unawaited(connector.handleSingleConnection(sideB));

onConnect?.call(sideASocket, sideBSocket);
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.1.0
version: 2.2.0
repository: https://github.com/cconstab/socket_connector

environment:
Expand Down
12 changes: 7 additions & 5 deletions test/socket_connector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,11 @@ void main() {
verbose: true,
timeout: Duration(milliseconds: 100),
multi: true,
onConnect: (Socket sideA, Socket sideB) {
beforeJoining: (Side sideA, Side sideB) {
serverConnections++;
print('SocketConnector.serverToSocket onConnect called back');
sideA.transformer = aToB;
sideB.transformer = bToA;
});
expect(connector.connections.isEmpty, true);

Expand Down Expand Up @@ -357,14 +359,14 @@ void main() {
// Wait for the sockets to send and receive data
await Future.delayed(Duration(milliseconds: 10));

socketA.write('hello world from side A');
socketA.write('hello world');
expect(currentSocketB != null, true);
currentSocketB?.write('hello world from side B');
currentSocketB?.write('hello world');
// Wait for the sockets to send and receive data
await Future.delayed(Duration(milliseconds: 10));

expect(rcvdA.last, "${aSockets.length}: hello world from side B");
expect(rcvdB.last, "${bSockets.length}: hello world from side A");
expect(rcvdA.last, "${aSockets.length}: from B: hello world");
expect(rcvdB.last, "${bSockets.length}: from A: hello world");
expect(rcvdA.length, i + 1);
expect(rcvdB.length, i + 1);
}
Expand Down

0 comments on commit 3eb2429

Please sign in to comment.