remove src/common/Timer class, Network does its own time management
This commit is contained in:
parent
4c25126ba6
commit
5ba97bb089
@ -1,7 +1,6 @@
|
||||
#include <math.h>
|
||||
#include "Client.h"
|
||||
#include "Types.h"
|
||||
#include "Timer.h"
|
||||
|
||||
/* TODO: this should be moved to common somewhere */
|
||||
#define MAX_SHOT_DISTANCE 250.0
|
||||
@ -23,11 +22,9 @@ Client::~Client()
|
||||
{
|
||||
// Send disconnect message
|
||||
bool connection_closed = false;
|
||||
double close_timer;
|
||||
sf::Packet client_packet;
|
||||
sf::Uint8 packet_type = PLAYER_DISCONNECT;
|
||||
Timer client_timer;
|
||||
client_timer.Init();
|
||||
sf::Clock timeout_clock;
|
||||
client_packet.clear();
|
||||
client_packet << packet_type;
|
||||
client_packet << m_current_player;
|
||||
@ -36,14 +33,8 @@ Client::~Client()
|
||||
// No time out needed here, since the
|
||||
// message will timeout after a couple of attempts
|
||||
// then exit anyway.
|
||||
close_timer = Timer::GetTimeDouble();
|
||||
while(!connection_closed)
|
||||
{
|
||||
// Time must be updated before any messages are sent
|
||||
// Especially guaranteed messages, since the time needs to be
|
||||
// non zero.
|
||||
client_timer.Update();
|
||||
|
||||
m_net_client->Receive();
|
||||
|
||||
while(m_net_client->getData(client_packet))
|
||||
@ -74,7 +65,7 @@ Client::~Client()
|
||||
|
||||
// If the server does not respond within one second just close
|
||||
// and the server can deal with the problems.
|
||||
if((Timer::GetTimeDouble() - close_timer) > 1.0)
|
||||
if(timeout_clock.getElapsedTime().asSeconds() > 1.0)
|
||||
{
|
||||
connection_closed = true;
|
||||
}
|
||||
@ -87,8 +78,6 @@ Client::~Client()
|
||||
|
||||
void Client::run(bool fullscreen, int width, int height, std::string pname)
|
||||
{
|
||||
Timer client_timer;
|
||||
client_timer.Init();
|
||||
m_current_player_name = pname;
|
||||
if (!create_window(fullscreen, width, height))
|
||||
return;
|
||||
@ -163,11 +152,6 @@ void Client::run(bool fullscreen, int width, int height, std::string pname)
|
||||
}
|
||||
}
|
||||
|
||||
// Time must be updated before any messages are sent
|
||||
// Especially guaranteed messages, since the time needs to be
|
||||
// non zero.
|
||||
client_timer.Update();
|
||||
|
||||
update(elapsed_time);
|
||||
redraw();
|
||||
last_time = current_time;
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include "Network.h"
|
||||
#include "Timer.h"
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
@ -11,6 +10,7 @@ char Network::rxbuff[RECEIVE_BUFFER_SIZE];
|
||||
std::map<sf::Uint32, Transmit_Message_t*> Network::transmit_queue;
|
||||
Client_t Network::clients[MAX_NUM_CLIENTS];
|
||||
sf::Clock Network::message_timer;
|
||||
sf::Clock Network::network_timer;
|
||||
|
||||
sf::Uint32 Network::getUniqueMessageId( void )
|
||||
{
|
||||
@ -216,10 +216,10 @@ void Network::Receive()
|
||||
{
|
||||
if(MAX_NUM_CLIENTS > client_id)
|
||||
{
|
||||
clients[client_id].ping = Timer::GetTimeDouble() - transmit_queue[msg_id]->TimeStarted;
|
||||
clients[client_id].ping = network_timer.getElapsedTime().asSeconds() - transmit_queue[msg_id]->TimeStarted;
|
||||
|
||||
// Need to also register that a ping message was received.
|
||||
transmit_queue[msg_id]->Responses[&clients[client_id]] = Timer::GetTimeDouble();
|
||||
transmit_queue[msg_id]->Responses[&clients[client_id]] = network_timer.getElapsedTime().asSeconds();
|
||||
// Received a response, so reset send attempts.
|
||||
clients[client_id].num_send_attempts = 0u;
|
||||
}
|
||||
@ -231,7 +231,7 @@ void Network::Receive()
|
||||
// Set that the message was acknowledged by the client
|
||||
if(MAX_NUM_CLIENTS > client_id)
|
||||
{
|
||||
transmit_queue[msg_id]->Responses[&clients[client_id]] = Timer::GetTimeDouble();
|
||||
transmit_queue[msg_id]->Responses[&clients[client_id]] = network_timer.getElapsedTime().asSeconds();
|
||||
|
||||
// Received a response, so reset send attempts.
|
||||
clients[client_id].num_send_attempts = 0u;
|
||||
@ -284,8 +284,8 @@ void Network::Transmit()
|
||||
{
|
||||
// Broadcast the mesages to all clients
|
||||
sf::Uint32 msg_id = 0;
|
||||
static double ping_timer = Timer::GetTimeDouble();
|
||||
double current_time = Timer::GetTimeDouble();
|
||||
static double ping_timer = network_timer.getElapsedTime().asSeconds();
|
||||
double current_time = network_timer.getElapsedTime().asSeconds();
|
||||
|
||||
// Every five seconds, send ping messages
|
||||
// Note this time must be longer than the combined
|
||||
@ -313,7 +313,7 @@ void Network::Transmit()
|
||||
// Send any pending messages
|
||||
while(transmit_queue.find(msg_id) != transmit_queue.end())
|
||||
{
|
||||
double curTime = Timer::GetTimeDouble();
|
||||
double curTime = network_timer.getElapsedTime().asSeconds();
|
||||
Transmit_Message_t * message = transmit_queue[msg_id];
|
||||
switch(message->msg_type)
|
||||
{
|
||||
@ -324,7 +324,7 @@ void Network::Transmit()
|
||||
// send the message and update the sent times.
|
||||
if(0.0 == message->TimeStarted)
|
||||
{
|
||||
message->TimeStarted = Timer::GetTimeDouble();
|
||||
message->TimeStarted = network_timer.getElapsedTime().asSeconds();
|
||||
for(int i = 0; i < MAX_NUM_CLIENTS; i++)
|
||||
{
|
||||
if((clients[i].addr != sf::IpAddress::None) && (clients[i].port != 0))
|
||||
@ -360,7 +360,7 @@ void Network::Transmit()
|
||||
if(MAX_NUM_SEND_ATTEMPTS < iter->first->num_send_attempts)
|
||||
{
|
||||
// Fake a receive message so that it will complete and be removed from the queue
|
||||
message->Responses[iter->first] = Timer::GetTimeDouble();
|
||||
message->Responses[iter->first] = network_timer.getElapsedTime().asSeconds();
|
||||
iter->first->disconnect = TIMEOUT_DISCONNECT;
|
||||
}
|
||||
}
|
||||
|
@ -81,6 +81,7 @@ class Network{
|
||||
static std::map<sf::Uint32, Transmit_Message_t*> transmit_queue;
|
||||
static char rxbuff[RECEIVE_BUFFER_SIZE];
|
||||
static sf::Clock message_timer;
|
||||
static sf::Clock network_timer;
|
||||
static sf::Uint32 getUniqueMessageId();
|
||||
static int addClients(Client_t *client, sf::Uint16 *curcl);
|
||||
static int findClient(Client_t *client);
|
||||
|
@ -1,58 +0,0 @@
|
||||
#include "Timer.h"
|
||||
|
||||
// The number of time steps per second
|
||||
const float STEPS_PER_SECOND = 60.0f;
|
||||
|
||||
double Timer::totalElapsedTime;
|
||||
float Timer::stepTime;
|
||||
sf::Uint32 Timer::curTimeStep;
|
||||
|
||||
void Timer::Init(void)
|
||||
{
|
||||
// Reset the clock
|
||||
myClock.restart();
|
||||
|
||||
// Set the time keepers to zero
|
||||
totalElapsedTime = 0;
|
||||
|
||||
// Reset the game speed
|
||||
gameSpeed = 1.0f;
|
||||
}
|
||||
|
||||
void Timer::Update(void)
|
||||
{
|
||||
// Record the time step
|
||||
stepTime = (myClock.getElapsedTime().asSeconds() * gameSpeed);
|
||||
myClock.restart();
|
||||
|
||||
// Add the time to the total time
|
||||
totalElapsedTime += stepTime;
|
||||
|
||||
// Calculate the game step
|
||||
curTimeStep = (sf::Uint32)(totalElapsedTime * STEPS_PER_SECOND);
|
||||
}
|
||||
|
||||
sf::Uint32 Timer::GetTime(void)
|
||||
{
|
||||
return curTimeStep;
|
||||
}
|
||||
|
||||
float Timer::GetStepTime(void)
|
||||
{
|
||||
return stepTime;
|
||||
}
|
||||
|
||||
sf::Uint32 Timer::GetTotalTime(void)
|
||||
{
|
||||
return (sf::Uint32)(totalElapsedTime);
|
||||
}
|
||||
|
||||
double Timer::GetTimeDouble(void)
|
||||
{
|
||||
return totalElapsedTime;
|
||||
}
|
||||
|
||||
float Timer::GetElapsedTime(sf::Uint32 baseTime)
|
||||
{
|
||||
return (totalElapsedTime - ((double)baseTime / STEPS_PER_SECOND));
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
#ifndef _TIMER_H
|
||||
#define _TIMER_H
|
||||
|
||||
#include <SFML/System/Clock.hpp>
|
||||
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
// Initializes the time module
|
||||
void Init(void);
|
||||
|
||||
// Updates the time module
|
||||
void Update();
|
||||
|
||||
// Returns the current time step
|
||||
static sf::Uint32 GetTime(void);
|
||||
|
||||
// Returns the time that elapsed this step
|
||||
static float GetStepTime(void);
|
||||
|
||||
// Returns the total time elapsed in milliseconds
|
||||
static sf::Uint32 GetTotalTime(void);
|
||||
|
||||
// Returns the total time in seconds as a double
|
||||
static double GetTimeDouble(void);
|
||||
|
||||
// Returns the difference between the current time and the given time in seconds
|
||||
static float GetElapsedTime(sf::Uint32 baseTime);
|
||||
|
||||
private:
|
||||
// The clock used to take the time measurements
|
||||
sf::Clock myClock;
|
||||
|
||||
// The game speed
|
||||
float gameSpeed;
|
||||
|
||||
// The total elapsed time since the start of the game
|
||||
static double totalElapsedTime;
|
||||
|
||||
// The time that elapsed this step
|
||||
static float stepTime;
|
||||
|
||||
// The number of time steps since the start of the game
|
||||
static sf::Uint32 curTimeStep;
|
||||
};
|
||||
#endif
|
@ -1,7 +1,6 @@
|
||||
#include "Server.h"
|
||||
#include "Types.h"
|
||||
#include <math.h>
|
||||
#include "Timer.h"
|
||||
|
||||
Server::Server(sf::Uint16 port)
|
||||
{
|
||||
@ -20,18 +19,11 @@ void Server::run( void )
|
||||
double current_time;
|
||||
double elapsed_time;
|
||||
double last_time = 0.0;
|
||||
Timer server_timer;
|
||||
server_timer.Init();
|
||||
while(1)
|
||||
{
|
||||
current_time = m_clock.getElapsedTime().asSeconds();
|
||||
elapsed_time = current_time - last_time;
|
||||
|
||||
// Time must be updated before any messages are sent
|
||||
// Especially guaranteed messages, since the time needs to be
|
||||
// non zero.
|
||||
server_timer.Update();
|
||||
|
||||
update( elapsed_time );
|
||||
last_time = current_time;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user