connect static method Null safety

Future<MySqlConnection> connect(
  1. ConnectionSettings c,
  2. {bool isUnixSocket = false}
)

Connects a MySQL server at the given host on port, authenticates using user and password and connects to db.

c.timeout is used as the connection timeout and the default timeout for all socket communication.

A SocketException is thrown on connection failure or a socket timeout connecting the socket. A TimeoutException is thrown if there is a timeout in the handshake with the server.

Implementation

static Future<MySqlConnection> connect(ConnectionSettings c, {bool isUnixSocket = false}) async {
  assert(!c.useSSL); // Not implemented
  assert(!c.useCompression);

  ReqRespConnection? conn;
  late Completer handshakeCompleter;

  _log.fine('opening connection to ${c.host}:${c.port}/${c.db}');

  var socket = await BufferedSocket.connect(c.host, c.port, c.timeout, isUnixSocket: isUnixSocket, onDataReady: () {
    conn?._readPacket();
  }, onDone: () {
    _log.fine('done');
  }, onError: (Object error) {
    _log.warning('socket error: $error');

    // If conn has not been connected there was a connection error.
    if (conn == null) {
      handshakeCompleter.completeError(error);
    } else {
      conn.handleError(error);
    }
  }, onClosed: () {
    if (conn != null) {
      conn.handleError(SocketException.closed());
    }
  });

  Handler handler =
      HandshakeHandler(c.user, c.password, c.maxPacketSize, c.characterSet, c.db, c.useCompression, c.useSSL);
  handshakeCompleter = Completer<void>();
  conn = ReqRespConnection(socket, handler, handshakeCompleter, c.maxPacketSize);

  await handshakeCompleter.future.timeout(c.timeout);
  return MySqlConnection(c.timeout, conn);
}