Network: Extend post-desync frame sync history

Record sync state at various points throughout each frame
This commit is contained in:
Jonathan G Rennison
2023-08-28 15:17:53 +01:00
parent 8e755bf0c2
commit cdf1ff8d5a
13 changed files with 241 additions and 65 deletions

View File

@@ -309,10 +309,18 @@ void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
{
_frame_counter++;
const size_t total_sync_records = _network_sync_records.size();
_network_sync_records.push_back({ _frame_counter, _random.state[0], _state_checksum.state });
_record_sync_records = true;
NetworkExecuteLocalCommandQueue();
StateGameLoop();
_network_sync_records.push_back({ NSRE_FRAME_DONE, _random.state[0], _state_checksum.state });
_network_sync_record_counts.push_back((uint)(_network_sync_records.size() - total_sync_records));
_record_sync_records = false;
/* Check if we are in sync! */
if (_sync_frame != 0) {
if (_sync_frame == _frame_counter) {
@@ -341,7 +349,8 @@ void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
_last_sync_date_fract = _date_fract;
_last_sync_tick_skip_counter = _tick_skip_counter;
_last_sync_frame_counter = _sync_frame;
_network_client_sync_records.clear();
_network_sync_records.clear();
_network_sync_record_counts.clear();
/* If this is the first time we have a sync-frame, we
* need to let the server know that we are ready and at the same
@@ -358,8 +367,10 @@ void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
}
}
if (_network_client_sync_records.size() <= 256) {
_network_client_sync_records.push_back({ _frame_counter, _random.state[0], _state_checksum.state });
if (_network_sync_record_counts.size() >= 128) {
/* Remove records from start of queue */
_network_sync_records.erase(_network_sync_records.begin(), _network_sync_records.begin() + _network_sync_record_counts[0]);
_network_sync_record_counts.pop_front();
}
return true;
@@ -632,14 +643,30 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendDesyncMessage(const char *
/** Send an error-packet over the network */
NetworkRecvStatus ClientNetworkGameSocketHandler::SendDesyncSyncData()
{
if (_network_client_sync_records.empty()) return NETWORK_RECV_STATUS_OKAY;
if (_network_sync_record_counts.empty()) return NETWORK_RECV_STATUS_OKAY;
uint total = 0;
for (uint32 count : _network_sync_record_counts) {
total += count;
}
if ((size_t)total != _network_sync_records.size()) {
DEBUG(net, 0, "Network sync record error");
return NETWORK_RECV_STATUS_OKAY;
}
Packet *p = new Packet(PACKET_CLIENT_DESYNC_SYNC_DATA, SHRT_MAX);
p->Send_uint32(_network_client_sync_records[0].frame);
p->Send_uint32((uint)_network_client_sync_records.size());
for (uint i = 0; i < (uint)_network_client_sync_records.size(); i++) {
p->Send_uint32(_network_client_sync_records[i].seed_1);
p->Send_uint64(_network_client_sync_records[i].state_checksum);
p->Send_uint32((uint32)_network_sync_record_counts.size());
uint32 offset = 0;
for (uint32 count : _network_sync_record_counts) {
p->Send_uint32(count);
for (uint i = 0; i < count; i++) {
const NetworkSyncRecord &record = _network_sync_records[offset + i];
p->Send_uint32(record.frame);
p->Send_uint32(record.seed_1);
p->Send_uint64(record.state_checksum);
}
offset += count;
}
my_client->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;