2017-05-09 16:15:21 +02:00
|
|
|
|
|
2017-05-14 00:59:30 +02:00
|
|
|
|
using Cyber.Console;
|
2017-05-09 16:15:21 +02:00
|
|
|
|
using Cyber.Entities;
|
2017-05-14 00:59:30 +02:00
|
|
|
|
using Cyber.Entities.SyncBases;
|
2017-05-09 16:15:21 +02:00
|
|
|
|
using Cyber.Networking.Messages;
|
2017-05-14 00:59:30 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-05-09 16:15:21 +02:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace Cyber.Networking.Clientside {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A Short clientside class for handling sync packages.
|
|
|
|
|
/// It simply keeps track of sync-packages and will not apply them if they are too old.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SyncHandler {
|
|
|
|
|
|
|
|
|
|
private SyncDB SyncDB;
|
|
|
|
|
private int LatestSyncID = -1;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the SyncHandler with SyncDB.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="syncDB"></param>
|
|
|
|
|
public SyncHandler(SyncDB syncDB) {
|
|
|
|
|
SyncDB = syncDB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-05-11 21:00:52 +02:00
|
|
|
|
/// Handle a given Network message. Must be checked to be <see cref="PktType.Sync"/> first.
|
2017-05-09 16:15:21 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
public void HandleSyncPkt(NetworkMessage message) {
|
|
|
|
|
SyncPkt SyncPacket = new SyncPkt(SyncDB);
|
|
|
|
|
SyncPacket.Deserialize(message.reader);
|
|
|
|
|
if (LatestSyncID < SyncPacket.SyncPacketID) {
|
|
|
|
|
LatestSyncID = SyncPacket.SyncPacketID;
|
|
|
|
|
SyncPacket.ApplySync(message.reader);
|
2017-05-14 00:59:30 +02:00
|
|
|
|
|
|
|
|
|
int[] SyncBases = SyncPacket.ChecksummedSyncBases;
|
|
|
|
|
int[] Checksums = SyncPacket.Checksums;
|
|
|
|
|
if (SyncBases.Length > 0) {
|
|
|
|
|
List<int> FailedSyncBases = new List<int>();
|
|
|
|
|
for (int i = 0; i < SyncBases.Length; i++) {
|
|
|
|
|
SyncBase SyncBase = SyncDB.Get(SyncBases[i]);
|
|
|
|
|
if (SyncBase.GenerateChecksum() != Checksums[i]) {
|
|
|
|
|
FailedSyncBases.Add(SyncBase.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.Send(PktType.FailedChecksums, new IntListPkt(FailedSyncBases.ToArray()));
|
|
|
|
|
}
|
2017-05-09 16:15:21 +02:00
|
|
|
|
}
|
|
|
|
|
// Otherwise disregard the sync.
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|