Skip to content

Commit

Permalink
feat: Enhance serverToSocket adding optional parameter `beforeJoining…
Browse files Browse the repository at this point in the history
…` which will be called before a socket pair is actually joined together. Thus different transformers can be used for each socket pair.
  • Loading branch information
gkc committed Mar 21, 2024
1 parent dd10aa6 commit 1b3d84e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
39 changes: 24 additions & 15 deletions lib/src/socket_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,21 +386,29 @@ 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]
static Future<SocketConnector> serverToSocket(
{
/// Defaults to [InternetAddress.anyIPv4]
InternetAddress? addressA,
int portA = 0,
required InternetAddress addressB,
required int portB,
DataTransformer? transformAtoB,
DataTransformer? transformBtoA,
bool verbose = false,
bool logTraffic = false,
Duration timeout = SocketConnector.defaultTimeout,
IOSink? logger,
bool multi = false,
Function(Socket sideA, Socket sideB)? onConnect}) async {
/// 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]
InternetAddress? addressA,
int portA = 0,
required InternetAddress addressB,
required int portB,
DataTransformer? transformAtoB,
DataTransformer? transformBtoA,
bool verbose = false,
bool logTraffic = false,
Duration timeout = SocketConnector.defaultTimeout,
IOSink? logger,
bool multi = false,
@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
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 1b3d84e

Please sign in to comment.