diff --git a/src/cdogs.c b/src/cdogs.c index b5efa277b..ae3e7f19e 100644 --- a/src/cdogs.c +++ b/src/cdogs.c @@ -22,7 +22,7 @@ This file incorporates work covered by the following copyright and permission notice: - Copyright (c) 2013-2017, 2019-2022 Cong Xu + Copyright (c) 2013-2017, 2019-2022, 2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -206,7 +206,7 @@ int main(int argc, char *argv[]) err = EXIT_FAILURE; goto bail; } - NetClientInit(&gNetClient); + NetClientInit(&gNetClient, ConfigGetInt(&gConfig, "ListenPort")); #endif LoadingScreenDraw(&gLoadingScreen, "Initializing sound device...", 0.25f); diff --git a/src/cdogs/config.c b/src/cdogs/config.c index 83da3a15c..2cd662b68 100644 --- a/src/cdogs/config.c +++ b/src/cdogs/config.c @@ -2,7 +2,7 @@ C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2013-2014, 2016-2018 Cong Xu + Copyright (c) 2013-2014, 2016-2018, 2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -39,6 +39,8 @@ #include "sounds.h" #include "utils.h" +#define NET_DEFAULT_LISTEN_PORT 34219 + const char *DifficultyStr(int d) { @@ -739,6 +741,7 @@ Config ConfigDefault(void) ConfigGroupAdd(&root, qp); ConfigGroupAdd(&root, ConfigNewBool("StartServer", false)); + ConfigGroupAdd(&root, ConfigNewInt("ListenPort", NET_DEFAULT_LISTEN_PORT, 0, 65535, 1, NULL, NULL)); return root; } diff --git a/src/cdogs/net_client.c b/src/cdogs/net_client.c index 3864acf71..ebb4146c8 100644 --- a/src/cdogs/net_client.c +++ b/src/cdogs/net_client.c @@ -2,7 +2,7 @@ C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2014-2016, 2019, 2021 Cong Xu + Copyright (c) 2014-2016, 2019, 2021, 2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -50,11 +50,12 @@ NetClient gNetClient; #define TIMEOUT_MS 5000 -void NetClientInit(NetClient *n) +void NetClientInit(NetClient *n, const uint16_t port) { memset(n, 0, sizeof *n); n->ClientId = -1; // -1 is unset n->scanner = ENET_SOCKET_NULL; + n->port = port; n->client = enet_host_create(NULL, 1, 2, 57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */, 14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */); @@ -129,7 +130,7 @@ static bool TryScanHost(NetClient *n, const enet_uint32 host) // Send the scanning message ENetAddress addr; addr.host = host; - addr.port = NET_LISTEN_PORT; + addr.port = n->port; // Send a dummy payload char data = 42; ENetBuffer sendbuf; @@ -300,7 +301,7 @@ void NetClientPoll(NetClient *n) { LOG(LM_NET, LL_ERROR, "connection error(%d)", check); NetClientTerminate(n); - NetClientInit(n); + NetClientInit(n, n->port); return; } else if (check > 0) diff --git a/src/cdogs/net_client.h b/src/cdogs/net_client.h index b44ceaecb..7caa8f836 100644 --- a/src/cdogs/net_client.h +++ b/src/cdogs/net_client.h @@ -2,7 +2,7 @@ C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2014-2016, Cong Xu + Copyright (c) 2014-2016, 2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,6 +49,7 @@ typedef struct bool Ready; // Socket used to scan for LAN servers ENetSocket scanner; + uint16_t port; // Only scan for a period; if > 0 then we are scanning int ScanTicks; // Addresses of scanned LAN servers @@ -59,7 +60,7 @@ typedef struct extern NetClient gNetClient; -void NetClientInit(NetClient *n); +void NetClientInit(NetClient *n, const uint16_t port); void NetClientTerminate(NetClient *n); // Start searching for LAN servers; note that the result will be returned in diff --git a/src/cdogs/net_server.c b/src/cdogs/net_server.c index ba6fc46f5..bda9befcb 100644 --- a/src/cdogs/net_server.c +++ b/src/cdogs/net_server.c @@ -2,7 +2,7 @@ C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2014-2017, 2021, 2023 Cong Xu + Copyright (c) 2014-2017, 2021, 2023-2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -62,8 +62,8 @@ void NetServerReset(NetServer *n) } static ENetHost *HostOpen(void); -static bool ListenSocketTryOpen(ENetSocket *listen); -void NetServerOpen(NetServer *n) +static bool ListenSocketTryOpen(ENetSocket *listen, const enet_uint16 port); +void NetServerOpen(NetServer *n, const uint16_t port) { if (n->server) { @@ -77,7 +77,7 @@ void NetServerOpen(NetServer *n) } // Start listen socket, to respond to UDP scans - if (!ListenSocketTryOpen(&n->listen)) + if (!ListenSocketTryOpen(&n->listen, port)) { return; } @@ -112,7 +112,7 @@ static ENetHost *HostOpen(void) } return host; } -static bool ListenSocketTryOpen(ENetSocket *listen) +static bool ListenSocketTryOpen(ENetSocket *listen, const enet_uint16 port) { *listen = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM); if (*listen == ENET_SOCKET_NULL) @@ -127,7 +127,7 @@ static bool ListenSocketTryOpen(ENetSocket *listen) } ENetAddress addr; addr.host = ENET_HOST_ANY; - addr.port = NET_LISTEN_PORT; + addr.port = port; if (enet_socket_bind(*listen, &addr) != 0) { LOG(LM_NET, LL_ERROR, "failed to bind listen socket"); diff --git a/src/cdogs/net_server.h b/src/cdogs/net_server.h index 9a687d130..cc75e2f4f 100644 --- a/src/cdogs/net_server.h +++ b/src/cdogs/net_server.h @@ -2,7 +2,7 @@ C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2014-2016, Cong Xu + Copyright (c) 2014-2016, 2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -61,7 +61,7 @@ void NetServerTerminate(NetServer *n); void NetServerReset(NetServer *n); // Open a port and start listening for data -void NetServerOpen(NetServer *n); +void NetServerOpen(NetServer *n, const uint16_t port); void NetServerClose(NetServer *n); // Service the recv buffer; if data is received then activate this device void NetServerPoll(NetServer *n); diff --git a/src/cdogs/net_util.h b/src/cdogs/net_util.h index 2bda8cc16..1c1b5e8ac 100644 --- a/src/cdogs/net_util.h +++ b/src/cdogs/net_util.h @@ -2,7 +2,7 @@ C-Dogs SDL A port of the legendary (and fun) action/arcade cdogs. - Copyright (c) 2014-2017, 2019-2023 Cong Xu + Copyright (c) 2014-2017, 2019-2024 Cong Xu All rights reserved. Redistribution and use in source and binary forms, with or without @@ -38,8 +38,6 @@ #include "map.h" #include "player.h" -#define NET_LISTEN_PORT 34219 - #define NET_PROTOCOL_VERSION 15 // Messages diff --git a/src/command_line.c b/src/command_line.c index 2c05b50e1..d253f9e2e 100644 --- a/src/command_line.c +++ b/src/command_line.c @@ -90,15 +90,15 @@ void PrintHelp(void) printf("\n"); } printf( - " --log=M,L Enable logging for module M at level L.\n\n" + " --log=M,L Enable logging for module M at level L.\n\n" " --log=L Enable logging for all modules at level L.\n\n" - " --logfile=F Log to file by filename\n\n"); + " --logfile=F Log to file by filename\n\n"); printf( "%s\n", "Other:\n" " --connect=host (Experimental) connect to a game server\n" - " --demo (Experimental) run game for 30 seconds\n"); + " --demo (Experimental) run game for 30 seconds\n"); } void ProcessCommandLine(char *buf, const int argc, char *argv[]) @@ -129,10 +129,11 @@ bool ParseArgs( {"scale", required_argument, NULL, 's'}, {"screen", required_argument, NULL, 'c'}, {"connect", required_argument, NULL, 'x'}, + {"listen_port", required_argument, NULL, 'p'}, {"config", optional_argument, NULL, 'C'}, {"log", required_argument, NULL, 1000}, {"logfile", required_argument, NULL, 1001}, - {"demo", no_argument, NULL, 1002}, + {"demo", no_argument, NULL, 1002}, {"help", no_argument, NULL, 'h'}, {0, 0, NULL, 0}}; int opt = 0; @@ -168,6 +169,11 @@ bool ParseArgs( case 'h': PrintHelp(); return false; + case 'p': + ConfigGet(&gConfig, "ListenPort")->u.Int.Value = + MAX(atoi(optarg), 0); + printf("Listen port: %d\n", ConfigGetInt(&gConfig, "ListenPort")); + break; case 1000: { char *comma = strchr(optarg, ','); if (comma) @@ -194,10 +200,10 @@ bool ParseArgs( case 1001: LogOpenFile(optarg); break; - case 1002: - *demoQuitTimer = 30 * 1000; - printf("Entering demo mode; will auto-quit in 30 seconds\n"); - break; + case 1002: + *demoQuitTimer = 30 * 1000; + printf("Entering demo mode; will auto-quit in 30 seconds\n"); + break; case 'x': if (enet_address_set_host(connectAddr, optarg) != 0) { diff --git a/src/prep.c b/src/prep.c index ee8e68e1b..d1d6d5140 100644 --- a/src/prep.c +++ b/src/prep.c @@ -729,7 +729,7 @@ static GameLoopResult GameOptionsUpdate(GameLoopData *data, LoopRunner *l) // If enabled, start net server if (!gCampaign.IsClient && ConfigGetBool(&gConfig, "StartServer")) { - NetServerOpen(&gNetServer); + NetServerOpen(&gNetServer, ConfigGetInt(&gConfig, "ListenPort")); } LoopRunnerPush( l, ScreenMissionBriefing(&gCampaign.Setting, &gMission));