25#ifdef FREECIV_HAVE_SYS_TYPES_H
28#ifdef HAVE_SYS_SOCKET_H
29#include <sys/socket.h>
31#ifdef HAVE_NETINET_IN_H
32#include <netinet/in.h>
34#ifdef HAVE_ARPA_INET_H
43#ifdef HAVE_SYS_SELECT_H
44#include <sys/select.h>
52#ifdef HAVE_SYS_UTSNAME_H
53#include <sys/utsname.h>
96#define AUTOCONNECT_INTERVAL 500
99#define MAX_AUTOCONNECT_ATTEMPTS 100
102static struct fc_sockaddr_list *
list = NULL;
130 fc_strlcpy(reason,
_(
"unknown reason"),
sizeof(reason));
136 log_error(
"Lost connection to server: %s.", reason);
150 char *errbuf,
int errbufsize)
153#ifdef FREECIV_JSON_CONNECTION
154 port = FREECIV_JSON_PORT;
156 port = DEFAULT_SOCK_PORT;
162 hostname =
"localhost";
166 fc_sockaddr_list_destroy(
list);
175 (void)
fc_strlcpy(errbuf,
_(
"Failed looking up host."), errbufsize);
202 (void)
fc_strlcpy(errbuf,
_(
"Connection in progress."), errbufsize);
209 if ((sock = socket(paddr->saddr.sa_family, SOCK_STREAM, 0)) == -1) {
232#ifdef FREECIV_HAVE_WINSOCK
249 char *errbuf,
int errbufsize)
251 if (errbufsize > 0 && errbuf != NULL) {
343 fd_set readfs, writefs, exceptfs;
344 int socket_fd = pc->
sock;
354 FD_SET(socket_fd, &readfs);
357 FD_SET(socket_fd, &exceptfs);
359 if (have_data_for_server) {
361 FD_SET(socket_fd, &writefs);
362 n =
fc_select(socket_fd + 1, &readfs, &writefs, &exceptfs,
365 n =
fc_select(socket_fd + 1, &readfs, NULL, &exceptfs,
376 if (errno == EINTR) {
383 log_error(
"select() return=%d errno=%d (%s)",
388 if (FD_ISSET(socket_fd, &exceptfs)) {
392 if (have_data_for_server && FD_ISSET(socket_fd, &writefs)) {
396 if (FD_ISSET(socket_fd, &readfs)) {
419 if (NULL != packet) {
429 }
else if (-2 == nb) {
443 int expected_request_id)
448 log_debug(
"input_from_server_till_request_got_processed("
449 "expected_request_id=%d)", expected_request_id);
459 if (NULL == packet) {
471 expected_request_id) {
477 }
else if (-2 == nb) {
495 static int count = 0;
496#ifndef FREECIV_MSWINDOWS
497 static int warning_shown = 0;
507 log_fatal(
_(
"Failed to contact server \"%s\" at port "
508 "%d as \"%s\" after %d attempts"),
518#ifndef FREECIV_MSWINDOWS
521 if (!warning_shown) {
522 log_error(
"Connection to server refused. Please start the server.");
524 "Please start the server."));
531 log_fatal(
_(
"Error contacting server \"%s\" at port %d "
532 "as \"%s\":\n %s\n"),
552 _(
"Auto-connecting to server \"%s\" at port %d "
553 "as \"%s\" every %f second(s) for %d times"),
559 log_fatal(
_(
"Error contacting server \"%s\" at port %d "
560 "as \"%s\":\n %s\n"),
void agents_thaw_hint(void)
void agents_freeze_hint(void)
void attribute_flush(void)
const char *const our_capability
void output_window_append(const struct ft_color color, const char *featured_text)
void output_window_printf(const struct ft_color color, const char *format,...)
void client_packet_input(void *packet, int type)
void stop_turn_change_wait(void)
void set_client_state(enum client_states newstate)
void start_autoconnecting_to_server(void)
char forced_tileset_name[512]
static struct fc_sockaddr_list * list
int connect_to_server(const char *username, const char *hostname, int port, char *errbuf, int errbufsize)
static bool autoconnecting
static void client_conn_close_callback(struct connection *pconn)
void make_connection(int sock, const char *username)
static int read_from_connection(struct connection *pc, bool block)
#define AUTOCONNECT_INTERVAL
static int try_to_connect(const char *username, char *errbuf, int errbufsize)
double try_to_autoconnect(void)
#define MAX_AUTOCONNECT_ATTEMPTS
void input_from_server_till_request_got_processed(int fd, int expected_request_id)
void input_from_server(int fd)
void disconnect_from_server(void)
static void close_socket_nomessage(struct connection *pc)
static int get_server_address(const char *hostname, int port, char *errbuf, int errbufsize)
void client_kill_server(bool force)
void connections_set_close_callback(conn_close_fn_t func)
void flush_connection_send_buffer_all(struct connection *pc)
void connection_common_init(struct connection *pconn)
void connection_close(struct connection *pconn, const char *reason)
void connection_common_close(struct connection *pconn)
int read_socket_data(int sock, struct socket_packet_buffer *buffer)
const struct ft_color ftc_client
void close_connection_dialog(void)
void popdown_races_dialog(void)
void remove_net_input(void)
void add_net_input(int sock)
#define fc_assert_ret(condition)
#define log_fatal(message,...)
#define log_debug(message,...)
#define log_error(message,...)
struct fc_sockaddr_list * net_lookup_service(const char *name, int port, enum fc_addr_family family)
void fc_closesocket(int sock)
int sockaddr_size(union fc_sockaddr *addr)
int fc_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
int fc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, fc_timeval *timeout)
struct timeval fc_timeval
#define fc_sockaddr_list_iterate_end
#define fc_sockaddr_list_iterate(sockaddrlist, paddr)
void options_save(option_save_log_callback log_cb)
struct client_options gui_options
#define get_packet_from_connection(pc, ptype)
int send_packet_server_join_req(struct connection *pc, const struct packet_server_join_req *packet)
@ PACKET_PROCESSING_FINISHED
void notify_about_incoming_packet(struct connection *pc, int packet_type, int size)
void notify_about_outgoing_packet(struct connection *pc, int packet_type, int size, int request_id)
char default_server_host[512]
bool save_options_on_exit
struct connection::@57::@62 client
int request_id_of_currently_handled_packet
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 socket_packet_buffer * send_buffer
int last_processed_request_id_seen
struct socket_packet_buffer * buffer
size_t fc_strlcpy(char *dest, const char *src, size_t n)
const char * fc_strerror(fc_errno err)
fc_errno fc_get_errno(void)
#define sz_strlcpy(dest, src)
#define RETURN_VALUE_AFTER_EXIT(_val_)