tryHandshake<T extends Message> method

Future<bool> tryHandshake<T extends Message>({
  1. required T message,
  2. required Duration timeout,
  3. required T constructor(
    1. List<int> bytes
    ),
  4. SocketInfo? destination,
})

A utility method to exchange a "handshake" to the destination

This will immediately send the message, and will complete once the message is received again by this socket. If the message is not received after timeout amount of time, then it will complete false

This assumes that there is a destination that is expecting this message and will immediately echo it back once receiving it

Implementation

Future<bool> tryHandshake<T extends Message>({
  required T message,
  required Duration timeout,
  required T Function(List<int> bytes) constructor,
  SocketInfo? destination,
}) async {
  sendMessage(message, destination: destination);
  final completer = Completer<bool>();

  late final StreamSubscription<T> subscription;
  subscription = messages.onMessage(
    name: message.messageName,
    constructor: constructor,
    callback: (handshake) {
      completer.complete(true);
      subscription.cancel();
    },
  );

  try {
    return await completer.future.timeout(timeout);
  } on TimeoutException {
    await subscription.cancel();
    return false;
  }
}