Skip to content

Commit

Permalink
Add --listen_port to customise net listen port (fixes #840)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Mar 24, 2024
1 parent 9ade064 commit b84c4a8
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/cdogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/cdogs/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,6 +39,8 @@
#include "sounds.h"
#include "utils.h"

#define NET_DEFAULT_LISTEN_PORT 34219


const char *DifficultyStr(int d)
{
Expand Down Expand Up @@ -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;
}
9 changes: 5 additions & 4 deletions src/cdogs/net_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/cdogs/net_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/cdogs/net_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/net_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/cdogs/net_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,8 +38,6 @@
#include "map.h"
#include "player.h"

#define NET_LISTEN_PORT 34219

#define NET_PROTOCOL_VERSION 15

// Messages
Expand Down
22 changes: 14 additions & 8 deletions src/command_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/prep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit b84c4a8

Please sign in to comment.