start method

  1. @override
Future<void> start({
  1. TransferFormat? transferFormat,
})
override

Implementation

@override
Future<void> start({TransferFormat? transferFormat}) async {
  transferFormat = transferFormat ?? TransferFormat.Binary;

  _logger
      ?.finer("Starting connection with transfer format '$transferFormat'.");

  if (_connectionState != ConnectionState.Disconnected) {
    return Future.error(GeneralError(
        "Cannot start a connection that is not in the 'Disconnected' state."));
  }

  _connectionState = ConnectionState.Connecting;

  _startInternalPromise = _startInternal(transferFormat);
  await _startInternalPromise;

  // The TypeScript compiler thinks that connectionState must be Connecting here. The TypeScript compiler is wrong.
  if (_connectionState == ConnectionState.Disconnecting) {
    // stop() was called and transitioned the client into the Disconnecting state.
    const message =
        "Failed to start the HttpConnection before stop() was called.";
    _logger?.severe(message);

    // We cannot await stopPromise inside startInternal since stopInternal awaits the startInternalPromise.
    await _stopPromise;

    return Future.error(GeneralError(message));
  } else if (_connectionState != ConnectionState.Connected) {
    // stop() was called and transitioned the client into the Disconnecting state.
    const message =
        "HttpConnection.startInternal completed gracefully but didn't enter the connection into the connected state!";
    _logger?.severe(message);
    return Future.error(GeneralError(message));
  }

  _connectionStarted = true;
}