2023-05-31 20:48:40 +01:00
|
|
|
// Client-Side Prediction Test - Client
|
|
|
|
// Barra Ó Catháin - 2023
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <stdbool.h>
|
2023-07-02 13:23:07 +01:00
|
|
|
#include <pthread.h>
|
2023-05-31 20:48:40 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2023-07-02 13:23:07 +01:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <SDL2/SDL_timer.h>
|
|
|
|
#include "../cspt-state.h"
|
2023-05-31 20:48:40 +01:00
|
|
|
#include "../cspt-message.h"
|
2023-07-13 02:05:07 +01:00
|
|
|
#define ENABLE_CLIENT_SIDE_PREDICTION
|
2023-08-03 00:15:44 +01:00
|
|
|
#define ENABLE_SERVER_RECONCILLIATION
|
2023-05-31 20:48:40 +01:00
|
|
|
|
2023-08-05 00:42:36 +01:00
|
|
|
const uint8_t colours[16][3] =
|
2023-07-13 00:09:54 +01:00
|
|
|
{
|
|
|
|
{255, 255, 255},
|
|
|
|
{100, 176, 254},
|
|
|
|
{147, 122, 254},
|
|
|
|
{199, 119, 254},
|
|
|
|
{243, 106, 254},
|
|
|
|
{254, 110, 205},
|
|
|
|
{254, 130, 112},
|
|
|
|
{235, 159, 35},
|
|
|
|
{189, 191, 0},
|
|
|
|
{137, 217, 0},
|
|
|
|
{93 , 229, 48},
|
|
|
|
{69 , 225, 130},
|
|
|
|
{72 , 206, 223}
|
|
|
|
};
|
2023-07-13 02:05:07 +01:00
|
|
|
|
2023-07-12 23:56:48 +01:00
|
|
|
// A structure for binding together the shared state between threads:
|
|
|
|
struct threadParameters
|
|
|
|
{
|
2023-07-13 01:02:29 +01:00
|
|
|
char * ipAddress;
|
|
|
|
bool * keepRunning;
|
2023-07-12 23:56:48 +01:00
|
|
|
struct gameState * state;
|
|
|
|
struct clientInput * message;
|
2023-08-03 00:15:44 +01:00
|
|
|
struct inputHistory * inputBuffer;
|
2023-07-12 23:56:48 +01:00
|
|
|
};
|
|
|
|
|
2023-07-02 13:23:07 +01:00
|
|
|
void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
|
|
|
|
{
|
2023-07-06 02:07:07 +01:00
|
|
|
const int32_t diameter = (radius * 2);
|
|
|
|
|
|
|
|
int32_t x = (radius - 1);
|
|
|
|
int32_t y = 0;
|
|
|
|
int32_t tx = 1;
|
|
|
|
int32_t ty = 1;
|
|
|
|
int32_t error = (tx - diameter);
|
|
|
|
|
|
|
|
while (x >= y)
|
|
|
|
{
|
|
|
|
// Each of the following renders an octant of the circle
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
|
|
|
|
SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);
|
|
|
|
|
|
|
|
if (error <= 0)
|
|
|
|
{
|
|
|
|
++y;
|
|
|
|
error += ty;
|
|
|
|
ty += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error > 0)
|
|
|
|
{
|
|
|
|
--x;
|
|
|
|
tx += 2;
|
|
|
|
error += (tx - diameter);
|
|
|
|
}
|
|
|
|
}
|
2023-07-02 13:23:07 +01:00
|
|
|
}
|
2023-07-06 21:28:32 +01:00
|
|
|
|
|
|
|
void * networkHandler(void * parameters)
|
|
|
|
{
|
2023-07-12 23:56:48 +01:00
|
|
|
// Declare the needed variables for the thread:
|
2023-07-06 21:28:32 +01:00
|
|
|
struct threadParameters * arguments = parameters;
|
|
|
|
struct sockaddr_in serverAddress;
|
2023-07-02 23:08:59 +01:00
|
|
|
int udpSocket = 0;
|
|
|
|
|
2023-07-06 21:28:32 +01:00
|
|
|
// Point at the server:
|
2023-07-06 00:22:59 +01:00
|
|
|
serverAddress.sin_family = AF_INET;
|
2023-07-12 23:56:48 +01:00
|
|
|
serverAddress.sin_addr.s_addr = inet_addr(arguments->ipAddress);
|
2023-07-06 00:22:59 +01:00
|
|
|
serverAddress.sin_port = htons(5200);
|
2023-07-02 23:08:59 +01:00
|
|
|
|
2023-07-06 21:28:32 +01:00
|
|
|
// Create a UDP socket to send through:
|
2023-07-02 23:08:59 +01:00
|
|
|
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
2023-07-06 21:28:32 +01:00
|
|
|
|
|
|
|
// Configure a timeout for recieving:
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 1000;
|
|
|
|
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
2023-07-25 23:53:40 +01:00
|
|
|
|
|
|
|
// Store the state we recieve from the network:
|
2023-07-12 01:12:18 +01:00
|
|
|
struct gameState * updatedState = calloc(1, sizeof(struct gameState));
|
2023-07-25 23:53:40 +01:00
|
|
|
|
2023-07-06 21:28:32 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Send our input, recieve the state:
|
2023-08-03 00:15:44 +01:00
|
|
|
sendto(udpSocket, arguments->message, sizeof(struct clientInput), 0,
|
|
|
|
(struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
2023-07-12 01:12:18 +01:00
|
|
|
recvfrom(udpSocket, updatedState, sizeof(struct gameState), 0, NULL, NULL);
|
2023-07-25 23:53:40 +01:00
|
|
|
|
|
|
|
// Only update the state if the given state is more recent than the current state:
|
2023-08-05 00:42:36 +01:00
|
|
|
if(updatedState->timestamp.tv_sec > arguments->state->timestamp.tv_sec ||
|
|
|
|
(updatedState->timestamp.tv_sec == arguments->state->timestamp.tv_sec &&
|
|
|
|
updatedState->timestamp.tv_usec > arguments->state->timestamp.tv_usec))
|
|
|
|
{
|
2023-08-03 00:15:44 +01:00
|
|
|
#ifdef ENABLE_SERVER_RECONCILLIATION
|
|
|
|
// Throw away any already acknowledged inputs:
|
|
|
|
while (arguments->inputBuffer->start != -1 &&
|
|
|
|
arguments->inputBuffer->inputs[arguments->inputBuffer->start].tickNumber < arguments->state->tickNumber)
|
|
|
|
{
|
|
|
|
arguments->inputBuffer->start = (arguments->inputBuffer->start + 1) % 256;
|
|
|
|
if(arguments->inputBuffer->start == arguments->inputBuffer->end)
|
|
|
|
{
|
|
|
|
arguments->inputBuffer->start = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t currentMessage = arguments->inputBuffer->start;
|
|
|
|
uint64_t lastTickNumber = arguments->inputBuffer->inputs[arguments->inputBuffer->start].tickNumber;
|
|
|
|
|
|
|
|
// Re-apply the currently unused messages:
|
|
|
|
while (currentMessage != 1 &&
|
|
|
|
currentMessage != arguments->inputBuffer->end)
|
|
|
|
{
|
|
|
|
updateInput(arguments->state, &arguments->inputBuffer->inputs[currentMessage]);
|
|
|
|
currentMessage = (currentMessage + 1) % 256;
|
|
|
|
if (arguments->inputBuffer->inputs[currentMessage].tickNumber != lastTickNumber)
|
|
|
|
{
|
|
|
|
doGameTick(arguments->state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2023-07-12 01:12:18 +01:00
|
|
|
}
|
2023-08-05 00:42:36 +01:00
|
|
|
lerpStates(arguments->state, updatedState);
|
2023-07-06 21:28:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 01:30:58 +01:00
|
|
|
void * gameThreadHandler(void * parameters)
|
2023-07-09 01:15:05 +01:00
|
|
|
{
|
2023-07-09 01:30:58 +01:00
|
|
|
struct threadParameters * arguments = parameters;
|
2023-08-05 00:42:36 +01:00
|
|
|
|
2023-07-25 23:53:40 +01:00
|
|
|
#ifdef ENABLE_CLIENT_SIDE_PREDICTION
|
2023-08-05 00:42:36 +01:00
|
|
|
struct gameState * nextStep = calloc(1, sizeof(struct gameState));
|
2023-07-09 01:15:05 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2023-07-09 01:30:58 +01:00
|
|
|
updateInput(arguments->state, arguments->message);
|
2023-08-03 00:15:44 +01:00
|
|
|
#ifdef ENABLE_SERVER_RECONCILLIATION
|
|
|
|
if(arguments->inputBuffer->start = -1)
|
|
|
|
{
|
|
|
|
memcpy(&arguments->inputBuffer->inputs[0], arguments->message, sizeof(struct clientInput));
|
|
|
|
arguments->inputBuffer->start = 0;
|
|
|
|
arguments->inputBuffer->end = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&arguments->inputBuffer->inputs[arguments->inputBuffer->end], arguments->message, sizeof(struct clientInput));
|
|
|
|
arguments->inputBuffer->end = (arguments->inputBuffer->end + 1) % 256;
|
|
|
|
}
|
|
|
|
#endif
|
2023-08-05 00:42:36 +01:00
|
|
|
memcpy(nextStep, arguments->state, sizeof(struct gameState));
|
|
|
|
doGameTick(nextStep);
|
|
|
|
lerpStates(arguments->state, nextStep);
|
2023-07-09 01:15:05 +01:00
|
|
|
usleep(15625);
|
|
|
|
}
|
2023-08-03 00:15:44 +01:00
|
|
|
#endif
|
2023-07-09 01:15:05 +01:00
|
|
|
}
|
|
|
|
|
2023-07-06 21:28:32 +01:00
|
|
|
void * graphicsThreadHandler(void * parameters)
|
|
|
|
{
|
2023-07-13 01:02:29 +01:00
|
|
|
bool * keepRunning = ((struct threadParameters *)parameters)->keepRunning;
|
2023-07-06 21:28:32 +01:00
|
|
|
struct gameState * state = ((struct threadParameters *)parameters)->state;
|
|
|
|
struct clientInput * message = ((struct threadParameters *)parameters)->message;
|
|
|
|
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
2023-07-02 13:23:07 +01:00
|
|
|
|
|
|
|
// Create an SDL window and rendering context in that window:
|
2023-07-13 01:02:29 +01:00
|
|
|
SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, 0);
|
2023-07-13 00:42:11 +01:00
|
|
|
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
|
2023-07-06 02:07:07 +01:00
|
|
|
SDL_Event event;
|
2023-07-13 00:42:11 +01:00
|
|
|
|
2023-07-02 13:23:07 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2023-07-06 21:28:32 +01:00
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
|
|
|
switch (event.type)
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-06 21:28:32 +01:00
|
|
|
case SDL_KEYDOWN:
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-12 23:56:48 +01:00
|
|
|
switch (event.key.keysym.sym)
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-06 21:28:32 +01:00
|
|
|
case SDLK_LEFT:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->left = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->right = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_UP:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->up = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_DOWN:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->down = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-07-06 21:28:32 +01:00
|
|
|
}
|
|
|
|
case SDL_KEYUP:
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-06 21:28:32 +01:00
|
|
|
switch (event.key.keysym.sym)
|
2023-07-06 02:07:07 +01:00
|
|
|
{
|
2023-07-06 21:28:32 +01:00
|
|
|
case SDLK_LEFT:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->left = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->right = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_UP:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->up = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_DOWN:
|
|
|
|
{
|
2023-07-25 23:32:11 +01:00
|
|
|
message->tickNumber = state->tickNumber;
|
2023-07-06 21:28:32 +01:00
|
|
|
message->down = false;
|
|
|
|
break;
|
|
|
|
}
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
2023-07-06 21:28:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-07-13 01:02:29 +01:00
|
|
|
case SDL_QUIT:
|
|
|
|
{
|
|
|
|
*keepRunning = false;
|
|
|
|
break;
|
|
|
|
}
|
2023-07-06 21:28:32 +01:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
2023-07-06 02:07:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-13 01:02:29 +01:00
|
|
|
|
2023-07-12 23:56:48 +01:00
|
|
|
// Clear the screen, filling it with black:
|
2023-07-02 13:23:07 +01:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
2023-07-12 23:56:48 +01:00
|
|
|
|
|
|
|
// Draw all the connected clients:
|
2023-07-02 13:23:07 +01:00
|
|
|
for (int index = 0; index < 16; index++)
|
|
|
|
{
|
|
|
|
if (state->clients[index].registered == true)
|
|
|
|
{
|
2023-07-25 23:53:40 +01:00
|
|
|
// Set the colour to the correct one for the client:
|
2023-07-13 00:09:54 +01:00
|
|
|
SDL_SetRenderDrawColor(renderer, colours[index][0], colours[index][1], colours[index][2], 255);
|
2023-07-25 23:53:40 +01:00
|
|
|
|
|
|
|
// Draw the circle:
|
2023-07-06 21:28:32 +01:00
|
|
|
DrawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 10);
|
2023-08-05 01:44:14 +01:00
|
|
|
|
|
|
|
// Draw an additional circle so we can tell ourselves apart from the rest:
|
|
|
|
if (index == message->clientNumber)
|
|
|
|
{
|
|
|
|
DrawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 5);
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:23:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present the rendered graphics:
|
|
|
|
SDL_RenderPresent(renderer);
|
2023-07-12 23:56:48 +01:00
|
|
|
|
|
|
|
// Delay enough so that we only hit 144 frames:
|
|
|
|
SDL_Delay(1000/144);
|
2023-07-02 13:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-31 20:48:40 +01:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
int serverSocket = 0;
|
2023-07-13 01:02:29 +01:00
|
|
|
bool keepRunning = true;
|
2023-07-02 13:23:07 +01:00
|
|
|
uint8_t currentPlayerNumber = 0;
|
2023-05-31 20:48:40 +01:00
|
|
|
struct sockaddr_in serverAddress;
|
2023-07-12 23:56:48 +01:00
|
|
|
struct CsptMessage currentMessage;
|
|
|
|
pthread_t graphicsThread, networkThread, gameThread;
|
|
|
|
struct gameState * currentState = calloc(1, sizeof(struct gameState));
|
|
|
|
struct clientInput * clientInput = calloc(1, sizeof(struct gameState));
|
|
|
|
|
|
|
|
// Say hello:
|
2023-05-31 20:48:40 +01:00
|
|
|
printf("Client-Side Prediction Test - Client Starting.\n");
|
|
|
|
|
|
|
|
// Give me a socket, and make sure it's working:
|
|
|
|
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (serverSocket == -1)
|
|
|
|
{
|
|
|
|
printf("Socket creation failed.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2023-07-12 23:56:48 +01:00
|
|
|
|
2023-05-31 20:48:40 +01:00
|
|
|
// Set our IP address and port. Default to localhost for testing:
|
2023-07-12 23:56:48 +01:00
|
|
|
char * ipAddress = calloc(46, sizeof(char));
|
2023-07-13 00:42:11 +01:00
|
|
|
if (argc < 2)
|
2023-07-12 23:56:48 +01:00
|
|
|
{
|
2023-07-13 00:42:11 +01:00
|
|
|
strncpy(ipAddress, "127.0.0.1", 10);
|
2023-07-12 23:56:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(ipAddress, argv[1], strlen(argv[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an address struct to point at the server:
|
2023-05-31 20:48:40 +01:00
|
|
|
serverAddress.sin_family = AF_INET;
|
2023-07-12 23:56:48 +01:00
|
|
|
serverAddress.sin_addr.s_addr = inet_addr(ipAddress);
|
2023-05-31 20:48:40 +01:00
|
|
|
serverAddress.sin_port = htons(5200);
|
|
|
|
|
|
|
|
// Connect to the server:
|
|
|
|
if (connect(serverSocket, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in)) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Connecting to the server failed.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
currentMessage.type = 0;
|
|
|
|
currentMessage.content = 0;
|
|
|
|
|
2023-07-02 13:23:07 +01:00
|
|
|
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
|
|
|
recv(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
2023-05-31 20:48:40 +01:00
|
|
|
|
|
|
|
if (currentMessage.type == 0)
|
|
|
|
{
|
|
|
|
currentPlayerNumber = currentMessage.content;
|
2023-07-06 21:28:32 +01:00
|
|
|
clientInput->clientNumber = currentPlayerNumber;
|
2023-05-31 20:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Registered as: %u\n", currentPlayerNumber);
|
2023-07-06 21:28:32 +01:00
|
|
|
|
2023-07-12 23:56:48 +01:00
|
|
|
// Configure the thread parameters:
|
2023-07-06 21:28:32 +01:00
|
|
|
struct threadParameters parameters;
|
|
|
|
parameters.state = currentState;
|
2023-07-13 01:02:29 +01:00
|
|
|
parameters.message = clientInput;
|
2023-07-12 23:56:48 +01:00
|
|
|
parameters.ipAddress = ipAddress;
|
2023-07-13 01:02:29 +01:00
|
|
|
parameters.keepRunning = &keepRunning;
|
2023-08-03 00:15:44 +01:00
|
|
|
parameters.inputBuffer = calloc(1, sizeof(struct inputHistory));
|
|
|
|
parameters.inputBuffer->start = -1;
|
|
|
|
parameters.inputBuffer->end = -1;
|
|
|
|
|
2023-07-12 23:56:48 +01:00
|
|
|
// Create all of our threads:
|
2023-07-09 01:30:58 +01:00
|
|
|
pthread_create(&gameThread, NULL, gameThreadHandler, ¶meters);
|
2023-07-06 21:28:32 +01:00
|
|
|
pthread_create(&networkThread, NULL, networkHandler, ¶meters);
|
2023-07-12 23:56:48 +01:00
|
|
|
pthread_create(&graphicsThread, NULL, graphicsThreadHandler, ¶meters);
|
2023-05-31 20:48:40 +01:00
|
|
|
|
2023-07-13 01:02:29 +01:00
|
|
|
while (keepRunning)
|
2023-05-31 20:48:40 +01:00
|
|
|
{
|
2023-07-02 13:23:07 +01:00
|
|
|
if (recv(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0) > 0)
|
2023-05-31 20:48:40 +01:00
|
|
|
{
|
|
|
|
switch (currentMessage.type)
|
|
|
|
{
|
2023-07-06 21:28:32 +01:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// We've been told to disconnect:
|
|
|
|
shutdown(serverSocket, SHUT_RDWR);
|
|
|
|
serverSocket = 0;
|
2023-07-13 01:02:29 +01:00
|
|
|
keepRunning = false;
|
2023-07-06 21:28:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
// Pinged, so we now must pong.
|
|
|
|
currentMessage.type = 3;
|
|
|
|
currentMessage.content = 0;
|
|
|
|
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-31 20:48:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-12 23:56:48 +01:00
|
|
|
// Say goodbye to the server:
|
2023-07-06 21:28:32 +01:00
|
|
|
currentMessage.type = 1;
|
|
|
|
currentMessage.content = 0;
|
2023-07-12 23:56:48 +01:00
|
|
|
|
|
|
|
// Send the goodbye message and shutdown:
|
2023-07-06 21:28:32 +01:00
|
|
|
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
2023-05-31 20:48:40 +01:00
|
|
|
shutdown(serverSocket, SHUT_RDWR);
|
|
|
|
serverSocket = 0;
|
2023-07-13 01:02:29 +01:00
|
|
|
keepRunning = false;
|
2023-05-31 20:48:40 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-13 01:02:29 +01:00
|
|
|
|
|
|
|
// Say goodbye to the server:
|
|
|
|
currentMessage.type = 1;
|
|
|
|
currentMessage.content = 0;
|
|
|
|
|
|
|
|
// Send the goodbye message and shutdown:
|
|
|
|
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
|
|
|
shutdown(serverSocket, SHUT_RDWR);
|
|
|
|
serverSocket = 0;
|
|
|
|
keepRunning = false;
|
2023-05-31 20:48:40 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|