Skip to content

Commit

Permalink
Add local socket support to the client
Browse files Browse the repository at this point in the history
This support is a bit hidden, it is enabled by using the fc21+local URL scheme.
  • Loading branch information
lmoureaux committed Feb 12, 2022
1 parent f038fa7 commit fd0c6af
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
3 changes: 2 additions & 1 deletion client/client_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 56 additions & 31 deletions client/clinet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<QAbstractSocket::SocketError>::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<QLocalSocket::LocalSocketError>::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;
Expand Down Expand Up @@ -371,20 +388,28 @@ void try_to_autoconnect(const QUrl &url)
client.conn.sock = new QTcpSocket;
}

QObject::connect(
client.conn.sock,
QOverload<QAbstractSocket::SocketError>::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<QTcpSocket *>(client.conn.sock)) {
QObject::connect(
tcp,
QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
error_handler);
} else if (auto local = qobject_cast<QLocalSocket *>(client.conn.sock)) {
QObject::connect(
local,
QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error),
error_handler);
}
}
4 changes: 2 additions & 2 deletions client/gui-qt/page_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit fd0c6af

Please sign in to comment.