15 #include <QLocalSocket>
53 "Closing a socket (%s) before calling "
54 "connections_set_close_callback().",
78 (*conn_close_callback)(pconn);
120 didget = sock->read(
reinterpret_cast<char *
>(buffer->
data + buffer->
ndata),
124 buffer->
ndata += didget;
127 }
else if (didget == 0) {
141 int start, nput, nblock;
147 for (start = 0; buf->
ndata - start > limit;) {
148 if (!pc->
sock->isOpen()) {
154 log_debug(
"trying to write %d limit=%d", nblock, limit);
155 if ((nput = pc->
sock->write(
156 reinterpret_cast<const char *
>(buf->
data) + start, nblock))
186 if (pc && pc->
sock) {
187 if (
auto socket = qobject_cast<QLocalSocket *>(pc->
sock)) {
189 }
else if (
auto socket = qobject_cast<QTcpSocket *>(pc->
sock)) {
207 if (pc && pc->
sock) {
208 if (
auto socket = qobject_cast<QLocalSocket *>(pc->
sock)) {
210 }
else if (
auto socket = qobject_cast<QTcpSocket *>(pc->
sock)) {
220 const unsigned char *data,
int len)
224 if (
nullptr == pconn || !pconn->
used
246 const unsigned char *
data,
int len)
248 if (
nullptr == pconn || !pconn->
used
258 qDebug(
"cut connection %s due to huge send buffer (1)",
266 qDebug(
"cut connection %s due to huge send buffer (2)",
280 if (pc && pc->
used) {
298 qCritical(
"Too many calls to unbuffer %s!", pc->
username);
381 "Trying to look at the id of a non existing connection");
383 if (pconn->id ==
id) {
399 buf->do_buffer_sends = 0;
401 buf->data =
static_cast<unsigned char *
>(
fc_malloc(buf->nsize));
439 qUtf8Printable(pconn->
addr));
456 if (
nullptr != pconn->
playing) {
492 int result = old_request_id + 1;
494 if ((result & 0xffff) == 0) {
495 log_packet(
"INFORMATION: request_id has wrapped around; "
496 "setting from %d to 2",
531 for (i = 0; i < PACKET_LAST; i++) {
532 if (pc->
phs.
sent[i] !=
nullptr) {
541 for (i = 0; i < PACKET_LAST; i++) {
577 qCritical(
"WARNING: Trying to close already closed connection");
579 pconn->
sock->deleteLater();
580 pconn->
sock =
nullptr;
619 for (i = 0; i < PACKET_LAST; i++) {
717 enum conn_pattern_type
type;
753 switch (ppattern->
type) {
762 test = pconn->
server.ipaddr;
767 if (!test.isEmpty()) {
770 qCritical(
"%s(): Invalid pattern type (%d)", __FUNCTION__,
800 conn_pattern_type_name(ppattern->
type),
810 enum conn_pattern_type prefer,
812 size_t error_buf_len)
814 enum conn_pattern_type
type = conn_pattern_type_invalid();
818 if ((p = strchr(pattern,
'='))) {
820 QString pattern_type;
822 pattern_type = QString(pattern).trimmed();
823 type = conn_pattern_type_by_name(qUtf8Printable(pattern_type),
825 if (!conn_pattern_type_is_valid(
type)) {
826 if (
nullptr != error_buf) {
828 _(
"\"%s\" is not a valid pattern type"),
829 qUtf8Printable(pattern_type));
837 if (!conn_pattern_type_is_valid(
type)) {
838 if (
nullptr != error_buf) {
839 fc_strlcpy(error_buf,
_(
"Missing pattern type"), error_buf_len);
846 while (QChar::isSpace(*p)) {
851 if (
nullptr != error_buf) {
852 fc_strlcpy(error_buf,
_(
"Missing pattern"), error_buf_len);
866 return (pconn && pconn->
used && !pconn->
server.is_closing);
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Put a string reprentation of the pattern in 'buf'.
static conn_close_fn_t conn_close_callback
This callback is used when an error occurs trying to write to the connection.
void conn_list_do_unbuffer(struct conn_list *dest)
Convenience functions to unbuffer a list of connections.
static void default_conn_close_callback(struct connection *pconn)
Default 'conn_close_fn_t' to close a connection.
bool can_conn_edit(const struct connection *pconn)
Return TRUE iff the connection is currently allowed to edit.
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Thaw a connection list.
void connections_set_close_callback(conn_close_fn_t func)
Register the close_callback.
static void flush_connection_send_buffer_packets(struct connection *pc)
Flush'em.
void flush_connection_send_buffer_all(struct connection *pc)
Flush'em.
int get_next_request_id(int old_request_id)
Get next request id.
struct player * conn_get_player(const struct connection *pconn)
Returns the player that this connection is attached to, or nullptr.
int read_socket_data(QIODevice *sock, struct socket_packet_buffer *buffer)
Read data from socket, and check if a packet is ready.
const char * conn_description(const struct connection *pconn, bool is_private)
° Return pointer to static string containing a description for this ° connection, based on pconn->nam...
void conn_list_do_buffer(struct conn_list *dest)
Convenience functions to buffer a list of connections.
static const char * connection_accessor(int i)
Like conn_by_username(), but allow unambigous prefix (i.e.
bool can_conn_enable_editing(const struct connection *pconn)
Return TRUE iff the connection is allowed to start editing.
void connection_do_buffer(struct connection *pc)
Turn on buffering, using a counter so that calls may be nested.
static bool add_connection_data(struct connection *pconn, const unsigned char *data, int len)
Add data to send to the connection.
bool conn_pattern_match(const struct conn_pattern *ppattern, const struct connection *pconn)
Returns TRUE whether the connection fits the connection pattern.
static bool buffer_ensure_free_extra_space(struct socket_packet_buffer *buf, int extra_space)
Make sure that there is at least extra_space bytes free space in buffer, allocating more memory if ne...
void conn_set_capability(struct connection *pconn, const char *capability)
Set the network capability string for 'pconn'.
void conn_pattern_destroy(struct conn_pattern *ppattern)
Free a connection pattern.
void conn_reset_delta_state(struct connection *pc)
Remove all is-game-info cached packets from the connection.
void connection_common_init(struct connection *pconn)
Initialize common part of connection structure.
static void init_packet_hashs(struct connection *pc)
Allocate and initialize packet hashs for given connection.
void conn_list_compression_freeze(const struct conn_list *pconn_list)
Freeze a connection list.
bool conn_controls_player(const struct connection *pconn)
Returns TRUE if the given connection is attached to a player which it also controls (i....
bool conn_is_global_observer(const struct connection *pconn)
Returns TRUE if the given connection is a global observer.
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 conn_compression_freeze(struct connection *pconn)
Freeze the connection.
struct connection * conn_by_number(int id)
Find connection by id, from game.all_connections.
static void free_packet_hashes(struct connection *pc)
Free packet hash resources from given connection.
static void free_socket_packet_buffer(struct socket_packet_buffer *buf)
Free malloced struct.
void free_compression_queue(struct connection *pc)
Free compression queue for given connection.
const char blank_addr_str[]
bool connection_send_data(struct connection *pconn, const unsigned char *data, int len)
Write data to socket.
struct connection * conn_by_user(const char *user_name)
Find connection by exact user name, from game.all_connections, case-insensitve.
static int write_socket_data(struct connection *pc, struct socket_packet_buffer *buf, int limit)
Write wrapper function -vasc.
enum cmdlevel conn_get_access(const struct connection *pconn)
Returns the current access level of the given connection.
bool conn_is_valid(const struct connection *pconn)
Returns TRUE if the connection is valid, i.e.
bool conn_compression_frozen(const struct connection *pconn)
Returns TRUE if the connection is frozen.
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
struct conn_pattern * conn_pattern_from_string(const char *pattern, enum conn_pattern_type prefer, char *error_buf, size_t error_buf_len)
Creates a new connection pattern from the string.
struct socket_packet_buffer * new_socket_packet_buffer()
Return malloced struct, appropriately initialized.
struct conn_pattern * conn_pattern_new(enum conn_pattern_type type, const char *wildcard)
Creates a new connection pattern.
bool conn_pattern_list_match(const struct conn_pattern_list *plist, const struct connection *pconn)
Returns TRUE whether the connection fits one of the connection patterns.
void connection_do_unbuffer(struct connection *pc)
Turn off buffering if internal counter of number of times buffering was turned on falls to zero,...
#define conn_pattern_list_iterate_end
void(* conn_close_fn_t)(struct connection *pconn)
bool conn_compression_thaw(struct connection *pconn)
Thaw the connection.
#define conn_list_iterate(connlist, pconn)
#define conn_pattern_list_iterate(plist, ppatern)
#define conn_list_iterate_end
bool is_server()
Is program type server?
void genhash_destroy(struct genhash *pgenhash)
Destructor: free internal memory.
void genhash_clear(struct genhash *pgenhash)
Remove all entries of the genhash table.
#define fc_assert_msg(condition, message,...)
#define fc_assert_ret(condition)
#define fc_assert(condition)
#define log_debug(message,...)
const struct packet_handlers * packet_handlers_get(const char *capability)
Returns the packet handlers variant for 'capability'.
void packet_header_init(struct packet_header *packet_header)
Set the packet header field lengths used for the login protocol, before the capability of the connect...
const struct packet_handlers * packet_handlers_initial()
Returns the packet handlers variant with no special capability.
bool packet_has_game_info_flag(enum packet_type type)
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
bool wildcard_fit_string(const char *pattern, const char *test)
Returns TRUE if test fit the pattern.
enum m_pre_result match_prefix(m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result)
See match_prefix_full().
struct packet_game_info info
struct conn_list * all_connections
enum conn_pattern_type type
class civtimer * last_write
struct genhash ** received
enum cmdlevel access_level
const struct packet_handlers * handlers
struct connection::@55::@61 server
struct connection::@58 compression
struct connection::@59 statistics
struct connection::@57 phs
struct packet_header packet_header
char username[MAX_LEN_NAME]
struct socket_packet_buffer * send_buffer
char capability[MAX_LEN_CAPSTR]
void(* notify_of_writable_data)(struct connection *pc, bool data_available_and_socket_full)
struct socket_packet_buffer * buffer
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
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-...
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
int cat_snprintf(char *str, size_t n, const char *format,...)
cat_snprintf is like a combination of fc_snprintf and fc_strlcat; it does snprintf to the end of an e...
int fc_strncasequotecmp(const char *str0, const char *str1, size_t n)
Compare strings like strncasecmp() but ignoring surrounding quotes in either string.
size_t effectivestrlenquote(const char *str)
Count length of string without possible surrounding quotes.
#define sz_strlcpy(dest, src)
#define fc_realloc(ptr, sz)
#define sz_strlcat(dest, src)
void timer_destroy(civtimer *t)
Deletes timer.
void timer_start(civtimer *t)
Start timing, adding to previous accumulated time if timer has not been cleared.
civtimer * timer_renew(civtimer *t, enum timer_timetype type, enum timer_use use)
Allocate a new timer, or reuse t, with specified "type" and "use".