Debian Codegraph @

src/io/network.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file network.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created level.c and menu.c
10 * - 3rd of February 2009: Renamed level.c to level.cpp and menu.c to menu.cpp
11 * - 9th March 2009: Created game.cpp from parts of menu.cpp and level.cpp
12 * - 3rd June 2009: Created network.cpp from parts of game.cpp
13 *
14 * @par Licence:
15 * Copyright (c) 2005-2017 AJ Thomson
16 * Copyright (c) 2015-2026 Carsten Teibes
17 *
18 * OpenJazz is distributed under the terms of
19 * the GNU General Public License, version 2.0
20 *
21 * @par Description:
22 * Deals with a platform-specific networking API.
23 *
24 * On most platforms, USE_SOCKETS should be defined.
25 *
26 */
27
28
29 #include "controls.h"
30 #include "gfx/font.h"
31 #include "gfx/video.h"
32 #include "network.h"
33
34 #include "platforms/platforms.h"
35 #include "loop.h"
36 #include "util.h"
37 #include "io/log.h"
38
39 #ifdef USE_SOCKETS
40 #ifdef _WIN32
41 #include <winsock.h>
42 #define ioctl ioctlsocket
43 #define socklen_t int
44 #ifndef EWOULDBLOCK
45 #define EWOULDBLOCK WSAEWOULDBLOCK
46 #endif
47 #else
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <arpa/inet.h>
51 #include <sys/select.h>
52 #include <sys/ioctl.h>
53 #include <netinet/in.h>
54 #include <unistd.h>
55 #include <errno.h>
56 #include <string.h>
57 #endif
58 #ifndef MSG_NOSIGNAL
59 #define MSG_NOSIGNAL 0
60 #endif
61 #elif defined(__wii__)
62 #include <network.h>
63 #elif defined(USE_SDL_NET)
64 #include <arpa/inet.h>
65 #endif
66
67
68 /**
69 * Initialise networking.
70 */
71 Network::Network () {
72
73 #ifdef USE_SOCKETS
74 #ifdef _WIN32
75 WSADATA WSAData;
76
77 // Start Windows Sockets
78 WSAStartup(MAKEWORD(1, 0), &WSAData);
79 #endif
80 if (!platform->NetHasConsole())
81 platform->NetInit();
82 #elif defined USE_SDL_NET
83 # ifdef __wii__
84 char ip[16];
85
86 // Initialize Wii networking (using dhcp)
87 if_config(ip, NULL, NULL, true, 20);
88 # endif
89 SDLNet_Init();
90 #endif
91
92 }
93
94
95 /**
96 * De-initialise networking.
97 */
98 Network::~Network () {
99
100 #ifdef USE_SOCKETS
101 #ifdef _WIN32
102 // Shut down Windows Sockets
103 WSACleanup();
104 #endif
105 if (!platform->NetHasConsole())
106 platform->NetExit();
107 #elif defined USE_SDL_NET
108 SDLNet_Quit();
109 # ifdef __wii__
110 net_deinit();
111 # endif
112 #endif
113
114 }
115
116
117 /**
118 * Open a host connection.
119 *
120 * @return Connection socket or error code
121 */
122 int Network::host () {
123
124 #ifdef USE_SOCKETS
125 sockaddr_in sockAddr;
126 int sock = socket(AF_INET, SOCK_STREAM, 0);
127
128 if (sock == -1) return E_N_SOCKET;
129
130
131 // Make the socket non-blocking
132 int nonblock = 1;
133 ioctl(sock, FIONBIO, (u_long *)&nonblock);
134
135 memset(&sockAddr, 0, sizeof(sockaddr_in));
136 sockAddr.sin_family = AF_INET;
137 sockAddr.sin_addr.s_addr = INADDR_ANY;
138 sockAddr.sin_port = htons(NET_PORT);
139
140
141 if (bind(sock, (sockaddr *)&sockAddr, sizeof(sockaddr_in))) {
142
143 close(sock);
144
145 return E_N_BIND;
146
147 }
148
149 if (listen(sock, MAX_CLIENTS) == -1) {
150
151 close(sock);
152
153 return E_N_LISTEN;
154
155 }
156
157 return sock;
158 #elif defined USE_SDL_NET
159 ipAddress.port = NET_PORT;
160 ipAddress.host = 0;
161 socket = SDLNet_TCP_Open(&ipAddress);
162
163 if (socket == NULL) return E_N_SOCKET;
164
165 return (int)socket;
166 #else
167 return E_N_OTHER;
168 #endif
169
170 }
171
172
173 /**
174 * Open a client connection to the specified server.
175 *
176 * @param address Address of the server
177 *
178 * @return Connection socket or error code
179 */
180 int Network::join (char *address) {
181
182 #ifdef USE_SOCKETS
183 sockaddr_in sockAddr;
184 fd_set writefds;
185 timeval timeouttv;
186 unsigned int timeout;
187 int sock, con;
188
189 // Create socket
190 sock = socket(AF_INET, SOCK_STREAM, 0);
191
192 if (sock == -1) return E_N_SOCKET;
193
194 // Make socket non-blocking
195 con = 1;
196 ioctl(sock, FIONBIO, (u_long *)&con);
197
198 // Connect to server
199 memset(&sockAddr, 0, sizeof(sockaddr_in));
200 sockAddr.sin_family = AF_INET;
201 sockAddr.sin_port = htons(NET_PORT);
202
203 #ifdef _WIN32
204 sockAddr.sin_addr.s_addr = inet_addr(address);
205 #else
206 if (inet_aton(address, &(sockAddr.sin_addr)) == 0) return E_N_ADDRESS;
207 #endif
208
209 // Initiate connection
210 con = connect(sock, (sockaddr *)&sockAddr, sizeof(sockAddr));
211
212 // If the connection completed, return
213 if (con == 0) return sock;
214
215
216 // Wait for connection to complete
217
218 con = 0;
219 timeout = globalTicks + T_TIMEOUT;
220
221 while (!con) {
222
223 if (loop(NORMAL_LOOP) == E_QUIT) {
224
225 close(sock);
226
227 return E_QUIT;
228
229 }
230
231 if (controls.release(C_ESCAPE)) {
232
233 close(sock);
234
235 return E_RETURN;
236
237 }
238
239 video.clearScreen(0);
240 fontmn2->showStringCentered("CONNECTING TO SERVER");
241
242 FD_ZERO(&writefds);
243 FD_SET(sock, &writefds);
244 timeouttv.tv_sec = 0;
245 timeouttv.tv_usec = T_MENU_FRAME;
246 con = select(sock + 1, NULL, &writefds, NULL, &timeouttv);
247
248 if (con == -1) {
249
250 LOG_WARN("Could not connect to server - code: %d", getError());
251
252 close(sock);
253
254 return E_N_CONNECT;
255
256 }
257
258 if (globalTicks > timeout) {
259
260 close(sock);
261
262 return E_TIMEOUT;
263
264 }
265
266 }
267
268 return sock;
269 #elif defined USE_SDL_NET
270 video.clearScreen(0);
271 fontmn2->showStringCentered("CONNECTING TO SERVER");
272 loop(NORMAL_LOOP);
273 ipAddress.port = NET_PORT;
274 ipAddress.host = inet_addr(address);
275 socket = SDLNet_TCP_Open(&ipAddress);
276
277 if (socket == NULL) return -1;
278
279 return (int)socket;
280 #else
281 return E_N_OTHER;
282 #endif
283
284 }
285
286
287 /**
288 * Accept a connection to a client
289 *
290 * @param sock The host connection socket
291 *
292 * @return Client connection socket, or -1 for no connection
293 */
294 int Network::accept (int sock) {
295
296 #ifdef USE_SOCKETS
297 sockaddr_in sockAddr;
298 int clientSocket, length;
299
300 length = sizeof(sockaddr_in);
301
302 clientSocket = ::accept(sock, (sockaddr *)&sockAddr, (socklen_t *)&length);
303
304 if (clientSocket != -1) {
305
306 // Make the socket non-blocking
307 length = 1;
308 ioctl(clientSocket, FIONBIO, (u_long *)&length);
309
310 }
311
312 return clientSocket;
313 #elif defined USE_SDL_NET
314 clientSocket = SDLNet_TCP_Accept((TCPsocket)sock);
315
316 if (clientSocket == NULL) return -1;
317
318 return (int)&clientSocket;
319 #else
320 return -1;
321 #endif
322
323 }
324
325
326 /**
327 * Close a connection.
328 *
329 * @param sock The connection socket
330 */
331 void Network::close (int sock) {
332
333 #ifdef USE_SOCKETS
334 #ifdef _WIN32
335 closesocket(sock);
336 #else
337 ::close(sock);
338 #endif
339 #elif defined USE_SDL_NET
340 SDLNet_TCP_Close((TCPsocket)sock);
341 #endif
342
343 }
344
345
346 /**
347 * Send data over the specified connection.
348 *
349 * @param sock Connection socket
350 * @param buffer Data to be sent
351 *
352 * @return Number of bytes sent, or -1 for failure
353 */
354 int Network::send (int sock, unsigned char *buffer) {
355
356 #ifdef USE_SOCKETS
357 return ::send(sock, reinterpret_cast<char*>(buffer), buffer[0], MSG_NOSIGNAL);
358 #elif defined USE_SDL_NET
359 return SDLNet_TCP_Send((TCPsocket)sock, reinterpret_cast<char*>(buffer), buffer[0]);
360 #else
361 return 0;
362 #endif
363
364 }
365
366
367 /**
368 * Receive data from the specified connection.
369 *
370 * @param sock Connection socket
371 * @param buffer Buffer to receive data
372 * @param length The size of the buffer, in bytes
373 *
374 * @return Number of bytes received, or -1 for failure
375 */
376 int Network::recv (int sock, unsigned char *buffer, int length) {
377
378 #ifdef USE_SOCKETS
379 return ::recv(sock, reinterpret_cast<char*>(buffer), length, MSG_NOSIGNAL);
380 #elif defined USE_SDL_NET
381 return SDLNet_TCP_Recv((TCPsocket)sock, buffer, length);
382 #else
383 return 0;
384 #endif
385
386 }
387
388
389 /**
390 * Check if a given socket is connected.
391 *
392 * @param sock The socket
393 *
394 * @return True if connected
395 */
396 bool Network::isConnected (int sock) {
397
398 #ifdef USE_SOCKETS
399 int length;
400 char buffer;
401
402 // Check for incoming data
403 length = ::recv(sock, &buffer, 1, MSG_PEEK | MSG_NOSIGNAL);
404
405 // Still connected if data was received or if there was no data to receive
406 return (length != -1) || (getError() == EWOULDBLOCK);
407 #elif defined USE_SDL_NET
408 return SDLNet_SocketReady((TCPsocket)sock);
409 #else
410 return false;
411 #endif
412
413 }
414
415
416 /**
417 * Get the last error code.
418 *
419 * @return Network library error code
420 */
421 int Network::getError () {
422
423 #ifdef USE_SOCKETS
424 #ifdef _WIN32
425 return WSAGetLastError();
426 #else
427 return errno;
428 #endif
429 #elif defined USE_SDL_NET
430 return (int)SDLNet_GetError();
431 #else
432 return 0;
433 #endif
434
435 }