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