project_files/frontlib/net/netbase.h
author Wuzzy <Wuzzy@disroot.org>
Fri, 12 May 2023 23:15:01 +0200
changeset 15952 28b692ef6f15
parent 10017 de822cd3df3a
permissions -rw-r--r--
Fix CMake warning for position of cmake version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    20
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    21
 * Low-level protocol support for the network connection
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    22
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    23
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    24
#ifndef NETBASE_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    25
#define NETBASE_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    27
#include <stddef.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
#include <stdint.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
struct _flib_netbase;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
typedef struct _flib_netbase flib_netbase;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    34
typedef struct {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    35
    int partCount;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    36
    char **parts;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
} flib_netmsg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
 * Start a connection to the specified Hedgewars server.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
 * Returns NULL on error. Destroy the created object with flib_netconn_destroy.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
flib_netbase *flib_netbase_create(const char *server, uint16_t port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
 * Free resources and close sockets.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
void flib_netbase_destroy(flib_netbase *net);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
 * Determine the current connection state. Starts out true, and turns to
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
 * false when we are disconnected from the server.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
bool flib_netbase_connected(flib_netbase *net);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
 * Receive a new message and return it as a flib_netmsg. The netmsg has to be
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
 * destroyed with flib_netmsg_destroy after use.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
 * Returns NULL if no message is available.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
 * Note: When a connection is closed, you probably want to call this function until
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
 * no further message is returned, to ensure you see all messages that were sent
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
 * before the connection closed.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
flib_netmsg *flib_netbase_recv_message(flib_netbase *net);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
int flib_netbase_send_raw(flib_netbase *net, const void *data, size_t len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
 * Write a single message to the server. This call blocks until the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
 * message is completely written or the connection is closed or an error occurs.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
 * Returns a negative value on failure.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
int flib_netbase_send_message(flib_netbase *net, const flib_netmsg *msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    77
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    78
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    79
 * Send a message printf-style.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
 * flib_netbase_sendf(net, "%s\n\n", "TOGGLE_READY");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
 * flib_netbase_sendf(net, "%s\n%s\n%i\n\n", "CFG", "MAPGEN", MAPGEN_MAZE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
int flib_netbase_sendf(flib_netbase *net, const char *format, ...);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
flib_netmsg *flib_netmsg_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
void flib_netmsg_destroy(flib_netmsg *msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
int flib_netmsg_append_part(flib_netmsg *msg, const void *param, size_t len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
#endif /* NETBASE_H_ */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91