Added CLI options to specify ip/port

This commit is contained in:
MysterD 2020-08-13 19:38:15 -07:00
parent 88bcd2a143
commit 7cd5dbb1a4
8 changed files with 43 additions and 38 deletions

View file

@ -7,11 +7,11 @@ if [ ! -f "$FILE" ]; then
FILE=./build/us_pc/sm64.us.f3dex2e
fi
$FILE --server --configfile sm64config_server.txt &
$FILE --server 27015 --configfile sm64config_server.txt &
# debug if cgdb exists
if ! [ -x "$(command -v cgdb)" ]; then
$FILE --client --configfile sm64config_client.txt &
$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt &
else
winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --client --configfile sm64config_client.txt' -ex 'quit'
winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --client 127.0.0.1 27015 --configfile sm64config_client.txt' -ex 'quit'
fi

View file

@ -23,9 +23,9 @@ static void print_help(void) {
printf("%-20s\tStarts the game in windowed mode.\n", "--windowed");
}
static inline int arg_string(const char *name, const char *value, char *target) {
static inline int arg_string(const char *name, const char *value, char *target, int maxLength) {
const unsigned int arglen = strlen(value);
if (arglen >= SYS_MAX_PATH) {
if (arglen >= maxLength) {
fprintf(stderr, "Supplied value for `%s` is too long.\n", name);
return 0;
}
@ -48,23 +48,26 @@ void parse_cli_opts(int argc, char* argv[]) {
else if (strcmp(argv[i], "--windowed") == 0) // Open game in windowed mode
gCLIOpts.FullScreen = 2;
else if (strcmp(argv[i], "--server") == 0) // Host server
else if (strcmp(argv[i], "--server") == 0 && (i + 1) < argc) { // Host server
gCLIOpts.Network = NT_SERVER;
arg_string("--server <port>", argv[++i], gCLIOpts.NetworkPort, PORT_MAX_LEN);
else if (strcmp(argv[i], "--client") == 0) // Join server
} else if (strcmp(argv[i], "--client") == 0 && (i + 2) < argc) { // Join server
gCLIOpts.Network = NT_CLIENT;
arg_string("--client <ip>", argv[++i], gCLIOpts.JoinIp, IP_MAX_LEN);
arg_string("--client <port>", argv[++i], gCLIOpts.NetworkPort, PORT_MAX_LEN);
else if (strcmp(argv[i], "--cheats") == 0) // Enable cheats menu
} else if (strcmp(argv[i], "--cheats") == 0) // Enable cheats menu
Cheats.EnableCheats = true;
else if (strcmp(argv[i], "--configfile") == 0 && (i + 1) < argc)
arg_string("--configfile", argv[++i], gCLIOpts.ConfigFile);
arg_string("--configfile", argv[++i], gCLIOpts.ConfigFile, SYS_MAX_PATH);
else if (strcmp(argv[i], "--gamedir") == 0 && (i + 1) < argc)
arg_string("--gamedir", argv[++i], gCLIOpts.GameDir);
arg_string("--gamedir", argv[++i], gCLIOpts.GameDir, SYS_MAX_PATH);
else if (strcmp(argv[i], "--savepath") == 0 && (i + 1) < argc)
arg_string("--savepath", argv[++i], gCLIOpts.SavePath);
arg_string("--savepath", argv[++i], gCLIOpts.SavePath, SYS_MAX_PATH);
// Print help
else if (strcmp(argv[i], "--help") == 0) {

View file

@ -9,10 +9,15 @@ enum NetworkType {
NT_CLIENT
};
#define IP_MAX_LEN 32
#define PORT_MAX_LEN 16
struct PCCLIOptions {
unsigned int SkipIntro;
unsigned int FullScreen;
enum NetworkType Network;
char JoinIp[IP_MAX_LEN];
char NetworkPort[PORT_MAX_LEN];
char ConfigFile[SYS_MAX_PATH];
char SavePath[SYS_MAX_PATH];
char GameDir[SYS_MAX_PATH];

View file

@ -6,11 +6,11 @@
enum NetworkType networkType;
static SOCKET gSocket;
static char* txIp = "127.0.0.1";
static unsigned short txPort;
struct sockaddr_in txAddr;
void network_init(enum NetworkType inNetworkType) {
void network_init(enum NetworkType inNetworkType, char* ip, char* port) {
networkType = inNetworkType;
if (networkType == NT_NONE) { return; }
// Create a receiver socket to receive datagrams
@ -18,12 +18,15 @@ void network_init(enum NetworkType inNetworkType) {
if (gSocket == INVALID_SOCKET) { return; }
// Bind the socket to any address and the specified port.
unsigned int port = (networkType == NT_SERVER) ? 27015 : 27016;
int rc = socket_bind(gSocket, port);
if (rc != NO_ERROR) { return; }
// Save the port to send to
txPort = (networkType == NT_SERVER) ? 27016 : 27015;
if (networkType == NT_SERVER) {
int rc = socket_bind(gSocket, atoi(port));
if (rc != NO_ERROR) { return; }
} else {
// Save the port to send to
txAddr.sin_family = AF_INET;
txAddr.sin_port = htons(atoi(port));
txAddr.sin_addr.s_addr = inet_addr(ip);
}
}
void network_send(struct Packet* p) {
@ -39,7 +42,7 @@ void network_send(struct Packet* p) {
memcpy(&p->buffer[p->dataLength], &hash, sizeof(u32));
// send
int rc = socket_send(gSocket, txIp, txPort, p->buffer, p->cursor + sizeof(u32));
int rc = socket_send(gSocket, &txAddr, p->buffer, p->cursor + sizeof(u32));
if (rc != NO_ERROR) { return; }
p->sent = true;
}
@ -57,7 +60,7 @@ void network_update(void) {
do {
// receive packet
struct Packet p = { .cursor = 3 };
int rc = socket_receive(gSocket, p.buffer, PACKET_LENGTH, &p.dataLength);
int rc = socket_receive(gSocket, &txAddr, p.buffer, PACKET_LENGTH, &p.dataLength);
if (rc != NO_ERROR) { break; }
// subtract and check hash

View file

@ -59,7 +59,7 @@ extern s16 sCurrPlayMode;
extern enum NetworkType networkType;
extern struct SyncObject syncObjects[];
void network_init(enum NetworkType networkType);
void network_init(enum NetworkType inNetworkType, char* ip, char* port);
void network_clear_sync_objects(void);
void network_init_object(struct Object *object, float maxSyncDistance);

View file

@ -15,13 +15,9 @@ int socket_bind(SOCKET sock, unsigned int port) {
return rc;
}
int socket_send(SOCKET sock, char* ip, unsigned int port, char* buffer, int bufferLength) {
struct sockaddr_in txAddr;
txAddr.sin_family = AF_INET;
txAddr.sin_port = htons(port);
txAddr.sin_addr.s_addr = inet_addr(ip);
int rc = sendto(sock, buffer, bufferLength, 0, (SOCKADDR*)&txAddr, sizeof(txAddr));
int socket_send(SOCKET sock, struct sockaddr_in* txAddr, char* buffer, int bufferLength) {
int txAddrSize = sizeof(struct sockaddr_in);
int rc = sendto(sock, buffer, bufferLength, 0, txAddr, txAddrSize);
if (rc == SOCKET_ERROR) {
printf("%s sendto failed with error: %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
}
@ -29,13 +25,11 @@ int socket_send(SOCKET sock, char* ip, unsigned int port, char* buffer, int buff
return rc;
}
int socket_receive(SOCKET sock, char* buffer, int bufferLength, int* receiveLength) {
int socket_receive(SOCKET sock, struct sockaddr_in* rxAddr, char* buffer, int bufferLength, int* receiveLength) {
*receiveLength = 0;
struct sockaddr_in rxAddr;
int rxAddrSize = sizeof(rxAddr);
int rc = recvfrom(sock, buffer, bufferLength, 0, (SOCKADDR*)&rxAddr, &rxAddrSize);
int rxAddrSize = sizeof(struct sockaddr_in);
int rc = recvfrom(sock, buffer, bufferLength, 0, rxAddr, &rxAddrSize);
if (rc == SOCKET_ERROR) {
int error = SOCKET_LAST_ERROR;
if (error != EWOULDBLOCK && error != ECONNRESET) {

View file

@ -9,8 +9,8 @@
SOCKET socket_initialize(void);
int socket_bind(SOCKET sock, unsigned int port);
int socket_send(SOCKET sock, char* ip, unsigned int port, char* buffer, int bufferLength);
int socket_receive(SOCKET sock, char* buffer, int bufferLength, int* receiveLength);
int socket_send(SOCKET sock, struct sockaddr_in* txAddr, char* buffer, int bufferLength);
int socket_receive(SOCKET sock, struct sockaddr_in* rxAddr, char* buffer, int bufferLength, int* receiveLength);
void socket_close(SOCKET sock);
#endif

View file

@ -257,7 +257,7 @@ void main_func(void) {
audio_api = &audio_null;
}
network_init(gCLIOpts.Network);
network_init(gCLIOpts.Network, gCLIOpts.JoinIp, gCLIOpts.NetworkPort);
audio_init();
sound_init();