diff --git a/Assets/Scripts/Networking/Clientside/ClientSyncer.cs b/Assets/Scripts/Networking/Clientside/ClientSyncer.cs index c9d008b..e7db3f3 100644 --- a/Assets/Scripts/Networking/Clientside/ClientSyncer.cs +++ b/Assets/Scripts/Networking/Clientside/ClientSyncer.cs @@ -1,46 +1,32 @@  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 + /// + /// Creates a Client syncer, constructor only defined because of inheritance of . + /// + public ClientSyncer() : base(1f / 10) {} - private const float TickInterval = 1 / 10f; - private float TimeSinceLastTick = TickInterval; - private int TickCounter = 0; + /// + /// Performs a client sync tick. + /// + /// The number of the tick, which can be used to determine a few things, like when to sync certain things. + 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..77f3e96 --- /dev/null +++ b/Assets/Scripts/Networking/Serverside/ServerSyncer.cs @@ -0,0 +1,85 @@ + +using Cyber.Entities; +using Cyber.Networking.Messages; +using System; +using System.Collections.Generic; + +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(); + + /// + /// Creates a Server syncer, constructor only defined because of inheritance of . + /// + 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); + } + + /// + /// Performs a server sync tick. + /// + /// The number of the tick, which can be used to determine a few things, like when to sync certain things. + 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..b7b9ca4 --- /dev/null +++ b/Assets/Scripts/Networking/Syncer.cs @@ -0,0 +1,58 @@ + +using UnityEngine; + +namespace Cyber.Networking { + + /// + /// An abstract syncer, used by and . + /// + public abstract class Syncer : MonoBehaviour { + + private int TickCounter = 0; + + private readonly float TickInterval; + private float TimeSinceLastTick = 0; + + private int SyncPacketID = 0; + + /// + /// Initializes the syncer, basically just sets the . + /// + /// + public Syncer(float tickInterval) { + TickInterval = tickInterval; + TimeSinceLastTick = tickInterval; + } + + /// + /// Returns the next ID for a sync packet. + /// + /// + public int NextSyncID() { + return SyncPacketID++; + } + + /// + /// Performs a tick on the syncer, called from . + /// + /// The tick number, used to e.g. determine when to sync certain things. + public abstract void PerformTick(int Tick); + + private void Update() { + TimeSinceLastTick += Time.deltaTime; + + if (TimeSinceLastTick > TickInterval) { + PerformTick(TickCounter); + + if (TickCounter < int.MaxValue) { + TickCounter++; + } else { + TickCounter = 0; + } + + TimeSinceLastTick -= TickInterval; + } + } + + } +} 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: