author | unc0rr |
Sat, 16 Feb 2013 23:28:16 +0400 | |
changeset 8507 | f4475782cf45 |
parent 7314 | 6171f0bad318 |
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:
7271
diff
changeset
|
1 |
/* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7271
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:
7271
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:
7271
diff
changeset
|
4 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7271
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:
7271
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:
7271
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:
7271
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:
7271
diff
changeset
|
9 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7271
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:
7271
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:
7271
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:
7271
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:
7271
diff
changeset
|
14 |
* |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7271
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:
7271
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:
7271
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:
7271
diff
changeset
|
18 |
*/ |
6171f0bad318
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents:
7271
diff
changeset
|
19 |
|
7171 | 20 |
/* |
21 |
* Sockets for TCP networking. |
|
22 |
* |
|
23 |
* This layer offers some functionality over what SDL_net offers directly: listening |
|
24 |
* sockets (called acceptors here) can be bound to port 0, which will make them listen |
|
25 |
* on a random unused port, if one can be found. To support this feature, you can also |
|
26 |
* query the local port that an acceptor is listening on. |
|
27 |
* |
|
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
28 |
* Further, we support nonblocking reads here. |
7171 | 29 |
*/ |
30 |
||
31 |
#ifndef SOCKET_H_ |
|
32 |
#define SOCKET_H_ |
|
33 |
||
34 |
#include <stdbool.h> |
|
35 |
#include <stdint.h> |
|
36 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
37 |
typedef struct _flib_tcpsocket flib_tcpsocket; |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
38 |
typedef struct _flib_acceptor flib_acceptor; |
7171 | 39 |
|
40 |
/** |
|
41 |
* Create a new acceptor which will listen for incoming TCP connections |
|
42 |
* on the given port. If port is 0, this will listen on a random |
|
43 |
* unused port which can then be queried with flib_acceptor_listenport. |
|
44 |
* |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
45 |
* Returns NULL on error. |
7171 | 46 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
47 |
flib_acceptor *flib_acceptor_create(uint16_t port); |
7171 | 48 |
|
49 |
/** |
|
50 |
* Return the port on which the acceptor is listening. |
|
51 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
52 |
uint16_t flib_acceptor_listenport(flib_acceptor *acceptor); |
7171 | 53 |
|
54 |
/** |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
55 |
* Close the acceptor and free its memory. NULL-safe. |
7171 | 56 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
57 |
void flib_acceptor_close(flib_acceptor *acceptor); |
7171 | 58 |
|
59 |
/** |
|
60 |
* Try to accept a connection from an acceptor (listening socket). |
|
61 |
* if localOnly is true, this will only accept connections which came from 127.0.0.1 |
|
62 |
* Returns NULL if nothing can be accepted. |
|
63 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
64 |
flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly); |
7171 | 65 |
|
66 |
/** |
|
7234
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
67 |
* Try to connect to the server at the given address. |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
68 |
*/ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
69 |
flib_tcpsocket *flib_socket_connect(const char *host, uint16_t port); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
70 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
71 |
/** |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
72 |
* Close the socket and free its memory. NULL-safe. |
7171 | 73 |
*/ |
74 |
void flib_socket_close(flib_tcpsocket *socket); |
|
75 |
||
76 |
/** |
|
77 |
* Attempt to receive up to maxlen bytes from the socket, but does not |
|
78 |
* block if nothing is available. |
|
79 |
* Returns the ammount of data received, 0 if there was nothing to receive, |
|
80 |
* or a negative number if the connection was closed or an error occurred. |
|
81 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
82 |
int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen); |
7171 | 83 |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
84 |
/** |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
85 |
* Blocking send all the data in the data buffer. Returns the actual ammount |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
86 |
* of data sent, or a negative value on error. If the value returned here |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
87 |
* is less than len, either the connection closed or an error occurred. |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
88 |
*/ |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
89 |
int flib_socket_send(flib_tcpsocket *sock, const void *data, int len); |
7171 | 90 |
|
91 |
#endif /* SOCKET_H_ */ |