serverToServer static method

Future<SocketConnector> serverToServer({
  1. InternetAddress? addressA,
  2. int portA = 0,
  3. InternetAddress? addressB,
  4. int portB = 0,
  5. bool verbose = false,
  6. bool logTraffic = false,
  7. SocketAuthVerifier? socketAuthVerifierA,
  8. SocketAuthVerifier? socketAuthVerifierB,
  9. Duration timeout = SocketConnector.defaultTimeout,
  10. Duration authTimeout = SocketConnector.defaultTimeout,
  11. IOSink? logger,
  12. int backlog = 0,
})

Binds two Server sockets on specified Internet Addresses. Ports on which to listen can be given but if not given a spare port will be found by the OS. Finally relays data between sockets and optionally displays contents using the verbose flag

Implementation

static Future<SocketConnector> serverToServer({
  /// Defaults to [InternetAddress.anyIPv4]
  InternetAddress? addressA,
  int portA = 0,

  /// Defaults to [InternetAddress.anyIPv4]
  InternetAddress? addressB,
  int portB = 0,
  bool verbose = false,
  bool logTraffic = false,
  SocketAuthVerifier? socketAuthVerifierA,
  SocketAuthVerifier? socketAuthVerifierB,
  Duration timeout = SocketConnector.defaultTimeout,
  Duration authTimeout = SocketConnector.defaultTimeout,
  IOSink? logger,
  int backlog = 0,
}) async {
  IOSink logSink = logger ?? stderr;
  addressA ??= InternetAddress.anyIPv4;
  addressB ??= InternetAddress.anyIPv4;

  SocketConnector connector = SocketConnector(
    verbose: verbose,
    logTraffic: logTraffic,
    timeout: timeout,
    authTimeout: authTimeout,
    logger: logSink,
  );
  connector._serverSocketA = await ServerSocket.bind(
    addressA,
    portA,
    backlog: backlog,
  );
  connector._serverSocketB = await ServerSocket.bind(
    addressB,
    portB,
    backlog: backlog,
  );
  if (verbose) {
    logSink.writeln(
        '${DateTime.now()} | serverToServer | Bound ports A: ${connector.sideAPort}, B: ${connector.sideBPort}');
  }

  // listen for connections to the side 'A' server
  connector._serverSocketA!.listen((
    socket,
  ) {
    if (verbose) {
      logSink.writeln(
          '${DateTime.now()} | serverToServer | Connection on serverSocketA: ${connector._serverSocketA!.port}');
    }
    Side sideA = Side(socket, true, socketAuthVerifier: socketAuthVerifierA);
    unawaited(connector.handleSingleConnection(sideA).catchError((err) {
      logSink
          .writeln('ERROR $err from handleSingleConnection on sideA $sideA');
    }));
  }, onError: (error) {
    logSink.writeln(
        '${DateTime.now()} | serverToServer | ERROR on serverSocketA: ${connector._serverSocketA?.port} : $error');
    connector.close();
  }, onDone: () {
    logSink.writeln(
        '${DateTime.now()} | serverToServer | onDone called on serverSocketA: ${connector._serverSocketA?.port}');
    connector.close();
  });

  // listen for connections to the side 'B' server
  connector._serverSocketB!.listen((socket) {
    if (verbose) {
      logSink.writeln(
          '${DateTime.now()} | serverToServer | Connection on serverSocketB: ${connector._serverSocketB!.port}');
    }
    Side sideB = Side(socket, false, socketAuthVerifier: socketAuthVerifierB);
    unawaited(connector.handleSingleConnection(sideB).catchError((err) {
      logSink
          .writeln('ERROR $err from handleSingleConnection on sideB $sideB');
    }));
  }, onError: (error) {
    logSink.writeln(
        '${DateTime.now()} | serverToServer | ERROR on serverSocketB: ${connector._serverSocketB?.port} : $error');
    connector.close();
  }, onDone: () {
    logSink.writeln(
        '${DateTime.now()} | serverToServer | onDone called on serverSocketB: ${connector._serverSocketB?.port}');
    connector.close();
  });

  return (connector);
}