30 #include "network_server.h" |
30 #include "network_server.h" |
31 #include "network_udp.h" |
31 #include "network_udp.h" |
32 #include "network_gamelist.h" |
32 #include "network_gamelist.h" |
33 #include "core/udp.h" |
33 #include "core/udp.h" |
34 #include "core/tcp.h" |
34 #include "core/tcp.h" |
|
35 #include "core/core.h" |
35 #include "network_gui.h" |
36 #include "network_gui.h" |
36 #include "../console.h" /* IConsoleCmdExec */ |
37 #include "../console.h" /* IConsoleCmdExec */ |
37 #include <stdarg.h> /* va_list */ |
38 #include <stdarg.h> /* va_list */ |
38 #include "../md5.h" |
39 #include "../md5.h" |
39 |
40 |
40 #ifdef __MORPHOS__ |
|
41 // the library base is required here |
|
42 struct Library *SocketBase = NULL; |
|
43 #endif |
|
44 |
|
45 // The listen socket for the server |
41 // The listen socket for the server |
46 static SOCKET _listensocket; |
42 static SOCKET _listensocket; |
47 |
43 |
48 // The amount of clients connected |
44 // The amount of clients connected |
49 static byte _network_clients_connected = 0; |
45 static byte _network_clients_connected = 0; |
83 // Function that looks up the CS for a given client-index |
79 // Function that looks up the CS for a given client-index |
84 NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index) |
80 NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index) |
85 { |
81 { |
86 NetworkClientState *cs; |
82 NetworkClientState *cs; |
87 |
83 |
88 for (cs = _clients; cs != &_clients[MAX_CLIENT_INFO]; cs++) { |
84 for (cs = _clients; cs != endof(_clients); cs++) { |
89 if (cs->index == client_index) return cs; |
85 if (cs->index == client_index) return cs; |
90 } |
86 } |
91 |
87 |
92 return NULL; |
88 return NULL; |
93 } |
89 } |
831 if (_network_server) { |
827 if (_network_server) { |
832 // We are a server, also close the listensocket |
828 // We are a server, also close the listensocket |
833 closesocket(_listensocket); |
829 closesocket(_listensocket); |
834 _listensocket = INVALID_SOCKET; |
830 _listensocket = INVALID_SOCKET; |
835 DEBUG(net, 1, "Closed listener"); |
831 DEBUG(net, 1, "Closed listener"); |
836 NetworkUDPClose(); |
832 NetworkUDPStop(); |
837 } |
833 } |
838 } |
834 } |
839 |
835 |
840 // Inits the network (cleans sockets and stuff) |
836 // Inits the network (cleans sockets and stuff) |
841 static void NetworkInitialize(void) |
837 static void NetworkInitialize(void) |
1338 |
1334 |
1339 /* _network_unique_id is our id */ |
1335 /* _network_unique_id is our id */ |
1340 snprintf(_network_unique_id, sizeof(_network_unique_id), "%s", hex_output); |
1336 snprintf(_network_unique_id, sizeof(_network_unique_id), "%s", hex_output); |
1341 } |
1337 } |
1342 |
1338 |
1343 // This tries to launch the network for a given OS |
1339 /** This tries to launch the network for a given OS */ |
1344 void NetworkStartUp(void) |
1340 void NetworkStartUp(void) |
1345 { |
1341 { |
1346 DEBUG(net, 3, "[core] starting network..."); |
1342 DEBUG(net, 3, "[core] starting network..."); |
1347 |
1343 |
1348 #if defined(__MORPHOS__) || defined(__AMIGA__) |
1344 /* Network is available */ |
1349 /* |
1345 _network_available = NetworkCoreInitialize();; |
1350 * IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_ |
|
1351 * network related function, else: crash. |
|
1352 */ |
|
1353 DEBUG(net, 3, "[core] loading bsd socket library"); |
|
1354 SocketBase = OpenLibrary("bsdsocket.library", 4); |
|
1355 if (SocketBase == NULL) { |
|
1356 DEBUG(net, 0, "[core] can't open bsdsocket.library version 4, network unavailable"); |
|
1357 _network_available = false; |
|
1358 return; |
|
1359 } |
|
1360 |
|
1361 #if defined(__AMIGA__) |
|
1362 // for usleep() implementation (only required for legacy AmigaOS builds) |
|
1363 TimerPort = CreateMsgPort(); |
|
1364 if (TimerPort != NULL) { |
|
1365 TimerRequest = (struct timerequest*)CreateIORequest(TimerPort, sizeof(struct timerequest); |
|
1366 if (TimerRequest != NULL) { |
|
1367 if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest*)TimerRequest, 0) == 0) { |
|
1368 TimerBase = TimerRequest->tr_node.io_Device; |
|
1369 if (TimerBase == NULL) { |
|
1370 // free ressources... |
|
1371 DEBUG(net, 0, "[core] can't initialize timer, network unavailable"); |
|
1372 _network_available = false; |
|
1373 return; |
|
1374 } |
|
1375 } |
|
1376 } |
|
1377 } |
|
1378 #endif // __AMIGA__ |
|
1379 #endif // __MORPHOS__ / __AMIGA__ |
|
1380 |
|
1381 // Network is available |
|
1382 _network_available = true; |
|
1383 _network_dedicated = false; |
1346 _network_dedicated = false; |
1384 _network_last_advertise_frame = 0; |
1347 _network_last_advertise_frame = 0; |
1385 _network_need_advertise = true; |
1348 _network_need_advertise = true; |
1386 _network_advertise_retries = 0; |
1349 _network_advertise_retries = 0; |
1387 |
1350 |
1402 _network_game_info.clients_max = cl_max; |
1365 _network_game_info.clients_max = cl_max; |
1403 _network_game_info.companies_max = cp_max; |
1366 _network_game_info.companies_max = cp_max; |
1404 _network_game_info.spectators_max = sp_max; |
1367 _network_game_info.spectators_max = sp_max; |
1405 } |
1368 } |
1406 |
1369 |
1407 // Let's load the network in windows |
|
1408 #if defined(WIN32) |
|
1409 { |
|
1410 WSADATA wsa; |
|
1411 DEBUG(net, 3, "[core] loading windows socket library"); |
|
1412 if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) { |
|
1413 DEBUG(net, 0, "[core] WSAStartup failed, network unavailable"); |
|
1414 _network_available = false; |
|
1415 return; |
|
1416 } |
|
1417 } |
|
1418 #endif // WIN32 |
|
1419 |
1370 |
1420 NetworkInitialize(); |
1371 NetworkInitialize(); |
1421 DEBUG(net, 3, "[core] network online, multiplayer available"); |
1372 DEBUG(net, 3, "[core] network online, multiplayer available"); |
1422 NetworkFindIPs(); |
1373 NetworkFindIPs(); |
1423 } |
1374 } |
1424 |
1375 |
1425 // This shuts the network down |
1376 /** This shuts the network down */ |
1426 void NetworkShutDown(void) |
1377 void NetworkShutDown(void) |
1427 { |
1378 { |
1428 NetworkDisconnect(); |
1379 NetworkDisconnect(); |
1429 NetworkUDPClose(); |
1380 NetworkUDPStop(); |
1430 |
1381 |
1431 DEBUG(net, 3, "[core] shutting down network"); |
1382 DEBUG(net, 3, "[core] shutting down network"); |
1432 |
1383 |
1433 _network_available = false; |
1384 _network_available = false; |
1434 |
1385 |
1435 #if defined(__MORPHOS__) || defined(__AMIGA__) |
1386 NetworkCoreShutdown(); |
1436 // free allocated ressources |
|
1437 #if defined(__AMIGA__) |
|
1438 if (TimerBase != NULL) CloseDevice((struct IORequest*)TimerRequest); // XXX This smells wrong |
|
1439 if (TimerRequest != NULL) DeleteIORequest(TimerRequest); |
|
1440 if (TimerPort != NULL) DeleteMsgPort(TimerPort); |
|
1441 #endif |
|
1442 |
|
1443 if (SocketBase != NULL) CloseLibrary(SocketBase); |
|
1444 #endif |
|
1445 |
|
1446 #if defined(WIN32) |
|
1447 WSACleanup(); |
|
1448 #endif |
|
1449 } |
1387 } |
1450 |
1388 |
1451 #endif /* ENABLE_NETWORK */ |
1389 #endif /* ENABLE_NETWORK */ |