Fixing and cleaning.

This commit is contained in:
Barra Ó Catháin 2023-08-06 01:30:33 +01:00
parent 5ca42a160e
commit d10ed76e7b
2 changed files with 109 additions and 76 deletions

View File

@ -1,5 +1,7 @@
// Client-Side Prediction Test - Client /* /======================================\
// Barra Ó Catháin - 2023 | Client-Side Prediction Test - Client |
| Barra Ó Catháin - 2023 |
\======================================/ */
#include <netdb.h> #include <netdb.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -17,10 +19,22 @@
#include <SDL2/SDL_timer.h> #include <SDL2/SDL_timer.h>
#include "../cspt-state.h" #include "../cspt-state.h"
#include "../cspt-message.h" #include "../cspt-message.h"
#define ENABLE_CLIENT_SIDE_PREDICTION #define ENABLE_CLIENT_SIDE_PREDICTION
#define ENABLE_SERVER_RECONCILLIATION #define ENABLE_SERVER_RECONCILLIATION
const uint8_t colours[16][3] = // A structure for binding together the shared state between threads:
struct ClientThreadParameters
{
char * ipAddress;
bool * keepRunning;
struct gameState * state;
struct clientInput * message;
struct inputHistory * inputBuffer;
};
// Seperate colours to distinguish each of the 16 possible clients:
static const uint8_t colours[16][3] =
{ {
{255, 255, 255}, {255, 255, 255},
{100, 176, 254}, {100, 176, 254},
@ -37,17 +51,8 @@ const uint8_t colours[16][3] =
{72 , 206, 223} {72 , 206, 223}
}; };
// A structure for binding together the shared state between threads: // Draws a circle based on the midpoint circle algorithm:
struct threadParameters void drawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{
char * ipAddress;
bool * keepRunning;
struct gameState * state;
struct clientInput * message;
struct inputHistory * inputBuffer;
};
void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{ {
const int32_t diameter = (radius * 2); const int32_t diameter = (radius * 2);
@ -55,7 +60,7 @@ void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32
int32_t y = 0; int32_t y = 0;
int32_t tx = 1; int32_t tx = 1;
int32_t ty = 1; int32_t ty = 1;
int32_t error = (tx - diameter); int32_t error = (tx - diameter);
while (x >= y) while (x >= y)
{ {
@ -84,110 +89,124 @@ void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32
} }
} }
} }
void * networkHandler(void * parameters) void * networkHandler(void * parameters)
{ {
// Declare the needed variables for the thread: // Unpack the variables passed to the thread:
struct threadParameters * arguments = parameters; char * ipAddress = ((struct ClientThreadParameters * )parameters)->ipAddress;
struct sockaddr_in serverAddress; bool * keepRunning = ((struct ClientThreadParameters * )parameters)->keepRunning;
int udpSocket = 0; struct gameState * state = ((struct ClientThreadParameters * )parameters)->state;
struct clientInput * message = ((struct ClientThreadParameters * )parameters)->message;
struct inputHistory * inputBuffer = ((struct ClientThreadParameters * )parameters)->inputBuffer;
// Point at the server: // Point at the server:
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET; serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(arguments->ipAddress); serverAddress.sin_addr.s_addr = inet_addr(ipAddress);
serverAddress.sin_port = htons(5200); serverAddress.sin_port = htons(5200);
// Create a UDP socket to send through: // Create a UDP socket to send through:
int udpSocket = 0;
udpSocket = socket(AF_INET, SOCK_DGRAM, 0); udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
// Configure a timeout for recieving: // Configure a timeout for receiving:
struct timeval timeout; struct timeval receiveTimeout;
timeout.tv_sec = 0; receiveTimeout.tv_sec = 0;
timeout.tv_usec = 1000; receiveTimeout.tv_usec = 1000;
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &receiveTimeout, sizeof(struct timeval));
// Store the state we recieve from the network: // A structure to store the most recent state from the network:
struct gameState * updatedState = calloc(1, sizeof(struct gameState)); struct gameState * updatedState = calloc(1, sizeof(struct gameState));
while (true) while (keepRunning)
{ {
// Send our input, recieve the state: // Send our input, recieve the state:
sendto(udpSocket, arguments->message, sizeof(struct clientInput), 0, sendto(udpSocket, message, sizeof(struct clientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
(struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
recvfrom(udpSocket, updatedState, sizeof(struct gameState), 0, NULL, NULL); recvfrom(udpSocket, updatedState, sizeof(struct gameState), 0, NULL, NULL);
// Only update the state if the given state is more recent than the current state: // Only update the state if the given state is more recent than the current state:
if(updatedState->timestamp.tv_sec > arguments->state->timestamp.tv_sec || if (updatedState->timestamp.tv_sec > state->timestamp.tv_sec ||
(updatedState->timestamp.tv_sec == arguments->state->timestamp.tv_sec && (updatedState->timestamp.tv_sec == state->timestamp.tv_sec &&
updatedState->timestamp.tv_usec > arguments->state->timestamp.tv_usec)) updatedState->timestamp.tv_usec > state->timestamp.tv_usec))
{ {
#ifdef ENABLE_SERVER_RECONCILLIATION #ifdef ENABLE_SERVER_RECONCILLIATION
// Throw away any already acknowledged inputs: // Throw away any already acknowledged inputs:
while (arguments->inputBuffer->start != -1 && while (inputBuffer->start != -1 && inputBuffer->inputs[inputBuffer->start].tickNumber < state->tickNumber)
arguments->inputBuffer->inputs[arguments->inputBuffer->start].tickNumber < arguments->state->tickNumber)
{ {
arguments->inputBuffer->start = (arguments->inputBuffer->start + 1) % 256; inputBuffer->start = (inputBuffer->start + 1) % 256;
if(arguments->inputBuffer->start == arguments->inputBuffer->end) if(inputBuffer->start == inputBuffer->end)
{ {
arguments->inputBuffer->start = -1; inputBuffer->start = -1;
} }
} }
uint8_t currentMessage = arguments->inputBuffer->start; uint8_t currentMessage = inputBuffer->start;
uint64_t lastTickNumber = arguments->inputBuffer->inputs[arguments->inputBuffer->start].tickNumber; uint64_t lastTickNumber = inputBuffer->inputs[inputBuffer->start].tickNumber;
// Re-apply the currently unused messages: // Re-apply the currently unused messages:
while (currentMessage != 1 && while (currentMessage != 1 && currentMessage != inputBuffer->end)
currentMessage != arguments->inputBuffer->end)
{ {
updateInput(arguments->state, &arguments->inputBuffer->inputs[currentMessage]); updateInput(state, &inputBuffer->inputs[currentMessage]);
currentMessage = (currentMessage + 1) % 256; currentMessage = (currentMessage + 1) % 256;
if (arguments->inputBuffer->inputs[currentMessage].tickNumber != lastTickNumber)
// When we get to the next tick in the inputs, apply a game tick:
if (inputBuffer->inputs[currentMessage].tickNumber != lastTickNumber)
{ {
doGameTick(arguments->state); doGameTick(state);
} }
} }
#endif #endif
// Interpolate to the new state:
lerpStates(state, updatedState);
} }
lerpStates(arguments->state, updatedState);
} }
return NULL;
} }
void * gameThreadHandler(void * parameters) void * gameThreadHandler(void * parameters)
{ {
struct threadParameters * arguments = parameters; // Unpack the variables passed to the thread:
bool * keepRunning = ((struct ClientThreadParameters * )parameters)->keepRunning;
struct gameState * state = ((struct ClientThreadParameters * )parameters)->state;
struct clientInput * message = ((struct ClientThreadParameters * )parameters)->message;
struct inputHistory * inputBuffer = ((struct ClientThreadParameters * )parameters)->inputBuffer;
#ifdef ENABLE_CLIENT_SIDE_PREDICTION #ifdef ENABLE_CLIENT_SIDE_PREDICTION
struct gameState * nextStep = calloc(1, sizeof(struct gameState)); struct gameState * nextStep = calloc(1, sizeof(struct gameState));
while (true) while (keepRunning)
{ {
updateInput(arguments->state, arguments->message); updateInput(state, message);
#ifdef ENABLE_SERVER_RECONCILLIATION #ifdef ENABLE_SERVER_RECONCILLIATION
if(arguments->inputBuffer->start = -1) if(inputBuffer->start == -1)
{ {
memcpy(&arguments->inputBuffer->inputs[0], arguments->message, sizeof(struct clientInput)); memcpy(&inputBuffer->inputs[0], message, sizeof(struct clientInput));
arguments->inputBuffer->start = 0; inputBuffer->start = 0;
arguments->inputBuffer->end = 1; inputBuffer->end = 1;
} }
else else
{ {
memcpy(&arguments->inputBuffer->inputs[arguments->inputBuffer->end], arguments->message, sizeof(struct clientInput)); memcpy(&inputBuffer->inputs[inputBuffer->end], message, sizeof(struct clientInput));
arguments->inputBuffer->end = (arguments->inputBuffer->end + 1) % 256; inputBuffer->end = (inputBuffer->end + 1) % 256;
} }
#endif #endif
memcpy(nextStep, arguments->state, sizeof(struct gameState));
memcpy(nextStep, state, sizeof(struct gameState));
doGameTick(nextStep); doGameTick(nextStep);
lerpStates(arguments->state, nextStep); lerpStates(state, nextStep);
usleep(15625); usleep(15625);
} }
#endif #endif
return NULL;
} }
void * graphicsThreadHandler(void * parameters) void * graphicsThreadHandler(void * parameters)
{ {
bool * keepRunning = ((struct threadParameters *)parameters)->keepRunning; bool * keepRunning = ((struct ClientThreadParameters *)parameters)->keepRunning;
struct gameState * state = ((struct threadParameters *)parameters)->state; struct gameState * state = ((struct ClientThreadParameters *)parameters)->state;
struct clientInput * message = ((struct threadParameters *)parameters)->message; struct clientInput * message = ((struct ClientThreadParameters *)parameters)->message;
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
// Create an SDL window and rendering context in that window: // Create an SDL window and rendering context in that window:
@ -195,7 +214,7 @@ void * graphicsThreadHandler(void * parameters)
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags); SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
SDL_Event event; SDL_Event event;
while (true) while (keepRunning)
{ {
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
@ -292,14 +311,13 @@ void * graphicsThreadHandler(void * parameters)
SDL_SetRenderDrawColor(renderer, colours[index][0], colours[index][1], colours[index][2], 255); SDL_SetRenderDrawColor(renderer, colours[index][0], colours[index][1], colours[index][2], 255);
// Draw the circle: // Draw the circle:
DrawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 10); drawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 10);
// Draw an additional circle so we can tell ourselves apart from the rest: // Draw an additional circle so we can tell ourselves apart from the rest:
if (index == message->clientNumber) if (index == message->clientNumber)
{ {
DrawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 5); drawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 5);
} }
} }
} }
@ -319,14 +337,22 @@ int main(int argc, char ** argv)
bool keepRunning = true; bool keepRunning = true;
uint8_t currentPlayerNumber = 0; uint8_t currentPlayerNumber = 0;
struct sockaddr_in serverAddress; struct sockaddr_in serverAddress;
struct CsptMessage currentMessage; struct CsptMessage currentMessage;
pthread_t graphicsThread, networkThread, gameThread;
struct gameState * currentState = calloc(1, sizeof(struct gameState)); struct gameState * currentState = calloc(1, sizeof(struct gameState));
struct clientInput * clientInput = calloc(1, sizeof(struct gameState)); struct clientInput * clientInput = calloc(1, sizeof(struct gameState));
// Say hello: // Print a welcome message:
printf("Client-Side Prediction Test - Client Starting.\n"); printf("Client-Side Prediction Test - Client Starting.\n");
printf("==============================================\n");
// Print a list of enabled features:
#ifdef ENABLE_CLIENT_SIDE_PREDICTION
printf("Client-side prediction is enabled in this build.\n");
#endif
#ifdef ENABLE_SERVER_RECONCILLIATION
printf("Server reconcilliation is enabled in this build.\n");
#endif
// Give me a socket, and make sure it's working: // Give me a socket, and make sure it's working:
serverSocket = socket(AF_INET, SOCK_STREAM, 0); serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) if (serverSocket == -1)
@ -370,10 +396,10 @@ int main(int argc, char ** argv)
clientInput->clientNumber = currentPlayerNumber; clientInput->clientNumber = currentPlayerNumber;
} }
printf("Registered as: %u\n", currentPlayerNumber); printf("Joined server as client: %u.\n", currentPlayerNumber);
// Configure the thread parameters: // Configure the thread parameters:
struct threadParameters parameters; struct ClientThreadParameters parameters;
parameters.state = currentState; parameters.state = currentState;
parameters.message = clientInput; parameters.message = clientInput;
parameters.ipAddress = ipAddress; parameters.ipAddress = ipAddress;
@ -383,6 +409,7 @@ int main(int argc, char ** argv)
parameters.inputBuffer->end = -1; parameters.inputBuffer->end = -1;
// Create all of our threads: // Create all of our threads:
pthread_t graphicsThread, networkThread, gameThread;
pthread_create(&gameThread, NULL, gameThreadHandler, &parameters); pthread_create(&gameThread, NULL, gameThreadHandler, &parameters);
pthread_create(&networkThread, NULL, networkHandler, &parameters); pthread_create(&networkThread, NULL, networkHandler, &parameters);
pthread_create(&graphicsThread, NULL, graphicsThreadHandler, &parameters); pthread_create(&graphicsThread, NULL, graphicsThreadHandler, &parameters);
@ -393,31 +420,37 @@ int main(int argc, char ** argv)
{ {
switch (currentMessage.type) switch (currentMessage.type)
{ {
// Recieved a "GOODBYE" message:
case 1: case 1:
{ {
// We've been told to disconnect: // Close the socket, and stop the client:
shutdown(serverSocket, SHUT_RDWR); shutdown(serverSocket, SHUT_RDWR);
serverSocket = 0; serverSocket = 0;
keepRunning = false; keepRunning = false;
break; break;
} }
// Recieved a "PING" message:
case 2: case 2:
{ {
// Pinged, so we now must pong. // Setup and send a "PONG" message:
currentMessage.type = 3; currentMessage.type = 3;
currentMessage.content = 0; currentMessage.content = 0;
send(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0); send(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0);
break; break;
} }
} }
} }
// If we've lost connection for some reason:
else else
{ {
// Say goodbye to the server: // Setup a "GOODBYE" message:
currentMessage.type = 1; currentMessage.type = 1;
currentMessage.content = 0; currentMessage.content = 0;
// Send the goodbye message and shutdown: // Send the "GOODBYE" message and shutdown the socket:
send(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0); send(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0);
shutdown(serverSocket, SHUT_RDWR); shutdown(serverSocket, SHUT_RDWR);
serverSocket = 0; serverSocket = 0;

View File

@ -33,7 +33,7 @@ struct networkThreadArguments
struct inputHistory struct inputHistory
{ {
uint8_t start, end; int16_t start, end;
struct clientInput inputs[256]; struct clientInput inputs[256];
}; };