14 #include <fc_config.h>
17 #include <QLocalSocket>
28 #include "fc_version.h"
46 #define AUTOCONNECT_INTERVAL 500
49 #define MAX_AUTOCONNECT_ATTEMPTS 100
73 if (pconn->
sock !=
nullptr) {
74 reason = pconn->
sock->errorString();
76 reason = QString::fromUtf8(
_(
"unknown reason"));
82 qCritical(
"Lost connection to server: %s.", qUtf8Printable(reason));
84 qUtf8Printable(reason));
117 (void)
fc_strlcpy(errbuf,
_(
"Connection in progress."), errbufsize);
129 if (url.scheme() == QStringLiteral(
"fc21")) {
130 QTcpSocket *sock =
new QTcpSocket;
132 QObject::connect(sock, &QAbstractSocket::errorOccurred,
134 QObject::connect(sock, &QTcpSocket::disconnected,
136 sock->connectToHost(url.host(), url.port());
138 if (!sock->waitForConnected(10000)) {
139 (void)
fc_strlcpy(errbuf,
_(
"Connection timed out."), errbufsize);
144 }
else if (url.scheme() == QStringLiteral(
"fc21+local")) {
145 QLocalSocket *sock =
new QLocalSocket;
147 QObject::connect(sock, &QLocalSocket::errorOccurred, &
error_on_socket);
149 sock, &QLocalSocket::disconnected,
150 [userName = url.userName()] { client.conn.used = false; });
151 sock->connectToServer(url.path());
153 if (!sock->waitForConnected(10000)) {
154 (void)
fc_strlcpy(errbuf,
_(
"Connection timed out."), errbufsize);
169 if (errbufsize > 0 && errbuf !=
nullptr) {
190 struct packet_server_join_req req;
205 req.major_version = MAJOR_VERSION;
206 req.minor_version = MINOR_VERSION;
207 req.patch_version = PATCH_VERSION;
210 sz_strlcpy(req.username, qUtf8Printable(username));
212 send_packet_server_join_req(&
client.
conn, &req);
256 auto socket = pc->
sock;
257 bool have_data_for_server =
260 if (!socket->isOpen()) {
266 if (have_data_for_server) {
272 socket->waitForReadyRead(-1);
277 while (socket->bytesAvailable() > 0) {
283 }
else if (result > 0) {
307 enum packet_type type;
310 if (
nullptr != packet) {
312 ::operator
delete(packet);
314 if (type == PACKET_PROCESSING_FINISHED) {
327 }
else if (-2 == nb) {
342 static int count = 0;
353 qFatal(
_(
"Failed to contact server \"%s\" after %d attempts"),
354 qUtf8Printable(url.toDisplayString()), count);
364 qCritical(
_(
"Error contacting server \"%s\":\n %s\n"),
365 qUtf8Printable(url.toDisplayString()), errbuf);
381 _(
"Auto-connecting to \"%s\" every %f second(s) for %d times"),
void attribute_flush()
Send current state to the server.
const char *const our_capability
void output_window_append(const struct ft_color color, const char *featured_text)
Add a line of text to the output ("chatline") window, like puts() would do it in the console.
void output_window_printf(const struct ft_color color, const char *format,...)
Add a line of text to the output ("chatline") window.
void client_packet_input(void *packet, int type)
Handle packet received from server.
void stop_turn_change_wait()
Server is responsive again.
void set_client_state(enum client_states newstate)
Change client state.
static bool autoconnecting
static void error_on_socket()
Called when there is an error on the socket.
static void client_conn_close_callback(struct connection *pconn)
Client connection close socket callback.
void make_connection(QIODevice *sock, const QString &username)
Called after a connection is completed (e.g., in try_to_connect).
static int read_from_connection(struct connection *pc, bool block)
A wrapper around read_socket_data() which also handles the case the socket becomes writeable and ther...
void input_from_server(QIODevice *sock)
This function is called when the client received a new input from the server.
#define AUTOCONNECT_INTERVAL
double try_to_autoconnect(const QUrl &url)
Make an attempt to autoconnect to the server.
#define MAX_AUTOCONNECT_ATTEMPTS
static void close_socket_nomessage(struct connection *pc)
Close socket and cleanup.
void start_autoconnecting_to_server(const QUrl &url)
Start trying to autoconnect to freeciv21-server.
static int try_to_connect(const QUrl &url, char *errbuf, int errbufsize)
Try to connect to a server:
int connect_to_server(const QUrl &url, char *errbuf, int errbufsize)
Connect to a freeciv21-server instance – or at least try to.
void disconnect_from_server()
Get rid of server connection.
void client_kill_server(bool force)
Kills the server if the client has started it.
void connections_set_close_callback(conn_close_fn_t func)
Register the close_callback.
void flush_connection_send_buffer_all(struct connection *pc)
Flush'em.
int read_socket_data(QIODevice *sock, struct socket_packet_buffer *buffer)
Read data from socket, and check if a packet is ready.
void connection_common_init(struct connection *pconn)
Initialize common part of connection structure.
void connection_close(struct connection *pconn, const QString &reason)
Call the conn_close_callback.
void connection_common_close(struct connection *pconn)
Connection closing part common to server and client.
void popdown_races_dialog(void)
Close the nation selection dialog.
const struct ft_color ftc_client
int cities_results_request()
void cma_got_result(int citynr)
void add_net_input(QIODevice *sock)
Wait for data on the given socket.
#define fc_assert_ret(condition)
#define log_debug(message,...)
client_options * gui_options
void options_save(option_save_log_callback log_cb)
Save all options.
#define get_packet_from_connection(pc, ptype)
void notify_about_incoming_packet(struct connection *pc, int packet_type, int size)
Notify interested parties about incoming packet.
void notify_about_outgoing_packet(struct connection *pc, int packet_type, int size, int request_id)
Notify interested parties about outgoing packet.
char default_server_host[512]
bool save_options_on_exit
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
void(* outgoing_packet_notify)(struct connection *pc, int packet_type, int size, int request_id)
struct connection::@55::@60 client
struct socket_packet_buffer * send_buffer
struct socket_packet_buffer * buffer
size_t fc_strlcpy(char *dest, const char *src, size_t n)
fc_strlcpy() provides utf-8 version of (non-standard) function strlcpy() It is intended as more user-...
#define sz_strlcpy(dest, src)