sendDatagramConfirmable method

Future<void> sendDatagramConfirmable({
  1. required int messageId,
  2. required Uint8List datagram,
  3. required Iterable<FullAddress> addresses,
  4. Duration? ackTimeout,
})
inherited

Sends a confirmable datagram and waits for an acknowledgement.

This method sends a datagram to the specified addresses and waits for an acknowledgement from the receiver. It uses a completer to handle the asynchronous operation and schedules retries if the acknowledgement is not received within the specified timeout.

messageId The ID of the message being sent. This ID is used to correlate the message with its acknowledgement. datagram The datagram data to be sent. addresses An iterable of FullAddress objects representing the destinations to send the datagram to. ackTimeout The duration to wait for an acknowledgement before timing out. If not specified, the default message TTL is used.

Returns a Future that completes when the acknowledgement is received or the timeout expires.

Implementation

Future<void> sendDatagramConfirmable({
  required int messageId,
  required Uint8List datagram,
  required Iterable<FullAddress> addresses,
  Duration? ackTimeout,
}) {
  final completer = Completer<void>();

  _ackCompleters[messageId] = completer;

  _sendAndRetry(
    datagram: datagram,
    messageId: messageId,
    addresses: addresses,
  );

  return completer.future
      // Set a timeout for the future, using the provided ackTimeout or
      // the default message TTL.
      .timeout(ackTimeout ?? messageTTL)
      // Remove the completer from the ackCompleters map when the future
      // completes.
      .whenComplete(() => _ackCompleters.remove(messageId));
}