From 1a7e44b733c72f46ad39d151c206e81a02a166cf Mon Sep 17 00:00:00 2001 From: teascade Date: Sun, 14 May 2017 23:20:18 +0300 Subject: [PATCH] Add base class for Server/ClientSyncer, fix #3 --- .../Networking/Clientside/ClientSyncer.cs | 39 ++----- .../Scripts/Networking/Serverside/Server.cs | 6 +- .../Networking/Serverside/ServerSyncer.cs | 83 +++++++++++++++ .../{Syncer.cs.meta => ServerSyncer.cs.meta} | 0 .../Scripts/Networking/Serverside/Syncer.cs | 100 ------------------ Assets/Scripts/Networking/Syncer.cs | 44 ++++++++ Assets/Scripts/Networking/Syncer.cs.meta | 12 +++ 7 files changed, 151 insertions(+), 133 deletions(-) create mode 100644 Assets/Scripts/Networking/Serverside/ServerSyncer.cs rename Assets/Scripts/Networking/Serverside/{Syncer.cs.meta => ServerSyncer.cs.meta} (100%) delete mode 100644 Assets/Scripts/Networking/Serverside/Syncer.cs create mode 100644 Assets/Scripts/Networking/Syncer.cs create mode 100644 Assets/Scripts/Networking/Syncer.cs.meta diff --git a/Assets/Scripts/Networking/Clientside/ClientSyncer.cs b/Assets/Scripts/Networking/Clientside/ClientSyncer.cs index c9d008b..1e424f8 100644 --- a/Assets/Scripts/Networking/Clientside/ClientSyncer.cs +++ b/Assets/Scripts/Networking/Clientside/ClientSyncer.cs @@ -1,46 +1,25 @@  using Cyber.Networking.Messages; -using UnityEngine; namespace Cyber.Networking.Clientside { /// /// Syncer used on the clientside which sends sync packets from the client to the server containing their movement direction, rotation and possible checksums. /// - public class ClientSyncer : MonoBehaviour { + public class ClientSyncer : Syncer { - private const int ChecksumInterval = 10; // Every 10 ticks -> 1 per second + public ClientSyncer() : base(1f / 10) {} - private const float TickInterval = 1 / 10f; - private float TimeSinceLastTick = TickInterval; - private int TickCounter = 0; + public override void PerformTick(int Tick) { - private int SyncIDCounter = 0; + ClientSyncPkt SyncPkt = new ClientSyncPkt(); - private void Update() { - TimeSinceLastTick += Time.deltaTime; - if (TimeSinceLastTick >= TickInterval) { + var PlayerCharacter = Client.GetConnectedPlayer().Character; + SyncPkt.ClientSyncID = NextSyncID(); + SyncPkt.MoveDirection = PlayerCharacter.GetMovementDirection(); + SyncPkt.Rotation = PlayerCharacter.GetRotation(); - ClientSyncPkt SyncPkt = new ClientSyncPkt(); - - if (TickCounter % ChecksumInterval == 0) { - // Add checksums - } - - var PlayerCharacter = Client.GetConnectedPlayer().Character; - SyncPkt.ClientSyncID = SyncIDCounter++; - SyncPkt.MoveDirection = PlayerCharacter.GetMovementDirection(); - SyncPkt.Rotation = PlayerCharacter.GetRotation(); - - Client.SendByChannel(PktType.ClientSync, SyncPkt, NetworkChannelID.Unreliable); - - if (TickCounter < int.MaxValue) { - TickCounter += 1; - } else { - TickCounter = 0; - } - TimeSinceLastTick -= TickInterval; - } + Client.SendByChannel(PktType.ClientSync, SyncPkt, NetworkChannelID.Unreliable); } } diff --git a/Assets/Scripts/Networking/Serverside/Server.cs b/Assets/Scripts/Networking/Serverside/Server.cs index b02269f..a56d980 100644 --- a/Assets/Scripts/Networking/Serverside/Server.cs +++ b/Assets/Scripts/Networking/Serverside/Server.cs @@ -21,9 +21,9 @@ namespace Cyber.Networking.Serverside { private Spawner Spawner; /// - /// The Syncer which syncs. + /// The Syncer which syncs. /// - public Syncer Syncer; + public ServerSyncer Syncer; private ServerSyncHandler ServerSyncHandler; @@ -167,7 +167,7 @@ namespace Cyber.Networking.Serverside { Shutdown(); }); - Syncer = gameObject.AddComponent(); + Syncer = gameObject.AddComponent(); ServerSyncHandler = new ServerSyncHandler(Players); diff --git a/Assets/Scripts/Networking/Serverside/ServerSyncer.cs b/Assets/Scripts/Networking/Serverside/ServerSyncer.cs new file mode 100644 index 0000000..6b73708 --- /dev/null +++ b/Assets/Scripts/Networking/Serverside/ServerSyncer.cs @@ -0,0 +1,83 @@ + +using Cyber.Entities; +using Cyber.Networking.Messages; +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace Cyber.Networking.Serverside { + + /// + /// Keeps stuff in-sync over at clients. Periodically collects stuff that needs to be synced and then sends them on the next 'tick.' + /// + public class ServerSyncer : Syncer { + + private SyncDB Database; + + private List QueuedSyncs = new List(); + private List DirtySyncBases = new List(); + + public ServerSyncer() : base(1f / 10) {} + + /// + /// Mark a SyncBase "Dirty", which makes it eligible to sync. + /// + /// The ID of the SyncBase. See + public void DirtSyncBase(int syncBaseID) { + if (DirtySyncBases.Contains(syncBaseID)) { + return; + } + DirtySyncBases.Add(syncBaseID); + } + + /// + /// + /// + /// + public override void PerformTick(int Tick) { + var Categorized = Database.GetCategorizedDatabase(); + List checksummedIds = new List(); + List checksums = new List(); + + 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()); + Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable); + + QueuedSyncs.Clear(); + } + } + + /// + /// Queue a SyncBase directly, so it will be synced next time a sync tick is called. + /// + /// The ID of the SyncBase. See + public void QueueSyncBase(int SyncBaseID) { + QueuedSyncs.Add(SyncBaseID); + } + + private void Start() { + Database = GetComponent(); + } + + } +} diff --git a/Assets/Scripts/Networking/Serverside/Syncer.cs.meta b/Assets/Scripts/Networking/Serverside/ServerSyncer.cs.meta similarity index 100% rename from Assets/Scripts/Networking/Serverside/Syncer.cs.meta rename to Assets/Scripts/Networking/Serverside/ServerSyncer.cs.meta diff --git a/Assets/Scripts/Networking/Serverside/Syncer.cs b/Assets/Scripts/Networking/Serverside/Syncer.cs deleted file mode 100644 index bcbe82c..0000000 --- a/Assets/Scripts/Networking/Serverside/Syncer.cs +++ /dev/null @@ -1,100 +0,0 @@ - -using Cyber.Console; -using Cyber.Entities; -using Cyber.Entities.SyncBases; -using Cyber.Networking.Messages; -using System; -using System.Collections.Generic; -using UnityEngine; - -namespace Cyber.Networking.Serverside { - - /// - /// Keeps stuff in-sync over at clients. Periodically collects stuff that needs to be synced and then sends them on the next 'tick.' - /// - public class Syncer : MonoBehaviour { - - private SyncDB Database; - - private int TickCounter = 0; - private const float TickInterval = 1f / 10; - - private float TimeSinceLastTick = TickInterval; - - private List QueuedSyncs = new List(); - private List DirtySyncBases = new List(); - - private int SyncPacketID = 0; - - /// - /// Mark a SyncBase "Dirty", which makes it eligible to sync. - /// - /// The ID of the SyncBase. See - public void DirtSyncBase(int syncBaseID) { - if (DirtySyncBases.Contains(syncBaseID)) { - return; - } - DirtySyncBases.Add(syncBaseID); - } - - /// - /// Queue a SyncBase directly, so it will be synced next time a sync tick is called. - /// - /// The ID of the SyncBase. See - public void QueueSyncBase(int SyncBaseID) { - QueuedSyncs.Add(SyncBaseID); - } - - private void Start() { - Database = GetComponent(); - } - - private void Update() { - TimeSinceLastTick += Time.deltaTime; - if (TimeSinceLastTick >= TickInterval) { - - var Categorized = Database.GetCategorizedDatabase(); - List checksummedIds = new List(); - List checksums = new List(); - - foreach (Type type in Categorized.Keys) { - SyncHandletype Handletype = Database.GetSyncHandletypes()[type]; - if (TickCounter % 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(), SyncPacketID++); - Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable); - - QueuedSyncs.Clear(); - } - - - if (TickCounter < int.MaxValue) { - TickCounter++; - } else { - TickCounter = 0; - } - TimeSinceLastTick -= TickInterval; - } - - - } - - } -} diff --git a/Assets/Scripts/Networking/Syncer.cs b/Assets/Scripts/Networking/Syncer.cs new file mode 100644 index 0000000..eedf964 --- /dev/null +++ b/Assets/Scripts/Networking/Syncer.cs @@ -0,0 +1,44 @@ + +using UnityEngine; + +namespace Cyber.Networking { + + + public abstract class Syncer : MonoBehaviour { + + private int TickCounter = 0; + + private readonly float TickInterval; + private float TimeSinceLastTick = 0; + + private int SyncPacketID = 0; + + public Syncer(float tickInterval) { + TickInterval = tickInterval; + TimeSinceLastTick = tickInterval; + } + + public int NextSyncID() { + return SyncPacketID++; + } + + private void Update() { + TimeSinceLastTick += Time.deltaTime; + + if (TimeSinceLastTick > TickInterval) { + PerformTick(TickCounter); + + if (TickCounter < int.MaxValue) { + TickCounter++; + } else { + TickCounter = 0; + } + + TimeSinceLastTick -= TickInterval; + } + } + + public abstract void PerformTick(int Tick); + + } +} diff --git a/Assets/Scripts/Networking/Syncer.cs.meta b/Assets/Scripts/Networking/Syncer.cs.meta new file mode 100644 index 0000000..2a77d97 --- /dev/null +++ b/Assets/Scripts/Networking/Syncer.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: b9778f182f90b2a4291e31b85f12803c +timeCreated: 1494793021 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: