Update client to consider timestamp.
This commit is contained in:
parent
b87f2d06ba
commit
09f5684d3e
|
@ -82,12 +82,21 @@ void * networkHandler(void * parameters)
|
||||||
timeout.tv_sec = 0;
|
timeout.tv_sec = 0;
|
||||||
timeout.tv_usec = 1000;
|
timeout.tv_usec = 1000;
|
||||||
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||||
|
struct gameState * updatedState = calloc(1, sizeof(struct gameState));
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// Send our input, recieve the state:
|
// Send our input, recieve the state:
|
||||||
sendto(udpSocket, arguments->message, sizeof(struct clientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
sendto(udpSocket, arguments->message, sizeof(struct clientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
||||||
recvfrom(udpSocket, arguments->state, sizeof(struct gameState), 0, NULL, NULL);
|
recvfrom(udpSocket, updatedState, sizeof(struct gameState), 0, NULL, NULL);
|
||||||
|
if(updatedState->timestamp.tv_sec > arguments->state->timestamp.tv_sec)
|
||||||
|
{
|
||||||
|
memcpy(arguments->state, updatedState, sizeof(struct gameState));
|
||||||
|
}
|
||||||
|
else if(updatedState->timestamp.tv_sec == arguments->state->timestamp.tv_sec &&
|
||||||
|
updatedState->timestamp.tv_usec > arguments->state->timestamp.tv_usec)
|
||||||
|
{
|
||||||
|
memcpy(arguments->state, updatedState, sizeof(struct gameState));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,6 @@ void updateInput(struct gameState * state, struct clientInput * message)
|
||||||
|
|
||||||
void doGameTick(struct gameState * state)
|
void doGameTick(struct gameState * state)
|
||||||
{
|
{
|
||||||
state->timestamp = time(NULL);
|
|
||||||
for (int index = 0; index < 16; index++)
|
for (int index = 0; index < 16; index++)
|
||||||
{
|
{
|
||||||
// Calculate acceleration:
|
// Calculate acceleration:
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define CSPT_STATE_H
|
#define CSPT_STATE_H
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
struct clientMovement
|
struct clientMovement
|
||||||
{
|
{
|
||||||
|
@ -17,7 +18,7 @@ struct clientInput
|
||||||
|
|
||||||
struct gameState
|
struct gameState
|
||||||
{
|
{
|
||||||
time_t timestamp;
|
struct timeval timestamp;
|
||||||
struct clientMovement clients[16];
|
struct clientMovement clients[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ void sigintHandler(int signal)
|
||||||
void * networkThreadHandler(void * arguments)
|
void * networkThreadHandler(void * arguments)
|
||||||
{
|
{
|
||||||
struct ThreadParameters args = *(struct ThreadParameters *)arguments;
|
struct ThreadParameters args = *(struct ThreadParameters *)arguments;
|
||||||
printf("%p\n", args.clientAddresses);
|
|
||||||
struct sockaddr_in clientAddress, serverAddress;
|
struct sockaddr_in clientAddress, serverAddress;
|
||||||
|
|
||||||
if ((*args.udpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
if ((*args.udpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
||||||
|
@ -82,6 +81,7 @@ void * gameThreadHandler(void * arguments)
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
doGameTick(args.state);
|
doGameTick(args.state);
|
||||||
|
gettimeofday(&args.state->timestamp, NULL);
|
||||||
for(int index = 0; index < 16; index++)
|
for(int index = 0; index < 16; index++)
|
||||||
{
|
{
|
||||||
sendto(*(args.udpSocket), args.state, sizeof(struct gameState), 0,
|
sendto(*(args.udpSocket), args.state, sizeof(struct gameState), 0,
|
||||||
|
@ -230,7 +230,6 @@ int main(int argc, char ** argv)
|
||||||
// The client has sent a message, recieve it and process:
|
// The client has sent a message, recieve it and process:
|
||||||
if ((returnValue = recv(clientSockets[index], ¤tMessage, sizeof(struct CsptMessage), 0)) > 0)
|
if ((returnValue = recv(clientSockets[index], ¤tMessage, sizeof(struct CsptMessage), 0)) > 0)
|
||||||
{
|
{
|
||||||
printf("%s, %u\n", messageStrings[currentMessage.type], currentMessage.content);
|
|
||||||
switch (currentMessage.type)
|
switch (currentMessage.type)
|
||||||
{
|
{
|
||||||
// Hello:
|
// Hello:
|
||||||
|
|
Loading…
Reference in New Issue