This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
SDLnetTCP.c
293 lines (255 loc) · 8.69 KB
/
SDLnetTCP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
SDL_net: An example cross-platform network library for use with SDL
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDLnetsys.h"
#include "SDL_net.h"
/* The network API for TCP sockets */
/* Since the UNIX/Win32/BeOS code is so different from MacOS,
we'll just have two completely different sections here.
*/
struct _TCPsocket {
int ready;
SOCKET channel;
IPaddress remoteAddress;
IPaddress localAddress;
int sflag;
};
/* Open a TCP network socket
If 'remote' is NULL, this creates a local server socket on the given port,
otherwise a TCP connection to the remote host and port is attempted.
The newly created socket is returned, or NULL if there was an error.
*/
TCPsocket SDLNet_TCP_Open(IPaddress *ip)
{
TCPsocket sock;
struct sockaddr_in sock_addr;
/* Allocate a TCP socket structure */
sock = (TCPsocket)SDL_malloc(sizeof(*sock));
if ( sock == NULL ) {
SDLNet_SetError("Out of memory");
goto error_return;
}
/* Open the socket */
sock->channel = socket(AF_INET, SOCK_STREAM, 0);
if ( sock->channel == INVALID_SOCKET ) {
SDLNet_SetError("Couldn't create socket");
goto error_return;
}
/* Connect to remote, or bind locally, as appropriate */
if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) {
/* ######### Connecting to remote */
SDL_memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = ip->host;
sock_addr.sin_port = ip->port;
/* Connect to the remote host */
if ( connect(sock->channel, (struct sockaddr *)&sock_addr,
sizeof(sock_addr)) == SOCKET_ERROR ) {
SDLNet_SetError("Couldn't connect to remote host");
goto error_return;
}
sock->sflag = 0;
} else {
/* ########## Binding locally */
SDL_memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = INADDR_ANY;
sock_addr.sin_port = ip->port;
/*
* Windows gets bad mojo with SO_REUSEADDR:
* http://www.devolution.com/pipermail/sdl/2005-September/070491.html
* --ryan.
*/
#ifndef WIN32
/* allow local address reuse */
{ int yes = 1;
setsockopt(sock->channel, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
}
#endif
/* Bind the socket for listening */
if ( bind(sock->channel, (struct sockaddr *)&sock_addr,
sizeof(sock_addr)) == SOCKET_ERROR ) {
SDLNet_SetError("Couldn't bind to local port");
goto error_return;
}
if ( listen(sock->channel, 5) == SOCKET_ERROR ) {
SDLNet_SetError("Couldn't listen to local port");
goto error_return;
}
/* Set the socket to non-blocking mode for accept() */
#if defined(__BEOS__) && defined(SO_NONBLOCK)
/* On BeOS r5 there is O_NONBLOCK but it's for files only */
{
long b = 1;
setsockopt(sock->channel, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
}
#elif defined(O_NONBLOCK)
{
fcntl(sock->channel, F_SETFL, O_NONBLOCK);
}
#elif defined(WIN32)
{
unsigned long mode = 1;
ioctlsocket (sock->channel, FIONBIO, &mode);
}
#elif defined(__OS2__)
{
int dontblock = 1;
ioctl(sock->channel, FIONBIO, &dontblock);
}
#else
#warning How do we set non-blocking mode on other operating systems?
#endif
sock->sflag = 1;
}
sock->ready = 0;
#ifdef TCP_NODELAY
/* Set the nodelay TCP option for real-time games */
{ int yes = 1;
setsockopt(sock->channel, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(yes));
}
#else
#warning Building without TCP_NODELAY
#endif /* TCP_NODELAY */
/* Fill in the channel host address */
sock->remoteAddress.host = sock_addr.sin_addr.s_addr;
sock->remoteAddress.port = sock_addr.sin_port;
/* The socket is ready */
return(sock);
error_return:
SDLNet_TCP_Close(sock);
return(NULL);
}
/* Accept an incoming connection on the given server socket.
The newly created socket is returned, or NULL if there was an error.
*/
TCPsocket SDLNet_TCP_Accept(TCPsocket server)
{
TCPsocket sock;
struct sockaddr_in sock_addr;
socklen_t sock_alen;
/* Only server sockets can accept */
if ( ! server->sflag ) {
SDLNet_SetError("Only server sockets can accept()");
return(NULL);
}
server->ready = 0;
/* Allocate a TCP socket structure */
sock = (TCPsocket)SDL_malloc(sizeof(*sock));
if ( sock == NULL ) {
SDLNet_SetError("Out of memory");
goto error_return;
}
/* Accept a new TCP connection on a server socket */
sock_alen = sizeof(sock_addr);
sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr,
&sock_alen);
if ( sock->channel == INVALID_SOCKET ) {
SDLNet_SetError("accept() failed");
goto error_return;
}
#ifdef WIN32
{
/* passing a zero value, socket mode set to block on */
unsigned long mode = 0;
ioctlsocket (sock->channel, FIONBIO, &mode);
}
#elif defined(O_NONBLOCK)
{
int flags = fcntl(sock->channel, F_GETFL, 0);
fcntl(sock->channel, F_SETFL, flags & ~O_NONBLOCK);
}
#endif /* WIN32 */
sock->remoteAddress.host = sock_addr.sin_addr.s_addr;
sock->remoteAddress.port = sock_addr.sin_port;
sock->sflag = 0;
sock->ready = 0;
/* The socket is ready */
return(sock);
error_return:
SDLNet_TCP_Close(sock);
return(NULL);
}
/* Get the IP address of the remote system associated with the socket.
If the socket is a server socket, this function returns NULL.
*/
IPaddress *SDLNet_TCP_GetPeerAddress(TCPsocket sock)
{
if ( sock->sflag ) {
return(NULL);
}
return(&sock->remoteAddress);
}
/* Send 'len' bytes of 'data' over the non-server socket 'sock'
This function returns the actual amount of data sent. If the return value
is less than the amount of data sent, then either the remote connection was
closed, or an unknown socket error occurred.
*/
int SDLNet_TCP_Send(TCPsocket sock, const void *datap, int len)
{
const Uint8 *data = (const Uint8 *)datap; /* For pointer arithmetic */
int sent, left;
/* Server sockets are for accepting connections only */
if ( sock->sflag ) {
SDLNet_SetError("Server sockets cannot send");
return(-1);
}
/* Keep sending data until it's sent or an error occurs */
left = len;
sent = 0;
SDLNet_SetLastError(0);
do {
len = send(sock->channel, (const char *) data, left, 0);
if ( len > 0 ) {
sent += len;
left -= len;
data += len;
}
} while ( (left > 0) && ((len > 0) || (SDLNet_GetLastError() == EINTR)) );
return(sent);
}
/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock',
and store them in the buffer pointed to by 'data'.
This function returns the actual amount of data received. If the return
value is less than or equal to zero, then either the remote connection was
closed, or an unknown socket error occurred.
*/
int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)
{
int len;
/* Server sockets are for accepting connections only */
if ( sock->sflag ) {
SDLNet_SetError("Server sockets cannot receive");
return(-1);
}
SDLNet_SetLastError(0);
do {
len = recv(sock->channel, (char *) data, maxlen, 0);
} while ( SDLNet_GetLastError() == EINTR );
sock->ready = 0;
return(len);
}
/* Close a TCP network socket */
void SDLNet_TCP_Close(TCPsocket sock)
{
if ( sock != NULL ) {
if ( sock->channel != INVALID_SOCKET ) {
closesocket(sock->channel);
}
SDL_free(sock);
}
}