Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance serverToSocket to enable different transformers on each socket pair #12

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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