make time consistent and fix sending time over network

This commit is contained in:
stalkerg
2020-08-14 16:51:33 +09:00
parent 4550530b70
commit 6c19ab84c1
5 changed files with 45 additions and 42 deletions

View File

@@ -76,10 +76,10 @@ void xtDoneApplication(void) {
void Syncro() {
const int dt_total = 1000 / 64;
static int t_prev = 0;
int dt = dt_total - (clock() - t_prev);
int dt = dt_total - (SDL_GetTicks() - t_prev);
if (dt > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(dt));
t_prev = clock();
t_prev = SDL_GetTicks();
}
int KDsPlus::Quant(void) {

View File

@@ -61,7 +61,7 @@ Game::Game(int ID) {
Game::ID = ID;
name[0] = 0;
client_version = 0;
birth_time = clock();
birth_time = SDL_GetTicks();
next = prev = 0;
list = 0;
used_players_IDs = 0;
@@ -862,7 +862,7 @@ void World::process_set_position(Player *player) {
p = p->next_alt;
}
if (send_position == 2)
player->last_sent_position = clock();
player->last_sent_position = SDL_GetTicks();
}
int World::check_visibility(Player *p1, Player *p2) {
@@ -925,9 +925,9 @@ Player::Player(Server *serv, XSocket &sock)
world = 0;
x = y = y_half_size_of_screen = 0;
x_prev = y_prev = y_half_size_of_screen_prev = 0;
last_IO_operation = clock() + 20 * 1000;
last_sent_position = clock();
birth_time = clock();
last_IO_operation = SDL_GetTicks() + 20 * 1000;
last_sent_position = SDL_GetTicks();
birth_time = SDL_GetTicks();
// current_sent_object = 0;
time_to_remove = 0;
next = prev = 0;
@@ -967,21 +967,21 @@ void Player::identification() {
int Player::is_alive() {
if (socket()) {
if ((int)(clock() - last_IO_operation) > 3000) {
if ((int)(SDL_GetTicks() - last_IO_operation) > 3000) {
short size = 0;
socket.send((const char *)&size, 2);
last_IO_operation = clock();
last_IO_operation = SDL_GetTicks();
}
return 1;
}
if (!game)
return 0;
if (!time_to_remove) {
time_to_remove = clock() + WAITING_TO_REMOVE;
time_to_remove = SDL_GetTicks() + WAITING_TO_REMOVE;
MOUT1("Connection have been lost", ID);
return 1;
}
return (int)(clock() - time_to_remove) < 0 ? 1 : 0;
return (int)(SDL_GetTicks() - time_to_remove) < 0 ? 1 : 0;
}
void Player::clear_object_queue(int keep_globals) {
@@ -1012,7 +1012,7 @@ int Player::receive() {
return 0;
int recv_size = in_buffer.receive(socket);
if (recv_size) {
last_IO_operation = clock();
last_IO_operation = SDL_GetTicks();
// IN_EVENTS_LOG1(Receive_Block,recv_size);
}
int code;
@@ -1296,7 +1296,7 @@ int Player::receive() {
case SERVER_TIME_QUERY:
out_buffer.begin_event(SERVER_TIME);
out_buffer < GLOBAL_CLOCK();
out_buffer < (unsigned int)GLOBAL_CLOCK();
out_buffer.end_event();
IN_EVENTS_LOG(SERVER_TIME_QUERY);
OUT_EVENTS_LOG(SERVER_TIME);
@@ -1375,7 +1375,7 @@ int Player::receive() {
case CLOSE_SOCKET:
socket.close();
time_to_remove = clock();
time_to_remove = SDL_GetTicks();
break;
default:
@@ -1395,7 +1395,7 @@ int Player::send() {
if (out_buffer.tell()) {
int sent;
total_sent += sent = out_buffer.send(socket);
last_IO_operation = clock();
last_IO_operation = SDL_GetTicks();
// OUT_EVENTS_LOG1(Send_Block,sent);
if (out_buffer.tell())
return total_sent;
@@ -1778,9 +1778,9 @@ void Server::consoleReport(int players) {
}
int Server::quant() {
// fout < "Quant: " <= frame < "\t" <= clock() < "\n";
if (next_broadcast < clock()) {
next_broadcast = clock() + 1000;
// fout < "Quant: " <= frame < "\t" <= SDL_GetTicks() < "\n";
if (next_broadcast < SDL_GetTicks()) {
next_broadcast = SDL_GetTicks() + 1000;
int n_players = 0;
Game *g = games.first();
while (g) {
@@ -1790,7 +1790,7 @@ int Server::quant() {
consoleReport(n_players);
if (time_to_live && !(transferring | games.size() | n_players | clients.size())) {
if (!time_to_destroy)
time_to_destroy = clock() + time_to_live * 1000;
time_to_destroy = SDL_GetTicks() + time_to_live * 1000;
} else
time_to_destroy = 0;
if (time_to_destroy && IS_PAST(time_to_destroy))
@@ -1890,7 +1890,7 @@ void Server::get_games_list(OutputEventBuffer &out_buffer, int client_version) {
out_buffer < g->ID < g->name < ": " <= g->players.size() < " " <
(g->data.GameType == VAN_WAR ? "V" : (g->data.GameType == MECHOSOMA ? "M" : "P"));
int t = (clock() - g->birth_time) / 1000;
int t = (SDL_GetTicks() - g->birth_time) / 1000;
int ts = t % 60;
t /= 60;
int tm = t % 60;

View File

@@ -325,12 +325,12 @@ struct Server {
/*******************************************************************************
Defines
*******************************************************************************/
#define GLOBAL_CLOCK() (round(clock() * (256. / CLOCKS_PER_SEC)))
#define SECONDS() (round(clock() * (1. / CLOCKS_PER_SEC)))
#define GLOBAL_CLOCK() (round(SDL_GetTicks() * (256. / 1000)))
#define SECONDS() (round(SDL_GetTicks() * (1. / 1000)))
#define SERVER_ERROR(str, code) ErrH.Abort(str, XERR_USER, code)
#define START_TIMER(interval) unsigned int _end_time_ = clock() + interval;
#define CHECK_TIMER() ((int)(clock() - _end_time_) < 0)
#define IS_FUTURE(time) ((int)((time)-clock()) > 0)
#define IS_PAST(time) ((int)(clock() - (time)) > 0)
#define TIME_INTERVAL(time) ((int)(clock() - (time)))
#define START_TIMER(interval) unsigned int _end_time_ = SDL_GetTicks() + interval;
#define CHECK_TIMER() ((int)(SDL_GetTicks() - _end_time_) < 0)
#define IS_FUTURE(time) ((int)((time)-SDL_GetTicks()) > 0)
#define IS_PAST(time) ((int)(SDL_GetTicks() - (time)) > 0)
#define TIME_INTERVAL(time) ((int)(SDL_GetTicks() - (time)))

View File

@@ -226,7 +226,7 @@ int identification(XSocket& socket)
char string[256] = "";
memset(string,0,256);
unsigned int len,identificated = 0;
START_TIMER(60*CLOCKS_PER_SEC);
START_TIMER(60*1000);
const char* request_str = "Vivat Sicher, Rock'n'Roll forever!!!";
strcpy(string, request_str);
string[strlen(string) + 1] = CLIENT_VERSION;
@@ -308,7 +308,7 @@ int ServersList::talk_to_server(int IP,int port,char* domain_name,int only_new_g
if(!sock.send(servers_buffer.GetBuf(),servers_buffer.tell()))
return 0;
START_TIMER(60*CLOCKS_PER_SEC);
START_TIMER(60*1000);
while(CHECK_TIMER())
if(sock.receive(servers_buffer.GetBuf(), servers_buffer.length(), 1000))
break;
@@ -597,10 +597,10 @@ int InputEventBuffer::receive_waiting_for_event(int event, XSocket& sock,int ski
{
//std::cout<<"InputEventBuffer::receive_waiting_for_event "<<event<<std::endl;
receive(sock);
START_TIMER(10*CLOCKS_PER_SEC);
START_TIMER(10*1000);
while(current_event() || CHECK_TIMER()) {
do {
//std::cout<<"current_event:"<<(int)current_event()<<" clock:"<<clock()<<" _end_time_:"<<_end_time_<<std::endl;
//std::cout<<"current_event:"<<(int)current_event()<<" clock:"<<SDL_GetTicks()<<" _end_time_:"<<_end_time_<<std::endl;
if(current_event() == event) {
//std::cout<<"ok"<<std::endl;
int size = event_size + 2;
@@ -723,6 +723,7 @@ int InputEventBuffer::next_event() {
//zmod
z_time_collect();
*this > time;
std::cout<<"server time:"<<time<<std::endl;
if(enable_transferring) {
ignore_event();
if(!lag_averaging_t0.empty()) {
@@ -859,9 +860,9 @@ int restore_connection()
void disconnect_from_server()
{
events_out.send_simple_query(CLOSE_SOCKET);
delay(256*(CLOCKS_PER_SEC/1000));
delay(256);
main_socket.close();
delay(256*(CLOCKS_PER_SEC/1000));
delay(256);
events_out.clear();
events_in.reset();
}
@@ -896,6 +897,8 @@ void set_time_by_server(int n_measures)
response_time /= N*256.;
average_lag = round(response_time*1000);
time_synchronization_sigma = sqrt((dtau2 - N*sqr(tau))/N/(N-1))/256.;
std::cout<<"set_time_by_server time_synchronization_sigma:"<<time_synchronization_sigma
<<" average_lag:"<<average_lag<<" t2:"<<t2<<std::endl;
}
int set_world(int world,int world_y_size) //znfo - send set_world event
{

View File

@@ -270,17 +270,17 @@ struct NetRndType
};
};
#ifndef CLOCK
#define CLOCK() (clock()*18/1000)
#define CLOCK() (SDL_GetTicks()*18/1000)
#endif
#define GLOBAL_CLOCK() (round(clock()*(256./1000)) - global_clock_tau)
#define LOCAL_CLOCK() (round(clock()*(256./1000)))
#define age_of_current_game() ((((int)GLOBAL_CLOCK() - (int)game_birth_time_offset) / 256) >> 8)
//#define age_of_current_game() (((int)((clock()/1000.0) - global_clock_tau) - (int)game_birth_time_offset) >> 8)
#define GLOBAL_CLOCK() (round(SDL_GetTicks()*(256./1000)) - global_clock_tau)
#define LOCAL_CLOCK() (round(SDL_GetTicks()*(256./1000)))
#define age_of_current_game() (((int)GLOBAL_CLOCK() - (int)game_birth_time_offset) >> 8)
//#define age_of_current_game() (((int)((SDL_GetTicks()/1000.0) - global_clock_tau) - (int)game_birth_time_offset) >> 8)
#define START_TIMER(interval) unsigned int _end_time_ = clock() + interval;
#define CHECK_TIMER() ((int)(clock() - _end_time_) < 0)
#define IS_FUTURE(time) ((int)((time) - clock()) > 0)
#define IS_PAST(time) ((int)(clock() - (time)) > 0)
#define START_TIMER(interval) unsigned int _end_time_ = SDL_GetTicks() + interval;
#define CHECK_TIMER() ((int)(SDL_GetTicks() - _end_time_) < 0)
#define IS_FUTURE(time) ((int)((time) - SDL_GetTicks()) > 0)
#define IS_PAST(time) ((int)(SDL_GetTicks() - (time)) > 0)
/*****************************************************************
Externs