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"cxx . . $ `<file>/src/io/controls.h`/
30 #include "gfx/font.h"cxx . . $ `<file>/src/io/gfx/font.h`/
31 #include "gfx/video.h"cxx . . $ `<file>/src/io/gfx/video.h`/
32 #include "network.h"cxx . . $ `<file>/src/io/network.h`/
33
34 #include "platforms/platforms.h"cxx . . $ `<file>/src/platforms/platforms.h`/
35 #include "loop.h"cxx . . $ `<file>/src/loop.h`/
36 #include "util.h"cxx . . $ `<file>/src/util.h`/
37 #include "io/log.h"cxx . . $ `<file>/src/io/log.h`/
38
39 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
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_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!
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 Networkcxx . . $ Network#::Networkcxx . . $ Network#Network(49f6e7a06ebc5aa8). () {
72
73 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
74 #ifdef _WIN32
75 WSADATA WSAData;
76
77 // Start Windows Sockets
78 WSAStartup(MAKEWORD(1, 0), &WSAData);
79 #endif
80 if (!platformcxx . . $ platform.->NetHasConsolecxx . . $ IPlatform#NetHasConsole(c9857a73d68e59ba).())
81 platformcxx . . $ platform.->NetInitcxx . . $ IPlatform#NetInit(49f6e7a06ebc5aa8).();
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 Networkcxx . . $ Network#::~cxx . . $ Network#`~Network`(49f6e7a06ebc5aa8).Networkcxx . . $ Network# () {
99
100 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
101 #ifdef _WIN32
102 // Shut down Windows Sockets
103 WSACleanup();
104 #endif
105 if (!platformcxx . . $ platform.->NetHasConsolecxx . . $ IPlatform#NetHasConsole(c9857a73d68e59ba).())
106 platformcxx . . $ platform.->NetExitcxx . . $ IPlatform#NetExit(49f6e7a06ebc5aa8).();
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 Networkcxx . . $ Network#::hostcxx . . $ Network#host(b126dc7c1de90089). () {
123
124 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
125 sockaddr_in sockAddr;
126 int sock = socket(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0);
127
128 if (sock == -1) return E_N_SOCKETcxx . . $ `src/OpenJazz.h:55:9`!;
129
130
131 // Make the socket non-blocking
132 int nonblock = 1;
133 ioctl(sock, FIONBIOcxx . . $ `<external>/123502480044527469/ioctls.h:53:9`!, (u_long *)&nonblock);
134
135 memset(&sockAddr, 0, sizeof(sockaddr_in));
136 sockAddr.sin_family = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!;
137 sockAddr.sin_addr.s_addr = INADDR_ANYcxx . . $ `<external>/9581057695539925927/in.h:198:9`!;
138 sockAddr.sin_port = htons(NET_PORTcxx . . $ `src/io/network.h:38:9`!);
139
140
141 if (bind(sock, (sockaddr *)&sockAddr, sizeof(sockaddr_in))) {
142
143 closecxx . . $ Network#close(d4f767463ce0a6b3).(sock);
144
145 return E_N_BINDcxx . . $ `src/OpenJazz.h:54:9`!;
146
147 }
148
149 if (listen(sock, MAX_CLIENTScxx . . $ `src/io/network.h:44:9`!) == -1) {
150
151 closecxx . . $ Network#close(d4f767463ce0a6b3).(sock);
152
153 return E_N_LISTENcxx . . $ `src/OpenJazz.h:53:9`!;
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 Networkcxx . . $ Network#::joincxx . . $ Network#join(1ef8e6ec083aabb9). (char *address) {
181
182 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
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_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0);
191
192 if (sock == -1) return E_N_SOCKETcxx . . $ `src/OpenJazz.h:55:9`!;
193
194 // Make socket non-blocking
195 con = 1;
196 ioctl(sock, FIONBIOcxx . . $ `<external>/123502480044527469/ioctls.h:53:9`!, (u_long *)&con);
197
198 // Connect to server
199 memset(&sockAddr, 0, sizeof(sockaddr_in));
200 sockAddr.sin_family = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!;
201 sockAddr.sin_port = htons(NET_PORTcxx . . $ `src/io/network.h:38:9`!);
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_ADDRESScxx . . $ `src/OpenJazz.h:52:9`!;
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 = globalTickscxx . . $ globalTicks. + T_TIMEOUTcxx . . $ `src/io/network.h:41:9`!;
220
221 while (!con) {
222
223 if (loopcxx . . $ loop(fd873fd06cfe1135).(NORMAL_LOOPcxx . . $ NORMAL_LOOP.) == E_QUITcxx . . $ `src/OpenJazz.h:67:9`!) {
224
225 closecxx . . $ Network#close(d4f767463ce0a6b3).(sock);
226
227 return E_QUITcxx . . $ `src/OpenJazz.h:67:9`!;
228
229 }
230
231 if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ESCAPEcxx . . $ `src/io/controls.h:39:9`!)) {
232
233 closecxx . . $ Network#close(d4f767463ce0a6b3).(sock);
234
235 return E_RETURNcxx . . $ `src/OpenJazz.h:66:9`!;
236
237 }
238
239 videocxx . . $ video..clearScreencxx . . $ Video#clearScreen(d4f767463ce0a6b3).(0);
240 fontmn2cxx . . $ fontmn2.->showStringCenteredcxx . . $ Font#showStringCentered(dffc78c69bf84bb8).("CONNECTING TO SERVER");
241
242 FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(&writefds);
243 FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(sock, &writefds);
244 timeouttv.tv_sec = 0;
245 timeouttv.tv_usec = T_MENU_FRAMEcxx . . $ `src/io/gfx/video.h:78:9`!;
246 con = select(sock + 1, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, &writefds, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, &timeouttv);
247
248 if (con == -1) {
249
250 LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Could not connect to server - code: %d", getError());
251
252 closecxx . . $ Network#close(d4f767463ce0a6b3).(sock);
253
254 return E_N_CONNECTcxx . . $ `src/OpenJazz.h:51:9`!;
255
256 }
257
258 if (globalTickscxx . . $ globalTicks. > timeout) {
259
260 closecxx . . $ Network#close(d4f767463ce0a6b3).(sock);
261
262 return E_TIMEOUTcxx . . $ `src/OpenJazz.h:58:9`!;
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 Networkcxx . . $ Network#::acceptcxx . . $ Network#accept(7864480464b09eea). (int sock) {
295
296 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
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, FIONBIOcxx . . $ `<external>/123502480044527469/ioctls.h:53:9`!, (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 Networkcxx . . $ Network#::closecxx . . $ Network#close(d4f767463ce0a6b3). (int sock) {
332
333 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
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 Networkcxx . . $ Network#::sendcxx . . $ Network#send(b2cc8657ba51080d). (int sock, unsigned char *buffer) {
355
356 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
357 return ::send(sock, reinterpret_cast<char*>(buffer), buffer[0], MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!);
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 Networkcxx . . $ Network#::recvcxx . . $ Network#recv(ef9024b465536c23). (int sock, unsigned char *buffer, int length) {
377
378 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
379 return ::recv(sock, reinterpret_cast<char*>(buffer), length, MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!);
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 Networkcxx . . $ Network#::isConnectedcxx . . $ Network#isConnected(83c267b20bac841a). (int sock) {
397
398 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
399 int length;
400 char buffer;
401
402 // Check for incoming data
403 length = ::recv(sock, &buffer, 1, MSG_PEEKcxx . . $ `<external>/8962547315054503424/socket.h:211:9`! | MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!);
404
405 // Still connected if data was received or if there was no data to receive
406 return (length != -1) || (getErrorcxx . . $ Network#getError(b126dc7c1de90089).() == EWOULDBLOCKcxx . . $ `<external>/17922322188510340118/errno.h:22:9`!);
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 Networkcxx . . $ Network#::getErrorcxx . . $ Network#getError(b126dc7c1de90089). () {
422
423 #ifdef USE_SOCKETScxx . . $ `<built-in>:467:9`!
424 #ifdef _WIN32
425 return WSAGetLastError();
426 #else
427 return errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
428 #endif
429 #elif defined USE_SDL_NET
430 return (int)SDLNet_GetError();
431 #else
432 return 0;
433 #endif
434
435 }