request method

Future<IsoMsg?> request(
  1. String key,
  2. Future<void> send(),
  3. Duration timeout
)

Registers key as awaiting a response, runs send, then waits.

Implementation

Future<IsoMsg?> request(String key, Future<void> Function() send, Duration timeout) async {
  if (_pending.containsKey(key)) throw StateError("duplicate in-flight message key $key");
  final Completer<IsoMsg> completer = Completer<IsoMsg>();
  _pending[key] = completer;
  _sent++;
  try {
    await send();
    final IsoMsg response = await completer.future.timeout(timeout);
    _received++;
    return response;
  } on TimeoutException {
    _expired++;
    return null;
  } finally {
    _pending.remove(key);
  }
}