socketToSocket static method

Future<SocketConnector> socketToSocket({
  1. SocketConnector? connector,
  2. required InternetAddress addressA,
  3. required int portA,
  4. required InternetAddress addressB,
  5. required int portB,
  6. DataTransformer? transformAtoB,
  7. DataTransformer? transformBtoA,
  8. bool verbose = false,
  9. bool logTraffic = false,
  10. Duration timeout = SocketConnector.defaultTimeout,
  11. IOSink? logger,
})
  • Creates socket to portA on addressA
  • Creates socket to portB on addressB
  • Relays data between the sockets

Implementation

static Future<SocketConnector> socketToSocket({
  SocketConnector? connector,
  required InternetAddress addressA,
  required int portA,
  required InternetAddress addressB,
  required int portB,
  DataTransformer? transformAtoB,
  DataTransformer? transformBtoA,
  bool verbose = false,
  bool logTraffic = false,
  Duration timeout = SocketConnector.defaultTimeout,
  IOSink? logger,
}) async {
  IOSink logSink = logger ?? stderr;
  connector ??= SocketConnector(
    verbose: verbose,
    logTraffic: logTraffic,
    timeout: timeout,
    logger: logSink,
  );

  if (verbose) {
    logSink.writeln('socket_connector: Connecting to $addressA:$portA');
  }
  Socket sideASocket = await Socket.connect(addressA, portA);
  Side sideA = Side(sideASocket, true, transformer: transformAtoB);
  unawaited(connector.handleSingleConnection(sideA).catchError((err) {
    logSink.writeln('ERROR $err from handleSingleConnection on sideA $sideA');
  }));

  if (verbose) {
    logSink.writeln('socket_connector: Connecting to $addressB:$portB');
  }
  Socket sideBSocket = await Socket.connect(addressB, portB);
  Side sideB = Side(sideBSocket, false, transformer: transformBtoA);
  unawaited(connector.handleSingleConnection(sideB).catchError((err) {
    logSink.writeln('ERROR $err from handleSingleConnection on sideB $sideB');
  }));

  if (verbose) {
    logSink.writeln('socket_connector: started');
  }
  return (connector);
}