Initial work on connecting clients over UDP.
This commit is contained in:
parent
a6d27ecfa4
commit
56c1332675
|
@ -60,6 +60,15 @@ 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;
|
||||||
|
struct sockaddr_in recieveAddress;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
// Create an SDL window and rendering context in that window:
|
// Create an SDL window and rendering context in that window:
|
||||||
SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED,
|
SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED,
|
||||||
|
@ -70,10 +79,13 @@ void * graphicsThreadHandler(void * parameters)
|
||||||
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;
|
||||||
|
|
||||||
// while (parameters->keepRunning)
|
bind(udpSocket, (struct sockaddr *)&recieveAddress, sizeof(struct sockaddr));
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
recvfrom(udpSocket, state, sizeof(struct gameState), 0, 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:
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
|
@ -1,4 +1,21 @@
|
||||||
#include "cspt-state.h"
|
#include "cspt-state.h"
|
||||||
|
void updateInput(struct gameState * state, struct clientInput * message, struct sockaddr_in * address, int * clientSockets)
|
||||||
|
{
|
||||||
|
int index = 0;
|
||||||
|
struct sockaddr_in currentClientAddress;
|
||||||
|
for (index = 0; index < 16; index++)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void doGameTick(struct gameState * state)
|
void doGameTick(struct gameState * state)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#define CSPT_STATE_H
|
#define CSPT_STATE_H
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
struct clientMovement
|
struct clientMovement
|
||||||
{
|
{
|
||||||
double xPosition, yPosition, xVelocity, yVelocity;
|
double xPosition, yPosition, xVelocity, yVelocity;
|
||||||
|
@ -20,6 +20,14 @@ struct gameState
|
||||||
struct clientMovement clients[16];
|
struct clientMovement clients[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct networkThreadArguments
|
||||||
|
{
|
||||||
|
int * clientSockets;
|
||||||
|
struct gameState * state;
|
||||||
|
};
|
||||||
|
|
||||||
|
void updateInput(struct gameState * state, struct clientInput * message, struct sockaddr_in * address, int * clientSockets);
|
||||||
|
|
||||||
void doGameTick(struct gameState * state);
|
void doGameTick(struct gameState * state);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -10,10 +10,12 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include "../cspt-state.h"
|
||||||
#include "../cspt-message.h"
|
#include "../cspt-message.h"
|
||||||
bool keepRunning = true;
|
bool keepRunning = true;
|
||||||
const int PORT = 5200;
|
const int PORT = 5200;
|
||||||
|
@ -29,8 +31,13 @@ void sigintHandler(int signal)
|
||||||
|
|
||||||
void * networkThreadHandler(void * arguments)
|
void * networkThreadHandler(void * arguments)
|
||||||
{
|
{
|
||||||
|
struct networkThreadArguments * args = (struct networkThreadArguments *)arguments;
|
||||||
int udpSocket;
|
int udpSocket;
|
||||||
struct sockaddr_in serverAddress;
|
pthread_t networkThread;
|
||||||
|
struct clientInput message;
|
||||||
|
socklen_t clientAddressLength;
|
||||||
|
struct sockaddr_in clientAddress, serverAddress;
|
||||||
|
|
||||||
if ((udpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
if ((udpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
||||||
{
|
{
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -40,6 +47,25 @@ void * networkThreadHandler(void * arguments)
|
||||||
serverAddress.sin_family = AF_INET;
|
serverAddress.sin_family = AF_INET;
|
||||||
serverAddress.sin_port = htons(PORT);
|
serverAddress.sin_port = htons(PORT);
|
||||||
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
// bind(udpSocket, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
||||||
|
|
||||||
|
printf("Started network thread.\n");
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -50,14 +76,23 @@ int main(int argc, char ** argv)
|
||||||
int masterSocket = 0;
|
int masterSocket = 0;
|
||||||
int clientSockets[16];
|
int clientSockets[16];
|
||||||
fd_set connectedClients;
|
fd_set connectedClients;
|
||||||
struct connectionStatus clientStatus[16];
|
pthread_t networkThread;
|
||||||
struct sockaddr_in serverAddress, clientAddress;
|
struct gameState currentState;
|
||||||
struct CsptMessage currentMessage;
|
struct CsptMessage currentMessage;
|
||||||
|
struct connectionStatus clientStatus[16];
|
||||||
|
struct networkThreadArguments networkArguments;
|
||||||
|
struct sockaddr_in serverAddress, clientAddress;
|
||||||
|
|
||||||
printf("Client-Side Prediction Test - Server Starting.\n");
|
printf("Client-Side Prediction Test - Server Starting.\n");
|
||||||
|
|
||||||
// Setup the sigint handler:
|
// Setup the sigint handler:
|
||||||
signal(SIGINT, sigintHandler);
|
signal(SIGINT, sigintHandler);
|
||||||
|
|
||||||
|
networkArguments.clientSockets = clientSockets;
|
||||||
|
networkArguments.state = ¤tState;
|
||||||
|
|
||||||
|
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... ");
|
||||||
masterSocket = socket(AF_INET, SOCK_STREAM, 0);
|
masterSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -179,13 +214,15 @@ int main(int argc, char ** argv)
|
||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
currentMessage.type = 0;
|
currentMessage.type = 0;
|
||||||
currentMessage.content = (uint8_t)random() % 16;
|
currentState.clients[index].registered = true;
|
||||||
|
currentMessage.content = (uint8_t)index;
|
||||||
send(clientSockets[index], ¤tMessage, sizeof(struct CsptMessage), 0);
|
send(clientSockets[index], ¤tMessage, sizeof(struct CsptMessage), 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Goodbye:
|
// Goodbye:
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
|
currentState.clients[index].registered = false;
|
||||||
FD_CLR(clientSockets[index], &connectedClients);
|
FD_CLR(clientSockets[index], &connectedClients);
|
||||||
shutdown(clientSockets[index], SHUT_RDWR);
|
shutdown(clientSockets[index], SHUT_RDWR);
|
||||||
clientSockets[index] = 0;
|
clientSockets[index] = 0;
|
||||||
|
@ -238,7 +275,6 @@ int main(int argc, char ** argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("Waiting on pongs.\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue