start method

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

Implementation

@override
Future<void> start({
  TransferFormat? transferFormat = TransferFormat.binary,
}) async {
  _logging!(LogLevel.debug,
      'Starting connection with transfer format \'${transferFormat.toString()}\'.');

  if (_connectionState != ConnectionState.disconnected) {
    return Future.error(
      Exception(
          'Cannot start an HttpConnection that is not in the \'Disconnected\' state.'),
    );
  }

  _connectionState = ConnectionState.connecting;

  _startInternalFuture = _startInternal(transferFormat: transferFormat);
  await _startInternalFuture;

  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.';
    _logging!(LogLevel.error, message);

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

    return Future.error(Exception(message));
  } else if (_connectionState as dynamic != 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!';
    _logging!(LogLevel.error, message);
    return Future.error(Exception(message));
  }

  _connectionStarted = true;
}