diff --git a/src/client/cspt-client.c b/src/client/cspt-client.c index aabe3b7..128b1f2 100644 --- a/src/client/cspt-client.c +++ b/src/client/cspt-client.c @@ -61,12 +61,14 @@ void * graphicsThreadHandler(void * parameters) struct gameState * state = (struct gameState *)parameters; uint32_t rendererFlags = SDL_RENDERER_ACCELERATED; 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: - recieveAddress.sin_family = AF_INET; - recieveAddress.sin_addr.s_addr = INADDR_ANY; - recieveAddress.sin_port = htons(5200); + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); + serverAddress.sin_port = htons(5200); udpSocket = socket(AF_INET, SOCK_DGRAM, 0); @@ -77,15 +79,19 @@ void * graphicsThreadHandler(void * parameters) // Enable resizing the window: SDL_SetWindowResizable(window, SDL_TRUE); - state->clients[0].xPosition = 300; - state->clients[0].yPosition = 300; - - bind(udpSocket, (struct sockaddr *)&recieveAddress, sizeof(struct sockaddr)); +// state->clients[0].xPosition = 300; +// state->clients[0].yPosition = 300; + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 100000; + setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)); + 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); // Clear the screen, filling it with black: SDL_RenderClear(renderer); @@ -96,14 +102,13 @@ void * graphicsThreadHandler(void * parameters) { 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); } } // Present the rendered graphics: SDL_RenderPresent(renderer); - SDL_Delay(1000/60); } return NULL; @@ -115,13 +120,11 @@ int main(int argc, char ** argv) pthread_t graphicsThread; struct CsptMessage currentMessage; bool continueRunning = true; - struct gameState currentState; + struct gameState * currentState = calloc(1, sizeof(struct gameState)); uint8_t currentPlayerNumber = 0; struct sockaddr_in serverAddress; printf("Client-Side Prediction Test - Client Starting.\n"); - bzero(¤tState, sizeof(struct gameState)); - // Give me a socket, and make sure it's working: serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (serverSocket == -1) @@ -156,7 +159,7 @@ int main(int argc, char ** argv) printf("Registered as: %u\n", currentPlayerNumber); printf("%-7s | %u\n", messageStrings[currentMessage.type], currentMessage.content); - pthread_create(&graphicsThread, NULL, graphicsThreadHandler, ¤tState); + pthread_create(&graphicsThread, NULL, graphicsThreadHandler, currentState); while (continueRunning) { if (recv(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0) > 0) diff --git a/src/cspt-state.c b/src/cspt-state.c index c184641..8e0558d 100644 --- a/src/cspt-state.c +++ b/src/cspt-state.c @@ -1,20 +1,14 @@ #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; - struct sockaddr_in currentClientAddress; - for (index = 0; index < 16; index++) + if(message->clientNumber < 16 && message->clientNumber >= 0) { - getsockname(clientSockets[index], (struct sockaddr *)¤tClientAddress, (socklen_t *)sizeof(struct sockaddr_in)); - if (currentClientAddress.sin_addr.s_addr == address->sin_addr.s_addr) - { - state->clients[index].leftAcceleration = message->left; - state->clients[index].rightAcceleration = message->right; - state->clients[index].upAcceleration = message->up; - state->clients[index].downAcceleration = message->down; - break; - } + state->clients[message->clientNumber].leftAcceleration = message->left; + state->clients[message->clientNumber].rightAcceleration = message->right; + state->clients[message->clientNumber].upAcceleration = message->up; + state->clients[message->clientNumber].downAcceleration = message->down; } + } void doGameTick(struct gameState * state) @@ -27,19 +21,19 @@ void doGameTick(struct gameState * state) // Calculate acceleration: if (state->clients[index].leftAcceleration) { - state->clients[index].xVelocity -= 0.1; + state->clients[index].xVelocity -= 0.5; } if (state->clients[index].rightAcceleration) { - state->clients[index].xVelocity += 0.1; + state->clients[index].xVelocity += 0.5; } if (state->clients[index].upAcceleration) { - state->clients[index].yVelocity += 0.1; + state->clients[index].yVelocity += 0.5; } 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) { @@ -51,21 +45,21 @@ void doGameTick(struct gameState * state) } // 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: diff --git a/src/cspt-state.h b/src/cspt-state.h index 0698e91..fa237c5 100644 --- a/src/cspt-state.h +++ b/src/cspt-state.h @@ -11,6 +11,7 @@ struct clientMovement struct clientInput { + int clientNumber; bool left, right, up, down; }; @@ -26,7 +27,7 @@ struct networkThreadArguments 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); diff --git a/src/server/cspt-server.c b/src/server/cspt-server.c index ed04a7d..7e61478 100644 --- a/src/server/cspt-server.c +++ b/src/server/cspt-server.c @@ -31,7 +31,6 @@ void sigintHandler(int signal) void * networkThreadHandler(void * arguments) { - struct networkThreadArguments * args = (struct networkThreadArguments *)arguments; int udpSocket; pthread_t networkThread; struct clientInput message; @@ -45,26 +44,21 @@ void * networkThreadHandler(void * arguments) memset(&serverAddress, 0, sizeof(serverAddress)); serverAddress.sin_family = AF_INET; - serverAddress.sin_port = htons(PORT); + serverAddress.sin_port = htons(5200); 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"); - + socklen_t test = sizeof(clientAddress); while (true) { -// bzero(&message, sizeof(struct clientInput)); -// recvfrom(udpSocket, &message, sizeof(struct clientInput), 0, (struct sockaddr *)&clientAddress, &clientAddressLength); -// updateInput(args->state, &message, &clientAddress, args->clientSockets); - for (int index = 0; index < 16; index++) - { - if(args->clientSockets[index] > 0) - { -// 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)); - } - } + recvfrom(udpSocket, &message, sizeof(struct clientInput), MSG_WAITALL, (struct sockaddr *)&clientAddress, &test); + sendto(udpSocket, arguments, sizeof(struct gameState), MSG_CONFIRM, + (struct sockaddr *)&clientAddress, (socklen_t)sizeof(struct sockaddr_in)); + updateInput(arguments, &message); + doGameTick(arguments); + bzero(&message, sizeof(struct clientInput)); } return NULL; @@ -88,10 +82,7 @@ int main(int argc, char ** argv) // Setup the sigint handler: signal(SIGINT, sigintHandler); - networkArguments.clientSockets = clientSockets; - networkArguments.state = ¤tState; - - pthread_create(&networkThread, NULL, networkThreadHandler, (void *)&networkArguments); + pthread_create(&networkThread, NULL, networkThreadHandler, ¤tState); // Setup TCP Master Socket: printf("Setting up master socket... ");