project_files/frontlib/net/netbase.h
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 18:02:45 +0200
changeset 7275 15f722e0b96f
parent 7234 613998625a3c
child 7314 6171f0bad318
permissions -rw-r--r--
frontlib: Getting there :) Added commandline client for testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
/*
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
 * Low-level protocol support for the network connection
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#ifndef NETBASE_H_
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#define NETBASE_H_
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <stddef.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <stdint.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
#include <stdbool.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
struct _flib_netbase;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
typedef struct _flib_netbase flib_netbase;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
typedef struct {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
	int partCount;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
	char **parts;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
} flib_netmsg;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
 * Start a connection to the specified Hedgewars server.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
 *
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
 * Returns NULL on error. Destroy the created object with flib_netconn_destroy.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
flib_netbase *flib_netbase_create(const char *server, uint16_t port);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
 * Free resources and close sockets.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
void flib_netbase_destroy(flib_netbase *net);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
 * Determine the current connection state. Starts out true, and turns to
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
 * false when we are disconnected from the server.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
bool flib_netbase_connected(flib_netbase *net);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
 * Receive a new message and return it as a flib_netmsg. The netmsg has to be
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
 * destroyed with flib_netmsg_destroy after use.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
 * Returns NULL if no message is available.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
 *
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
 * Note: When a connection is closed, you probably want to call this function until
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
 * no further message is returned, to ensure you see all messages that were sent
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
 * before the connection closed.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
flib_netmsg *flib_netbase_recv_message(flib_netbase *net);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
int flib_netbase_send_raw(flib_netbase *net, const void *data, size_t len);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
 * Write a single message to the server. This call blocks until the
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
 * message is completely written or the connection is closed or an error occurs.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
 *
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
 * Returns a negative value on failure.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
int flib_netbase_send_message(flib_netbase *net, flib_netmsg *msg);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
 * Send a message printf-style.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
 *
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
 * flib_netbase_sendf(net, "%s\n\n", "TOGGLE_READY");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
 * flib_netbase_sendf(net, "%s\n%s\n%i\n\n", "CFG", "MAPGEN", MAPGEN_MAZE);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
int flib_netbase_sendf(flib_netbase *net, const char *format, ...);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
flib_netmsg *flib_netmsg_create();
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
void flib_netmsg_destroy(flib_netmsg *msg);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
int flib_netmsg_append_part(flib_netmsg *msg, const void *param, size_t len);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
#endif /* NETBASE_H_ */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72