diff --git a/client/client_main.cpp b/client/client_main.cpp index 45601e43a0..47270adfda 100644 --- a/client/client_main.cpp +++ b/client/client_main.cpp @@ -475,7 +475,8 @@ int client_main(int argc, char *argv[]) auto positional = parser.positionalArguments(); if (positional.size() == 1) { url = QUrl(positional.constFirst()); - if (!url.isValid() || url.scheme() != QStringLiteral("fc21")) { + // Supported schemes: fc21://, fc21+local:// + if (!url.isValid() || (!url.scheme().startsWith("fc21"))) { // Try with the default protocol url = QUrl(QStringLiteral("fc21://") + positional.constFirst()); // Still no luck diff --git a/client/clinet.cpp b/client/clinet.cpp index 7995969713..52ddc8b56d 100644 --- a/client/clinet.cpp +++ b/client/clinet.cpp @@ -99,6 +99,21 @@ static void client_conn_close_callback(struct connection *pconn) } namespace { +/// The URL we are presently connecting to. +QUrl connect_to; + +/** + * Called when there is an error on the socket. + */ +static void error_on_socket() +{ + if (client.conn.sock != nullptr) { + log_debug("%s", qUtf8Printable(client.conn.sock->errorString())); + real_output_window_append(client.conn.sock->errorString(), NULL, -1); + } + client.conn.used = false; +} + /** Try to connect to a server: - try to create a TCP socket to the given URL (default to @@ -111,9 +126,6 @@ namespace { message in ERRBUF and return the Unix error code (ie., errno, which will be non-zero). */ - -QUrl connect_to; - static int try_to_connect(const QUrl &url, char *errbuf, int errbufsize) { // Apply defaults @@ -154,25 +166,30 @@ static int try_to_connect(const QUrl &url, char *errbuf, int errbufsize) client.conn.used = true; // Now there will be a connection :) // Connect - if (!client.conn.sock) { - auto sock = new QTcpSocket; + if (url.scheme() == QStringLiteral("fc21")) { + QTcpSocket *sock = new QTcpSocket; client.conn.sock = sock; QObject::connect( sock, QOverload::of(&QAbstractSocket::error), - [] { - if (client.conn.sock != nullptr) { - log_debug("%s", qUtf8Printable(client.conn.sock->errorString())); - real_output_window_append(client.conn.sock->errorString(), NULL, - -1); - } - client.conn.used = false; - }); + &error_on_socket); QObject::connect(sock, &QTcpSocket::connected, - [userName = url.userName()] { - make_connection(client.conn.sock, userName); - }); + [userName = url.userName()] { + make_connection(client.conn.sock, userName); + }); sock->connectToHost(url.host(), url.port()); + } else if (url.scheme() == QStringLiteral("fc21+local")) { + QLocalSocket *sock = new QLocalSocket; + client.conn.sock = sock; + QObject::connect( + sock, + QOverload::of(&QLocalSocket::error), + &error_on_socket); + QObject::connect(sock, &QLocalSocket::connected, + [userName = url.userName()] { + make_connection(client.conn.sock, userName); + }); + sock->connectToServer(url.path()); } return 0; @@ -371,20 +388,28 @@ void try_to_autoconnect(const QUrl &url) client.conn.sock = new QTcpSocket; } - QObject::connect( - client.conn.sock, - QOverload::of(&QAbstractSocket::error), - [url] { - if (client.conn.sock != nullptr) { - output_window_printf(ftc_client, - _("Failed to autoconnect to %s: %d of %d " - "attempts, retrying..."), - qUtf8Printable(url.toDisplayString()), count, - MAX_AUTOCONNECT_ATTEMPTS); - QTimer::singleShot(AUTOCONNECT_INTERVAL, - [url]() { try_to_autoconnect(url); }); - } - }); - try_to_connect(url, errbuf, sizeof(errbuf)); + + auto error_handler = [url] { + if (client.conn.sock != nullptr) { + output_window_printf(ftc_client, + _("Failed to autoconnect to %s: %d of %d " + "attempts, retrying..."), + qUtf8Printable(url.toDisplayString()), count, + MAX_AUTOCONNECT_ATTEMPTS); + QTimer::singleShot(AUTOCONNECT_INTERVAL, + [url]() { try_to_autoconnect(url); }); + } + }; + if (auto tcp = qobject_cast(client.conn.sock)) { + QObject::connect( + tcp, + QOverload::of(&QAbstractSocket::error), + error_handler); + } else if (auto local = qobject_cast(client.conn.sock)) { + QObject::connect( + local, + QOverload::of(&QLocalSocket::error), + error_handler); + } } diff --git a/client/gui-qt/page_network.cpp b/client/gui-qt/page_network.cpp index 3ce80d3bbb..5cf181ea6b 100644 --- a/client/gui-qt/page_network.cpp +++ b/client/gui-qt/page_network.cpp @@ -437,12 +437,12 @@ void page_network::slot_connect() switch (connection_status) { case LOGIN_TYPE: + client_url().setScheme(QStringLiteral("fc21")); client_url().setUserName(ui.connect_login_edit->text()); client_url().setHost(ui.connect_host_edit->text()); client_url().setPort(ui.connect_port_edit->text().toInt()); - if (connect_to_server(client_url(), errbuf, sizeof(errbuf)) != -1) { - } else { + if (connect_to_server(client_url(), errbuf, sizeof(errbuf)) < 0) { king->set_status_bar(QString::fromUtf8(errbuf)); output_window_append(ftc_client, errbuf); }