UDP data going back and forth

This commit is contained in:
Barra Ó Catháin 2023-07-06 00:22:59 +01:00
parent 56c1332675
commit ff01152aa1
4 changed files with 51 additions and 62 deletions

View File

@ -61,12 +61,14 @@ void * graphicsThreadHandler(void * parameters)
struct gameState * state = (struct gameState *)parameters; struct gameState * state = (struct gameState *)parameters;
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
int udpSocket = 0; int udpSocket = 0;
struct sockaddr_in recieveAddress; struct sockaddr_in recieveAddress, serverAddress;
struct clientInput message;
message.right = true;
message.clientNumber = 1;
// Set our IP address and port. Default to localhost for testing: serverAddress.sin_family = AF_INET;
recieveAddress.sin_family = AF_INET; serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
recieveAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_port = htons(5200);
recieveAddress.sin_port = htons(5200);
udpSocket = socket(AF_INET, SOCK_DGRAM, 0); udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
@ -77,14 +79,18 @@ void * graphicsThreadHandler(void * parameters)
// Enable resizing the window: // Enable resizing the window:
SDL_SetWindowResizable(window, SDL_TRUE); SDL_SetWindowResizable(window, SDL_TRUE);
state->clients[0].xPosition = 300; // state->clients[0].xPosition = 300;
state->clients[0].yPosition = 300; // state->clients[0].yPosition = 300;
bind(udpSocket, (struct sockaddr *)&recieveAddress, sizeof(struct sockaddr)); struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv));
while (true) while (true)
{ {
recvfrom(udpSocket, state, sizeof(struct gameState), 0, NULL, NULL); sendto(udpSocket, &message, sizeof(struct clientInput), MSG_CONFIRM, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
recvfrom(udpSocket, state, sizeof(struct gameState), MSG_WAITALL, NULL, NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Clear the screen, filling it with black: // Clear the screen, filling it with black:
@ -96,14 +102,13 @@ void * graphicsThreadHandler(void * parameters)
{ {
if (state->clients[index].registered == true) if (state->clients[index].registered == true)
{ {
DrawCircle(renderer, (int)state->clients[index].xPosition, (int)state->clients[index].yPosition, DrawCircle(renderer, (int)state->clients[index].yPosition, (int)state->clients[index].xPosition,
10); 10);
} }
} }
// Present the rendered graphics: // Present the rendered graphics:
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_Delay(1000/60);
} }
return NULL; return NULL;
@ -115,13 +120,11 @@ int main(int argc, char ** argv)
pthread_t graphicsThread; pthread_t graphicsThread;
struct CsptMessage currentMessage; struct CsptMessage currentMessage;
bool continueRunning = true; bool continueRunning = true;
struct gameState currentState; struct gameState * currentState = calloc(1, sizeof(struct gameState));
uint8_t currentPlayerNumber = 0; uint8_t currentPlayerNumber = 0;
struct sockaddr_in serverAddress; struct sockaddr_in serverAddress;
printf("Client-Side Prediction Test - Client Starting.\n"); printf("Client-Side Prediction Test - Client Starting.\n");
bzero(&currentState, sizeof(struct gameState));
// 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)
@ -156,7 +159,7 @@ int main(int argc, char ** argv)
printf("Registered as: %u\n", currentPlayerNumber); printf("Registered as: %u\n", currentPlayerNumber);
printf("%-7s | %u\n", messageStrings[currentMessage.type], currentMessage.content); printf("%-7s | %u\n", messageStrings[currentMessage.type], currentMessage.content);
pthread_create(&graphicsThread, NULL, graphicsThreadHandler, &currentState); pthread_create(&graphicsThread, NULL, graphicsThreadHandler, currentState);
while (continueRunning) while (continueRunning)
{ {
if (recv(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0) > 0) if (recv(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0) > 0)

View File

@ -1,20 +1,14 @@
#include "cspt-state.h" #include "cspt-state.h"
void updateInput(struct gameState * state, struct clientInput * message, struct sockaddr_in * address, int * clientSockets) void updateInput(struct gameState * state, struct clientInput * message)
{ {
int index = 0; if(message->clientNumber < 16 && message->clientNumber >= 0)
struct sockaddr_in currentClientAddress;
for (index = 0; index < 16; index++)
{ {
getsockname(clientSockets[index], (struct sockaddr *)&currentClientAddress, (socklen_t *)sizeof(struct sockaddr_in)); state->clients[message->clientNumber].leftAcceleration = message->left;
if (currentClientAddress.sin_addr.s_addr == address->sin_addr.s_addr) state->clients[message->clientNumber].rightAcceleration = message->right;
{ state->clients[message->clientNumber].upAcceleration = message->up;
state->clients[index].leftAcceleration = message->left; state->clients[message->clientNumber].downAcceleration = message->down;
state->clients[index].rightAcceleration = message->right;
state->clients[index].upAcceleration = message->up;
state->clients[index].downAcceleration = message->down;
break;
}
} }
} }
void doGameTick(struct gameState * state) void doGameTick(struct gameState * state)
@ -27,19 +21,19 @@ void doGameTick(struct gameState * state)
// Calculate acceleration: // Calculate acceleration:
if (state->clients[index].leftAcceleration) if (state->clients[index].leftAcceleration)
{ {
state->clients[index].xVelocity -= 0.1; state->clients[index].xVelocity -= 0.5;
} }
if (state->clients[index].rightAcceleration) if (state->clients[index].rightAcceleration)
{ {
state->clients[index].xVelocity += 0.1; state->clients[index].xVelocity += 0.5;
} }
if (state->clients[index].upAcceleration) if (state->clients[index].upAcceleration)
{ {
state->clients[index].yVelocity += 0.1; state->clients[index].yVelocity += 0.5;
} }
if (state->clients[index].downAcceleration) if (state->clients[index].downAcceleration)
{ {
state->clients[index].yVelocity += 0.1; state->clients[index].yVelocity += 0.5;
} }
if (!state->clients[index].leftAcceleration && !state->clients[index].rightAcceleration) if (!state->clients[index].leftAcceleration && !state->clients[index].rightAcceleration)
{ {
@ -51,21 +45,21 @@ void doGameTick(struct gameState * state)
} }
// Clamp speed: // Clamp speed:
if (state->clients[index].xVelocity > 10) if (state->clients[index].xVelocity > 15)
{ {
state->clients[index].xVelocity = 10; state->clients[index].xVelocity = 15;
} }
if (state->clients[index].xVelocity < -10) if (state->clients[index].xVelocity < -15)
{ {
state->clients[index].xVelocity = -10; state->clients[index].xVelocity = -15;
} }
if (state->clients[index].yVelocity > 10) if (state->clients[index].yVelocity > 15)
{ {
state->clients[index].yVelocity = 10; state->clients[index].yVelocity = 15;
} }
if (state->clients[index].yVelocity < -10) if (state->clients[index].yVelocity < -15)
{ {
state->clients[index].yVelocity = -10; state->clients[index].yVelocity = -15;
} }
// Do movement: // Do movement:

View File

@ -11,6 +11,7 @@ struct clientMovement
struct clientInput struct clientInput
{ {
int clientNumber;
bool left, right, up, down; bool left, right, up, down;
}; };
@ -26,7 +27,7 @@ struct networkThreadArguments
struct gameState * state; struct gameState * state;
}; };
void updateInput(struct gameState * state, struct clientInput * message, struct sockaddr_in * address, int * clientSockets); void updateInput(struct gameState * state, struct clientInput * message);
void doGameTick(struct gameState * state); void doGameTick(struct gameState * state);

View File

@ -31,7 +31,6 @@ void sigintHandler(int signal)
void * networkThreadHandler(void * arguments) void * networkThreadHandler(void * arguments)
{ {
struct networkThreadArguments * args = (struct networkThreadArguments *)arguments;
int udpSocket; int udpSocket;
pthread_t networkThread; pthread_t networkThread;
struct clientInput message; struct clientInput message;
@ -45,26 +44,21 @@ void * networkThreadHandler(void * arguments)
memset(&serverAddress, 0, sizeof(serverAddress)); memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET; serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT); serverAddress.sin_port = htons(5200);
serverAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_addr.s_addr = INADDR_ANY;
// bind(udpSocket, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in)); bind(udpSocket, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
printf("Started network thread.\n"); printf("Started network thread.\n");
socklen_t test = sizeof(clientAddress);
while (true) while (true)
{ {
// bzero(&message, sizeof(struct clientInput)); recvfrom(udpSocket, &message, sizeof(struct clientInput), MSG_WAITALL, (struct sockaddr *)&clientAddress, &test);
// recvfrom(udpSocket, &message, sizeof(struct clientInput), 0, (struct sockaddr *)&clientAddress, &clientAddressLength); sendto(udpSocket, arguments, sizeof(struct gameState), MSG_CONFIRM,
// updateInput(args->state, &message, &clientAddress, args->clientSockets); (struct sockaddr *)&clientAddress, (socklen_t)sizeof(struct sockaddr_in));
for (int index = 0; index < 16; index++) updateInput(arguments, &message);
{ doGameTick(arguments);
if(args->clientSockets[index] > 0) bzero(&message, sizeof(struct clientInput));
{
// getsockname(args->clientSockets[index], (struct sockaddr *)&clientAddress, (socklen_t *)sizeof(struct sockaddr_in));
sendto(udpSocket, args->state, sizeof(struct gameState), 0, (struct sockaddr *)&serverAddress, (socklen_t)sizeof(struct sockaddr_in));
}
}
} }
return NULL; return NULL;
@ -88,10 +82,7 @@ int main(int argc, char ** argv)
// Setup the sigint handler: // Setup the sigint handler:
signal(SIGINT, sigintHandler); signal(SIGINT, sigintHandler);
networkArguments.clientSockets = clientSockets; pthread_create(&networkThread, NULL, networkThreadHandler, &currentState);
networkArguments.state = &currentState;
pthread_create(&networkThread, NULL, networkThreadHandler, (void *)&networkArguments);
// Setup TCP Master Socket: // Setup TCP Master Socket:
printf("Setting up master socket... "); printf("Setting up master socket... ");