author | Medo <smaxein@googlemail.com> |
Sat, 02 Jun 2012 22:54:09 +0200 | |
changeset 7169 | b66eef8c8092 |
parent 7160 | c42949cfdd92 |
permissions | -rw-r--r-- |
7158
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
#include "nonblocksockets.h" |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
#include "logging.h" |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
#include <stdlib.h> |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
|
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
static uint32_t get_peer_ip(TCPsocket sock) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
IPaddress *addr = SDLNet_TCP_GetPeerAddress(sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
return SDLNet_Read32(&addr->host); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
|
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
static bool connection_is_local(TCPsocket sock) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1 |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
|
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
void flib_nbsocket_close(NonBlockSocket *nbsockptr) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
NonBlockSocket nbsock = *nbsockptr; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
if(nbsock!=NULL) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
SDLNet_DelSocket(nbsock->sockset, (SDLNet_GenericSocket)nbsock->sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
SDLNet_TCP_Close(nbsock->sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
SDLNet_FreeSocketSet(nbsock->sockset); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
free(nbsock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
*nbsockptr = NULL; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
|
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
NonBlockSocket flib_nbsocket_accept(TCPsocket listensocket, bool localOnly) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
NonBlockSocket result = NULL; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
if(!listensocket) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
flib_log_e("Attempt to accept a connection on a NULL socket."); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
return NULL; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
while(result==NULL) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
TCPsocket sock = SDLNet_TCP_Accept(listensocket); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
if(!sock) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
// No incoming connections |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
return NULL; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
if(localOnly && !connection_is_local(sock)) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(get_peer_ip(sock))); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
SDLNet_TCP_Close(sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
} else { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
result = malloc(sizeof(_NonBlockSocket)); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
if(result==NULL) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
flib_log_e("Out of memory!"); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
SDLNet_TCP_Close(sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
return NULL; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
result->sock = sock; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
result->sockset = SDLNet_AllocSocketSet(1); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
49 |
if(result->sockset==NULL) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
flib_log_e("Out of memory!"); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
51 |
SDLNet_TCP_Close(sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
52 |
free(result); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
53 |
return NULL; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
54 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
57 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
58 |
return result; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
59 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
60 |
|
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
61 |
int flib_nbsocket_recv(NonBlockSocket sock, void *data, int maxlen) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
62 |
if(!sock) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
63 |
flib_log_e("Attempt to receive on a NULL socket."); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
64 |
return -1; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
65 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
66 |
int readySockets = SDLNet_CheckSockets(sock->sockset, 0); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
67 |
if(readySockets>0) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
int size = SDLNet_TCP_Recv(sock->sock, data, maxlen); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
69 |
return size>0 ? size : -1; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
70 |
} else if(readySockets==0) { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
71 |
return 0; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
72 |
} else { |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
73 |
flib_log_e("Error in select system call: %s", SDLNet_GetError()); |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
74 |
return -1; |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
75 |
} |
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
76 |
} |