upgradeToTls method

Future<void> upgradeToTls()

Upgrade the control connection to TLS using SecureSocket.secureServer(). Re-attaches the command listener on the new secure socket.

Implementation

Future<void> upgradeToTls() async {
  // Flush the pending 234 response before starting the TLS handshake,
  // otherwise SecureSocket.secureServer() may consume the socket before
  // the plain-text response reaches the client.
  await _controlSocket.flush();
  // Pause the existing listener so the underlying socket stays open while
  // SecureSocket.secureServer() performs the TLS handshake.
  _controlSub?.pause();
  final secureSocket = await SecureSocket.secureServer(
    _controlSocket,
    securityContext!,
  );
  _controlSocket = secureSocket;
  _attachControlListener();
}