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