2017-05-09 16:15:21 +02:00
|
|
|
|
|
2017-05-12 00:24:34 +02:00
|
|
|
|
using Cyber.Console;
|
2017-05-09 16:15:21 +02:00
|
|
|
|
using Cyber.Entities;
|
2017-05-10 15:05:02 +02:00
|
|
|
|
using Cyber.Entities.SyncBases;
|
2017-05-09 16:15:21 +02:00
|
|
|
|
using Cyber.Networking.Messages;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Cyber.Networking.Serverside {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Keeps stuff in-sync over at clients. Periodically collects stuff that needs to be synced and then sends them on the next 'tick.'
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Syncer : MonoBehaviour {
|
|
|
|
|
|
|
|
|
|
private SyncDB Database;
|
|
|
|
|
|
|
|
|
|
private int TickCounter = 0;
|
|
|
|
|
private const float TickInterval = 1f / 10;
|
|
|
|
|
|
|
|
|
|
private float TimeSinceLastTick = TickInterval;
|
|
|
|
|
|
|
|
|
|
private List<int> QueuedSyncs = new List<int>();
|
|
|
|
|
private List<int> DirtySyncBases = new List<int>();
|
|
|
|
|
|
|
|
|
|
private int SyncPacketID = 0;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Mark a SyncBase "Dirty", which makes it eligible to sync.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="syncBaseID">The ID of the SyncBase. See <see cref="SyncBase.ID"/></param>
|
|
|
|
|
public void DirtSyncBase(int syncBaseID) {
|
|
|
|
|
DirtySyncBases.Add(syncBaseID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Queue a SyncBase directly, so it will be synced next time a sync tick is called.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SyncBaseID">The ID of the SyncBase. See <see cref="SyncBase.ID"/></param>
|
|
|
|
|
public void QueueSyncBase(int SyncBaseID) {
|
|
|
|
|
QueuedSyncs.Add(SyncBaseID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Start() {
|
|
|
|
|
Database = GetComponent<SyncDB>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
|
|
|
|
TimeSinceLastTick += Time.deltaTime;
|
|
|
|
|
if (TimeSinceLastTick >= TickInterval) {
|
|
|
|
|
|
|
|
|
|
var Categorized = Database.GetCategorizedDatabase();
|
|
|
|
|
|
|
|
|
|
foreach (Type type in Categorized.Keys) {
|
|
|
|
|
SyncHandletype Handletype = Database.GetSyncHandletypes()[type];
|
2017-05-10 16:34:16 +02:00
|
|
|
|
if (TickCounter % Handletype.TickInterval == 0) {
|
2017-05-09 16:15:21 +02:00
|
|
|
|
foreach (int SyncBaseID in Categorized[type]) {
|
2017-05-12 00:24:34 +02:00
|
|
|
|
bool Contains = DirtySyncBases.Contains(SyncBaseID);
|
|
|
|
|
if (Contains == Handletype.RequireHash || Contains) {
|
2017-05-09 16:15:21 +02:00
|
|
|
|
QueueSyncBase(SyncBaseID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (QueuedSyncs.Count > 0) {
|
|
|
|
|
int[] SyncIDs = QueuedSyncs.ToArray();
|
|
|
|
|
SyncPkt SyncPacket = new SyncPkt(Database, SyncIDs, SyncPacketID++);
|
2017-05-11 21:00:52 +02:00
|
|
|
|
Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable);
|
2017-05-09 16:15:21 +02:00
|
|
|
|
|
|
|
|
|
QueuedSyncs.Clear();
|
|
|
|
|
DirtySyncBases.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 16:34:16 +02:00
|
|
|
|
|
|
|
|
|
if (TickCounter < int.MaxValue) {
|
|
|
|
|
TickCounter++;
|
|
|
|
|
} else {
|
|
|
|
|
TickCounter = 0;
|
|
|
|
|
}
|
|
|
|
|
TimeSinceLastTick -= TickInterval;
|
2017-05-09 16:15:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|