Network: Add state checksum which is check in network sync

This commit is contained in:
Jonathan G Rennison
2019-08-05 19:16:51 +01:00
parent c941d2d2a6
commit a1d36b02f0
17 changed files with 76 additions and 2 deletions

View File

@@ -35,6 +35,7 @@
#include "../core/pool_func.hpp"
#include "../gfx_func.h"
#include "../error.h"
#include "../core/checksum_func.hpp"
#include "../safeguards.h"
@@ -74,6 +75,7 @@ uint32 _sync_seed_1; ///< Seed to compare during sync checks.
#ifdef NETWORK_SEND_DOUBLE_SEED
uint32 _sync_seed_2; ///< Second part of the seed.
#endif
uint64 _sync_state_checksum; ///< State checksum to compare during sync checks.
uint32 _sync_frame; ///< The frame to perform the sync check.
bool _network_first_time; ///< Whether we have finished joining or not.
bool _network_udp_server; ///< Is the UDP server started?
@@ -1028,6 +1030,7 @@ void NetworkGameLoop()
#ifdef NETWORK_SEND_DOUBLE_SEED
_sync_seed_2 = _random.state[1];
#endif
_sync_state_checksum = _state_checksum.state;
NetworkServer_Tick(send_frame);
} else {

View File

@@ -32,6 +32,7 @@
#include "../core/backup_type.hpp"
#include "../thread.h"
#include "../crashlog.h"
#include "../core/checksum_func.hpp"
#include "table/strings.h"
@@ -282,9 +283,9 @@ void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
if (_sync_frame != 0) {
if (_sync_frame == _frame_counter) {
#ifdef NETWORK_SEND_DOUBLE_SEED
if (_sync_seed_1 != _random.state[0] || _sync_seed_2 != _random.state[1]) {
if (_sync_seed_1 != _random.state[0] || _sync_seed_2 != _random.state[1] || _sync_state_checksum != _state_checksum.state) {
#else
if (_sync_seed_1 != _random.state[0]) {
if (_sync_seed_1 != _random.state[0] || _sync_state_checksum != _state_checksum.state) {
#endif
NetworkError(STR_NETWORK_ERROR_DESYNC);
DEBUG(desync, 1, "sync_err: date{%08x; %02x; %02x}", _date, _date_fract, _tick_skip_counter);
@@ -945,6 +946,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FRAME(Packet *p
#ifdef NETWORK_SEND_DOUBLE_SEED
_sync_seed_2 = p->Recv_uint32();
#endif
_sync_state_checksum = p->Recv_uint64();
}
#endif
/* Receive the token. */
@@ -972,6 +974,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_SYNC(Packet *p)
#ifdef NETWORK_SEND_DOUBLE_SEED
_sync_seed_2 = p->Recv_uint32();
#endif
_sync_state_checksum = p->Recv_uint64();
return NETWORK_RECV_STATUS_OKAY;
}

View File

@@ -118,6 +118,7 @@ extern uint32 _sync_seed_1;
#ifdef NETWORK_SEND_DOUBLE_SEED
extern uint32 _sync_seed_2;
#endif
extern uint64 _sync_state_checksum;
extern uint32 _sync_frame;
extern bool _network_first_time;
/* Vars needed for the join-GUI */

View File

@@ -689,6 +689,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendFrame()
#ifdef NETWORK_SEND_DOUBLE_SEED
p->Send_uint32(_sync_seed_2);
#endif
p->Send_uint64(_sync_state_checksum);
#endif
/* If token equals 0, we need to make a new token and send that. */
@@ -711,6 +712,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendSync()
#ifdef NETWORK_SEND_DOUBLE_SEED
p->Send_uint32(_sync_seed_2);
#endif
p->Send_uint64(_sync_state_checksum);
this->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;
}