author | Periklis Ntanasis <pntanasis@gmail.com> |
Fri, 12 Jul 2013 12:00:30 +0300 | |
branch | spacecampaign |
changeset 9334 | 4bf3edc24444 |
parent 7497 | 7e1d72fc03c7 |
child 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7314
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
1 |
/* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
4 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
9 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
13 |
* GNU General Public License for more details. |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
14 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
18 |
*/ |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
19 |
|
7234
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 |
* Low-level protocol support for the network connection |
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 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
#ifndef NETBASE_H_ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
#define NETBASE_H_ |
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 |
#include <stddef.h> |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
#include <stdint.h> |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
#include <stdbool.h> |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
struct _flib_netbase; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
typedef struct _flib_netbase flib_netbase; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
typedef struct { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
int partCount; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
char **parts; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
} flib_netmsg; |
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 |
/** |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
* Start a connection to the specified Hedgewars server. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
* |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
* 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
|
43 |
*/ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
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
|
45 |
|
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 |
* Free resources and close sockets. |
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 |
void flib_netbase_destroy(flib_netbase *net); |
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 |
* 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
|
53 |
* false when we are disconnected from the server. |
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 |
bool flib_netbase_connected(flib_netbase *net); |
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 |
/** |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
58 |
* 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
|
59 |
* destroyed with flib_netmsg_destroy after use. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
60 |
* Returns NULL if no message is available. |
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 |
* 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
|
63 |
* 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
|
64 |
* before the connection closed. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
65 |
*/ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
66 |
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
|
67 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
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
|
69 |
|
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 |
* 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
|
72 |
* 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
|
73 |
* |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
74 |
* Returns a negative value on failure. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
75 |
*/ |
7497 | 76 |
int flib_netbase_send_message(flib_netbase *net, const flib_netmsg *msg); |
7234
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
77 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
78 |
/** |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
79 |
* Send a message printf-style. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
80 |
* |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
81 |
* 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
|
82 |
* 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
|
83 |
*/ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
84 |
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
|
85 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
86 |
flib_netmsg *flib_netmsg_create(); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
87 |
void flib_netmsg_destroy(flib_netmsg *msg); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
88 |
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
|
89 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
90 |
#endif /* NETBASE_H_ */ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
91 |