author | Medo <smaxein@googlemail.com> |
Tue, 19 Jun 2012 21:17:05 +0200 | |
changeset 7234 | 613998625a3c |
parent 7224 | 5143861c83bd |
child 7271 | 5608ac657362 |
permissions | -rw-r--r-- |
7171 | 1 |
/* |
2 |
* Sockets for TCP networking. |
|
3 |
* |
|
4 |
* This layer offers some functionality over what SDL_net offers directly: listening |
|
5 |
* sockets (called acceptors here) can be bound to port 0, which will make them listen |
|
6 |
* on a random unused port, if one can be found. To support this feature, you can also |
|
7 |
* query the local port that an acceptor is listening on. |
|
8 |
* |
|
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
9 |
* Further, we support nonblocking reads here. |
7171 | 10 |
*/ |
11 |
||
12 |
#ifndef SOCKET_H_ |
|
13 |
#define SOCKET_H_ |
|
14 |
||
15 |
#include <stdbool.h> |
|
16 |
#include <stdint.h> |
|
17 |
||
18 |
struct _flib_tcpsocket; |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
19 |
typedef struct _flib_tcpsocket flib_tcpsocket; |
7171 | 20 |
|
21 |
struct _flib_acceptor; |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
22 |
typedef struct _flib_acceptor flib_acceptor; |
7171 | 23 |
|
24 |
/** |
|
25 |
* Create a new acceptor which will listen for incoming TCP connections |
|
26 |
* on the given port. If port is 0, this will listen on a random |
|
27 |
* unused port which can then be queried with flib_acceptor_listenport. |
|
28 |
* |
|
29 |
* Can return NULL on error. |
|
30 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
31 |
flib_acceptor *flib_acceptor_create(uint16_t port); |
7171 | 32 |
|
33 |
/** |
|
34 |
* Return the port on which the acceptor is listening. |
|
35 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
36 |
uint16_t flib_acceptor_listenport(flib_acceptor *acceptor); |
7171 | 37 |
|
38 |
/** |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
39 |
* Close the acceptor and free its memory. |
7171 | 40 |
* If the acceptor is already NULL, nothing happens. |
41 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
42 |
void flib_acceptor_close(flib_acceptor *acceptor); |
7171 | 43 |
|
44 |
/** |
|
45 |
* Try to accept a connection from an acceptor (listening socket). |
|
46 |
* if localOnly is true, this will only accept connections which came from 127.0.0.1 |
|
47 |
* Returns NULL if nothing can be accepted. |
|
48 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
49 |
flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly); |
7171 | 50 |
|
51 |
/** |
|
7234
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
52 |
* 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
|
53 |
*/ |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
54 |
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
|
55 |
|
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
56 |
/** |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
57 |
* Close the socket and free its memory. |
7171 | 58 |
* If the socket is already NULL, nothing happens. |
59 |
*/ |
|
60 |
void flib_socket_close(flib_tcpsocket *socket); |
|
61 |
||
62 |
/** |
|
63 |
* Attempt to receive up to maxlen bytes from the socket, but does not |
|
64 |
* block if nothing is available. |
|
65 |
* Returns the ammount of data received, 0 if there was nothing to receive, |
|
66 |
* or a negative number if the connection was closed or an error occurred. |
|
67 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
68 |
int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen); |
7171 | 69 |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
70 |
int flib_socket_send(flib_tcpsocket *sock, const void *data, int len); |
7171 | 71 |
|
72 |
#endif /* SOCKET_H_ */ |