/// <summary> /// Handle a given Network message. Must be checked to be <see cref="PktType.Sync"/> first. /// </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); 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); } } if (FailedSyncBases.Count > 0) { Client.Send(PktType.FailedChecksums, new IntListPkt(FailedSyncBases.ToArray())); } } LastTimestamp = SyncPacket.Timestamp; LastPing = NetworkHelper.GetTime() - LastTimestamp; LastSyncID = SyncPacket.SyncPacketID; if (SmallestSyncID == -1) { SmallestSyncID = SyncPacket.SyncPacketID; } SyncPacketsTotal++; } // Otherwise disregard the sync. }
/// <summary> /// Performs a server sync tick. /// </summary> /// <param name="Tick">The number of the tick, which can be used to determine a few things, like when to sync certain things.</param> public override void PerformTick(int Tick) { var Categorized = Database.GetCategorizedDatabase(); List <int> checksummedIds = new List <int>(); List <int> checksums = new List <int>(); foreach (Type type in Categorized.Keys) { SyncHandletype Handletype = Database.GetSyncHandletypes()[type]; if (Tick % Handletype.TickInterval == 0) { foreach (int SyncBaseID in Categorized[type]) { bool Contains = DirtySyncBases.Contains(SyncBaseID); if (Contains == Handletype.RequireHash || Contains) { QueueSyncBase(SyncBaseID); if (Contains) { DirtySyncBases.Remove(SyncBaseID); } } if (Handletype.RequireHash) { checksummedIds.Add(SyncBaseID); checksums.Add(Database.Get(SyncBaseID).GenerateChecksum()); } } } } if (QueuedSyncs.Count > 0) { int[] SyncIDs = QueuedSyncs.ToArray(); SyncPkt SyncPacket = new SyncPkt(Database, SyncIDs, checksummedIds.ToArray(), checksums.ToArray(), NextSyncID(), NetworkHelper.GetTime()); Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable); QueuedSyncs.Clear(); } }