Skip to content

Commit

Permalink
Make the timeouts configurable with net scores.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremysrand committed Jun 12, 2023
1 parent 6afbf4e commit 59dc11e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
8 changes: 8 additions & 0 deletions BuGS/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
/* Defines and macros */

#define GLOBAL_SCORE_REFRESH_TIME (15 * 60 * 60)
#define SHUTDOWN_NETWORK_TIMEOUT (2 * 60)
#define TCP_CONNECT_TIMEOUT (8 * 60)
#define READ_NETWORK_TIMEOUT (5 * 60)
#define NETWORK_RETRY_TIMEOUT (3 * 60 * 60)

#define TOOLFAIL(string) \
if (toolerror()) SysFailMgr(toolerror(), (Pointer)"\p" string "\n\r Error Code -> $");
Expand Down Expand Up @@ -241,6 +245,10 @@ int main(void)
highScoreInitParams.scorePort = NETWORK_SERVERPORT;
highScoreInitParams.secret1 = NETWORK_SERVERSECRET1;
highScoreInitParams.secret2 = NETWORK_SERVERSECRET2;
highScoreInitParams.shutdownTimeout = SHUTDOWN_NETWORK_TIMEOUT;
highScoreInitParams.connectTimeout = TCP_CONNECT_TIMEOUT;
highScoreInitParams.readTimeout = READ_NETWORK_TIMEOUT;
highScoreInitParams.retryTimeout = NETWORK_RETRY_TIMEOUT;

highScoreInitParams.displayConnectionString = showConnectionString;
highScoreInitParams.waitForVbl = waitForVbl;
Expand Down
35 changes: 22 additions & 13 deletions BuGS/netScores.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#define RESPONSE_TYPE_SCORES 1
#define RESPONSE_TYPE_STATUS 2

#define SHUTDOWN_NETWORK_TIMEOUT (2 * 60)
#define TCP_CONNECT_TIMEOUT (8 * 60)
#define READ_NETWORK_TIMEOUT (5 * 60)
#define NETWORK_RETRY_TIMEOUT (3 * 60 * 60)
#define DEFAULT_SHUTDOWN_NETWORK_TIMEOUT (2 * 60)
#define DEFAULT_TCP_CONNECT_TIMEOUT (8 * 60)
#define DEFAULT_READ_NETWORK_TIMEOUT (5 * 60)
#define DEFAULT_NETWORK_RETRY_TIMEOUT (3 * 60 * 60)


// Types
Expand Down Expand Up @@ -252,6 +252,15 @@ void NSGS_InitNetwork(tNSGSHighScoreInitParams * params)

memcpy(&(networkGlobals->initParams), params, sizeof(networkGlobals->initParams));

if (networkGlobals->initParams.shutdownTimeout == 0)
networkGlobals->initParams.shutdownTimeout = DEFAULT_SHUTDOWN_NETWORK_TIMEOUT;
if (networkGlobals->initParams.connectTimeout == 0)
networkGlobals->initParams.connectTimeout = DEFAULT_TCP_CONNECT_TIMEOUT;
if (networkGlobals->initParams.readTimeout == 0)
networkGlobals->initParams.readTimeout = DEFAULT_READ_NETWORK_TIMEOUT;
if (networkGlobals->initParams.retryTimeout == 0)
networkGlobals->initParams.retryTimeout = DEFAULT_NETWORK_RETRY_TIMEOUT;

networkGlobals->networkStartedConnected = TCPIPGetConnectStatus();
if (networkGlobals->networkStartedConnected) {
networkGlobals->gameNetworkState = GAME_NETWORK_CONNECTED;
Expand Down Expand Up @@ -301,7 +310,7 @@ void NSGS_DisconnectNetwork(void)
if (networkGlobals->gameNetworkState < GAME_NETWORK_CLOSING_TCP) {
TCPIPCloseTCP(networkGlobals->ipid);
networkGlobals->gameNetworkState = GAME_NETWORK_CLOSING_TCP;
networkGlobals->timeout = SHUTDOWN_NETWORK_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.shutdownTimeout;
}

}
Expand Down Expand Up @@ -373,7 +382,7 @@ static void handleSocketError(void)
if (networkGlobals->initParams.displayError != NULL)
networkGlobals->initParams.displayError(NSGS_SOCKET_ERROR, networkGlobals->errorCode);
networkGlobals->gameNetworkState = GAME_NETWORK_FAILURE;
networkGlobals->timeout = NETWORK_RETRY_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.retryTimeout;
}

static void handleProtocolFailed(void)
Expand All @@ -382,7 +391,7 @@ static void handleProtocolFailed(void)
if (networkGlobals->initParams.displayError != NULL)
networkGlobals->initParams.displayError(NSGS_PROTOCOL_ERROR, networkGlobals->errorCode);
networkGlobals->gameNetworkState = GAME_NETWORK_FAILURE;
networkGlobals->timeout = NETWORK_RETRY_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.retryTimeout;
}

static void handleFailure(void)
Expand Down Expand Up @@ -421,7 +430,7 @@ static void handleTcpUnconnected(void)
return;
}
networkGlobals->gameNetworkState = GAME_NETWORK_WAITING_FOR_TCP;
networkGlobals->timeout = TCP_CONNECT_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.connectTimeout;
}

static void handleWaitingForTcp(void)
Expand Down Expand Up @@ -451,7 +460,7 @@ static void handleWaitingForTcp(void)
}

networkGlobals->gameNetworkState = GAME_NETWORK_WAITING_FOR_HELLO;
networkGlobals->timeout = READ_NETWORK_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.readTimeout;
networkGlobals->bytesRead = 0;
}

Expand Down Expand Up @@ -500,7 +509,7 @@ static void handleWaitingForHello(void)
} else {
TCPIPCloseTCP(networkGlobals->ipid);
networkGlobals->gameNetworkState = GAME_NETWORK_CLOSING_TCP;
networkGlobals->timeout = SHUTDOWN_NETWORK_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.shutdownTimeout;
}
}

Expand All @@ -521,7 +530,7 @@ static void handleRequestScores(void)
}

networkGlobals->gameNetworkState = GAME_NETWORK_WAITING_FOR_SCORES;
networkGlobals->timeout = READ_NETWORK_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.readTimeout;
networkGlobals->bytesRead = 0;
}

Expand Down Expand Up @@ -566,7 +575,7 @@ static void handleWaitingForScores(void)

TCPIPCloseTCP(networkGlobals->ipid);
networkGlobals->gameNetworkState = GAME_NETWORK_CLOSING_TCP;
networkGlobals->timeout = SHUTDOWN_NETWORK_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.shutdownTimeout;
}

static void handleSetHighScore(void)
Expand All @@ -587,7 +596,7 @@ static void handleSetHighScore(void)
}

networkGlobals->gameNetworkState = GAME_NETWORK_WAITING_FOR_SCORE_ACK;
networkGlobals->timeout = READ_NETWORK_TIMEOUT;
networkGlobals->timeout = networkGlobals->initParams.readTimeout;
networkGlobals->bytesRead = 0;
}

Expand Down
20 changes: 20 additions & 0 deletions BuGS/netScores.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ typedef struct tNSGSHighScoreInitParams
unsigned long secret1;
unsigned long secret2;

/* Each of these timeouts are measured in poll periods. So, if you call NSGS_PollNetwork() every 60th of a
second, then you would use 60 to set the timeout to 1 second. If these are zero, then the "good value"
in the comment will be used as a default.
The shutdown timeout controls how long we wait for a clean TCP disconnection before forcing an abort of
the connection. Two seconds is a good value for this timeout. */
unsigned int shutdownTimeout;

/* The connect timeout is the amount of time we wait for a TCP connection to come up before declaring a
timeout protocol error. Eight seconds is a good value for this timeout. */
unsigned int connectTimeout;

/* The read timeout is the amount of time we wait for a response from the server after we have made a
request of it, whether that is getting the high score list or setting a new high score. Five seconds
is a good value for this timeout. */
unsigned int readTimeout;

/* The retry timeout is the amount of time we wait in an error state before retrying. This only happens
for "soft" errors where a retry is worthwhile. Three minutes is a good value for this timeout. */
unsigned int retryTimeout;

/* This function should display a message to the user that the network is being brought up and they should
be patient when the argument is TRUE and when the argument is FALSE, it should clear that message. This
Expand Down

0 comments on commit 59dc11e

Please sign in to comment.