author | koda |
Wed, 12 Jun 2013 01:00:47 +0200 | |
changeset 9202 | 4d69569baabb |
parent 8091 | e0a76056a633 |
child 8094 | 6c5b4e69f03d |
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 |
#include "socket.h" |
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
21 |
#include "util/logging.h" |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
22 |
#include "util/util.h" |
7171 | 23 |
#include <stdlib.h> |
24 |
#include <SDL_net.h> |
|
25 |
#include <time.h> |
|
26 |
||
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
27 |
struct _flib_tcpsocket { |
7171 | 28 |
TCPsocket sock; |
29 |
SDLNet_SocketSet sockset; |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
30 |
}; |
7171 | 31 |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
32 |
struct _flib_acceptor { |
7171 | 33 |
TCPsocket sock; |
34 |
uint16_t port; |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
35 |
}; |
7171 | 36 |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
37 |
static uint32_t getPeerIp(TCPsocket sock) { |
7171 | 38 |
IPaddress *addr = SDLNet_TCP_GetPeerAddress(sock); |
39 |
return SDLNet_Read32(&addr->host); |
|
40 |
} |
|
41 |
||
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
42 |
static bool connectionIsLocal(TCPsocket sock) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
43 |
return getPeerIp(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1 |
7171 | 44 |
} |
45 |
||
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
46 |
static flib_tcpsocket *createSocket(TCPsocket sdlsock) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
47 |
flib_tcpsocket *result = flib_calloc(1, sizeof(flib_tcpsocket)); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
48 |
if(result) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
49 |
result->sock = sdlsock; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
50 |
result->sockset = SDLNet_AllocSocketSet(1); |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
51 |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
52 |
if(!result->sockset) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
53 |
flib_log_e("Can't allocate socket: Out of memory!"); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
54 |
SDLNet_FreeSocketSet(result->sockset); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
55 |
free(result); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
56 |
result = NULL; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
57 |
} else { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
58 |
SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
59 |
} |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
60 |
} |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
61 |
return result; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
62 |
} |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
63 |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
64 |
TCPsocket listen(uint16_t port) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
65 |
IPaddress addr; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
66 |
addr.host = INADDR_ANY; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
67 |
SDLNet_Write16(port, &addr.port); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
68 |
TCPsocket sock = SDLNet_TCP_Open(&addr); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
69 |
if(!sock) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
70 |
flib_log_w("Unable to listen on port %u: %s", (unsigned)port, SDLNet_GetError()); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
71 |
} |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
72 |
return sock; |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
73 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
74 |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
75 |
flib_acceptor *flib_acceptor_create(uint16_t port) { |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
76 |
flib_acceptor *result = flib_calloc(1, sizeof(flib_acceptor)); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
77 |
if(result) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
78 |
if(port > 0) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
79 |
result->port = port; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
80 |
result->sock = listen(result->port); |
7171 | 81 |
} else { |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
82 |
/* SDL_net does not seem to have a way to listen on a random unused port |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
83 |
and find out which port that is, so let's try to find one ourselves. */ |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
84 |
srand(time(NULL)); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
85 |
for(int i=0; !result->sock && i<1000; i++) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
86 |
// IANA suggests using ports in the range 49152-65535 for things like this |
8091 | 87 |
result->port = 49152+(rand()%(65536-49152)); |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
88 |
result->sock = listen(result->port); |
7171 | 89 |
} |
90 |
} |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
91 |
if(!result->sock) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
92 |
flib_log_e("Failed to create acceptor."); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
93 |
free(result); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
94 |
result = NULL; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
95 |
} |
7171 | 96 |
} |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
97 |
return result; |
7171 | 98 |
} |
99 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
100 |
uint16_t flib_acceptor_listenport(flib_acceptor *acceptor) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
101 |
if(!acceptor) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
102 |
flib_log_e("Call to flib_acceptor_listenport with acceptor==null"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
103 |
return 0; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
104 |
} |
7171 | 105 |
return acceptor->port; |
106 |
} |
|
107 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
108 |
void flib_acceptor_close(flib_acceptor *acceptor) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
109 |
if(acceptor) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
110 |
SDLNet_TCP_Close(acceptor->sock); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
111 |
free(acceptor); |
7171 | 112 |
} |
113 |
} |
|
114 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
115 |
flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly) { |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
116 |
flib_tcpsocket *result = NULL; |
7171 | 117 |
if(!acceptor) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
118 |
flib_log_e("Call to flib_socket_accept with acceptor==null"); |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
119 |
} else { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
120 |
TCPsocket sock = NULL; |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
121 |
while(!result && (sock = SDLNet_TCP_Accept(acceptor->sock))) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
122 |
if(localOnly && !connectionIsLocal(sock)) { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
123 |
flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(getPeerIp(sock))); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
124 |
} else { |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
125 |
result = createSocket(sock); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
126 |
} |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
127 |
if(!result) { |
7171 | 128 |
SDLNet_TCP_Close(sock); |
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
return result; |
|
133 |
} |
|
134 |
||
7234
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
135 |
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
|
136 |
flib_tcpsocket *result = NULL; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
137 |
if(!host || port==0) { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
138 |
flib_log_e("Invalid parameter in flib_socket_connect"); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
139 |
} else { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
140 |
IPaddress ip; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
141 |
if(SDLNet_ResolveHost(&ip,host,port)==-1) { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
142 |
flib_log_e("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
143 |
} else { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
144 |
TCPsocket sock=SDLNet_TCP_Open(&ip); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
145 |
if(!sock) { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
146 |
flib_log_e("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
147 |
} else { |
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
148 |
result = createSocket(sock); |
7234
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
149 |
if(result) { |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
150 |
sock = NULL; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
151 |
} |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
152 |
} |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
153 |
SDLNet_TCP_Close(sock); |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
154 |
} |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
155 |
} |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
156 |
return result; |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
157 |
} |
613998625a3c
frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
158 |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
159 |
void flib_socket_close(flib_tcpsocket *sock) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
160 |
if(sock) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
161 |
SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
162 |
SDLNet_TCP_Close(sock->sock); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
163 |
SDLNet_FreeSocketSet(sock->sockset); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
164 |
free(sock); |
7171 | 165 |
} |
166 |
} |
|
167 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
168 |
int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
169 |
if(!sock || (maxlen>0 && !data)) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
170 |
flib_log_e("Call to flib_socket_nbrecv with sock==null or data==null"); |
7171 | 171 |
return -1; |
172 |
} |
|
173 |
int readySockets = SDLNet_CheckSockets(sock->sockset, 0); |
|
174 |
if(readySockets>0) { |
|
175 |
int size = SDLNet_TCP_Recv(sock->sock, data, maxlen); |
|
176 |
return size>0 ? size : -1; |
|
177 |
} else if(readySockets==0) { |
|
178 |
return 0; |
|
179 |
} else { |
|
180 |
flib_log_e("Error in select system call: %s", SDLNet_GetError()); |
|
181 |
return -1; |
|
182 |
} |
|
183 |
} |
|
184 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
185 |
int flib_socket_send(flib_tcpsocket *sock, const void *data, int len) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
186 |
if(!sock || (len>0 && !data)) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
187 |
flib_log_e("Call to flib_socket_send with sock==null or data==null"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
188 |
return -1; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
189 |
} |
7171 | 190 |
return SDLNet_TCP_Send(sock->sock, data, len); |
191 |
} |