Merge branch 'master' of github.com:Saltosion/Cyber

This commit is contained in:
excitedneon 2017-05-14 02:13:00 +03:00
commit 47781cb2a5
6 changed files with 124 additions and 11 deletions

View File

@ -1,6 +1,8 @@
 
using Cyber.Console;
using Cyber.Items; using Cyber.Items;
using Cyber.Networking; using Cyber.Networking;
using Cyber.Networking.Serverside;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
@ -21,9 +23,24 @@ namespace Cyber.Entities.SyncBases {
/// </summary> /// </summary>
public Inventory() { public Inventory() {
Drive = new Drive(10f); Drive = new Drive(10f);
if (Server.IsRunning()) {
Drive.AddItem(ItemDB.Singleton.Get(0)); Drive.AddItem(ItemDB.Singleton.Get(0));
Drive.AddItem(ItemDB.Singleton.Get(1)); Drive.AddItem(ItemDB.Singleton.Get(1));
} }
}
/// <summary>
/// Generates a checksum for the inventory
/// </summary>
/// <returns>A checksum of the IDs of the items</returns>
public override int GenerateChecksum() {
var Items = Drive.GetItems().ToArray();
int Checksum = 0;
for (int i = 0; i < Items.Length; i++) {
Checksum ^= Items[i].ID;
}
return Checksum;
}
/// <summary> /// <summary>
/// Returns the sync handletype indicating how the inventory should be synced. /// Returns the sync handletype indicating how the inventory should be synced.
@ -38,6 +55,8 @@ namespace Cyber.Entities.SyncBases {
/// </summary> /// </summary>
/// <param name="reader"></param> /// <param name="reader"></param>
public override void Deserialize(NetworkReader reader) { public override void Deserialize(NetworkReader reader) {
Debug.Log("Deserializing inventory!");
byte[][] ByteArray = new byte[4][]; byte[][] ByteArray = new byte[4][];
ByteArray[0] = reader.ReadBytesAndSize(); ByteArray[0] = reader.ReadBytesAndSize();
ByteArray[1] = reader.ReadBytesAndSize(); ByteArray[1] = reader.ReadBytesAndSize();

View File

@ -1,6 +1,9 @@
 
using Cyber.Console;
using Cyber.Entities; using Cyber.Entities;
using Cyber.Entities.SyncBases;
using Cyber.Networking.Messages; using Cyber.Networking.Messages;
using System.Collections.Generic;
using UnityEngine.Networking; using UnityEngine.Networking;
namespace Cyber.Networking.Clientside { namespace Cyber.Networking.Clientside {
@ -32,6 +35,20 @@ namespace Cyber.Networking.Clientside {
if (LatestSyncID < SyncPacket.SyncPacketID) { if (LatestSyncID < SyncPacket.SyncPacketID) {
LatestSyncID = SyncPacket.SyncPacketID; LatestSyncID = SyncPacket.SyncPacketID;
SyncPacket.ApplySync(message.reader); 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);
}
}
Client.Send(PktType.FailedChecksums, new IntListPkt(FailedSyncBases.ToArray()));
}
} }
// Otherwise disregard the sync. // Otherwise disregard the sync.

View File

@ -20,6 +20,16 @@ namespace Cyber.Networking.Messages {
/// </summary> /// </summary>
public int[] SyncedSyncBases; public int[] SyncedSyncBases;
/// <summary>
/// The Array of SyncIDs that have their hashes in this syncpacket
/// </summary>
public int[] ChecksummedSyncBases;
/// <summary>
/// The Array of Checksums that are sent with <see cref="ChecksummedSyncBases"/>.
/// </summary>
public int[] Checksums;
private SyncDB SyncDB; private SyncDB SyncDB;
/// <summary> /// <summary>
@ -27,11 +37,15 @@ namespace Cyber.Networking.Messages {
/// </summary> /// </summary>
/// <param name="syncDB">SyncDB to sync from.</param> /// <param name="syncDB">SyncDB to sync from.</param>
/// <param name="syncBases">The ID's of the SyncBases to sync.</param> /// <param name="syncBases">The ID's of the SyncBases to sync.</param>
/// <param name="checksummedSyncBases"></param>
/// <param name="checksums"></param>
/// <param name="syncPacketID">ID of the sync packet itself.</param> /// <param name="syncPacketID">ID of the sync packet itself.</param>
public SyncPkt(SyncDB syncDB, int[] syncBases, int syncPacketID) { public SyncPkt(SyncDB syncDB, int[] syncBases, int[] checksummedSyncBases, int[] checksums, int syncPacketID) {
SyncPacketID = syncPacketID; SyncPacketID = syncPacketID;
SyncDB = syncDB; SyncDB = syncDB;
SyncedSyncBases = syncBases; SyncedSyncBases = syncBases;
ChecksummedSyncBases = checksummedSyncBases;
Checksums = checksums;
} }
/// <summary> /// <summary>
@ -40,6 +54,8 @@ namespace Cyber.Networking.Messages {
/// <param name="syncDB">SyncBase to sync to.</param> /// <param name="syncDB">SyncBase to sync to.</param>
public SyncPkt(SyncDB syncDB) { public SyncPkt(SyncDB syncDB) {
SyncDB = syncDB; SyncDB = syncDB;
ChecksummedSyncBases = new int[0];
Checksums = new int[0];
} }
/// <summary> /// <summary>
@ -65,6 +81,25 @@ namespace Cyber.Networking.Messages {
foreach (int syncId in SyncedSyncBases) { foreach (int syncId in SyncedSyncBases) {
SyncDB.Get(syncId).Deserialize(reader); SyncDB.Get(syncId).Deserialize(reader);
} }
bool checksums = reader.ReadBoolean();
if (checksums) {
byte[][] SummedIdsByteArray = new byte[4][];
SummedIdsByteArray[0] = reader.ReadBytesAndSize();
SummedIdsByteArray[1] = reader.ReadBytesAndSize();
SummedIdsByteArray[2] = reader.ReadBytesAndSize();
SummedIdsByteArray[3] = reader.ReadBytesAndSize();
byte[][] SumsByteArray = new byte[4][];
SumsByteArray[0] = reader.ReadBytesAndSize();
SumsByteArray[1] = reader.ReadBytesAndSize();
SumsByteArray[2] = reader.ReadBytesAndSize();
SumsByteArray[3] = reader.ReadBytesAndSize();
ChecksummedSyncBases = NetworkHelper.DeserializeIntArray(SummedIdsByteArray);
Checksums = NetworkHelper.DeserializeIntArray(SumsByteArray);
}
} }
/// <summary> /// <summary>
@ -74,15 +109,33 @@ namespace Cyber.Networking.Messages {
public override void Serialize(NetworkWriter writer) { public override void Serialize(NetworkWriter writer) {
writer.Write(SyncPacketID); writer.Write(SyncPacketID);
byte[][] ByteArray = NetworkHelper.SerializeIntArray(SyncedSyncBases); byte[][] SyncedIdsByteArray = NetworkHelper.SerializeIntArray(SyncedSyncBases);
writer.WriteBytesFull(ByteArray[0]); writer.WriteBytesFull(SyncedIdsByteArray[0]);
writer.WriteBytesFull(ByteArray[1]); writer.WriteBytesFull(SyncedIdsByteArray[1]);
writer.WriteBytesFull(ByteArray[2]); writer.WriteBytesFull(SyncedIdsByteArray[2]);
writer.WriteBytesFull(ByteArray[3]); writer.WriteBytesFull(SyncedIdsByteArray[3]);
foreach (int syncId in SyncedSyncBases) { foreach (int syncId in SyncedSyncBases) {
SyncDB.Get(syncId).Serialize(writer); SyncDB.Get(syncId).Serialize(writer);
} }
if (ChecksummedSyncBases.Length == 0) {
writer.Write(false);
} else {
writer.Write(true);
byte[][] SummedIdsByteArray = NetworkHelper.SerializeIntArray(ChecksummedSyncBases);
writer.WriteBytesFull(SummedIdsByteArray[0]);
writer.WriteBytesFull(SummedIdsByteArray[1]);
writer.WriteBytesFull(SummedIdsByteArray[2]);
writer.WriteBytesFull(SummedIdsByteArray[3]);
byte[][] SumsByteArray = NetworkHelper.SerializeIntArray(Checksums);
writer.WriteBytesFull(SumsByteArray[0]);
writer.WriteBytesFull(SumsByteArray[1]);
writer.WriteBytesFull(SumsByteArray[2]);
writer.WriteBytesFull(SumsByteArray[3]);
}
} }
} }

View File

@ -60,5 +60,10 @@ namespace Cyber.Networking {
/// </summary> /// </summary>
public const short Disconnect = 209; public const short Disconnect = 209;
/// <summary>
/// Packet containing SyncBase id's of the SyncBases which failed the checksum test.
/// </summary>
public const short FailedChecksums = 210;
} }
} }

View File

@ -148,6 +148,7 @@ namespace Cyber.Networking.Serverside {
NetworkServer.RegisterHandler(PktType.Interact, HandlePacket); NetworkServer.RegisterHandler(PktType.Interact, HandlePacket);
NetworkServer.RegisterHandler(PktType.ClientSync, HandlePacket); NetworkServer.RegisterHandler(PktType.ClientSync, HandlePacket);
NetworkServer.RegisterHandler(PktType.Disconnect, HandlePacket); NetworkServer.RegisterHandler(PktType.Disconnect, HandlePacket);
NetworkServer.RegisterHandler(PktType.FailedChecksums, HandlePacket);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected); NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected); NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
@ -232,6 +233,13 @@ namespace Cyber.Networking.Serverside {
case PktType.Disconnect: case PktType.Disconnect:
msg.conn.Disconnect(); msg.conn.Disconnect();
break; break;
case PktType.FailedChecksums:
IntListPkt FailedSyncs = new IntListPkt();
FailedSyncs.Deserialize(msg.reader);
foreach (int SyncBaseId in FailedSyncs.IdList) {
Syncer.DirtSyncBase(SyncBaseId);
}
break;
default: default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType); Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType); Term.Println("Received an unknown packet, id: " + msg.msgType);

View File

@ -31,6 +31,9 @@ namespace Cyber.Networking.Serverside {
/// </summary> /// </summary>
/// <param name="syncBaseID">The ID of the SyncBase. See <see cref="SyncBase.ID"/></param> /// <param name="syncBaseID">The ID of the SyncBase. See <see cref="SyncBase.ID"/></param>
public void DirtSyncBase(int syncBaseID) { public void DirtSyncBase(int syncBaseID) {
if (DirtySyncBases.Contains(syncBaseID)) {
return;
}
DirtySyncBases.Add(syncBaseID); DirtySyncBases.Add(syncBaseID);
} }
@ -51,6 +54,8 @@ namespace Cyber.Networking.Serverside {
if (TimeSinceLastTick >= TickInterval) { if (TimeSinceLastTick >= TickInterval) {
var Categorized = Database.GetCategorizedDatabase(); var Categorized = Database.GetCategorizedDatabase();
List<int> checksummedIds = new List<int>();
List<int> checksums = new List<int>();
foreach (Type type in Categorized.Keys) { foreach (Type type in Categorized.Keys) {
SyncHandletype Handletype = Database.GetSyncHandletypes()[type]; SyncHandletype Handletype = Database.GetSyncHandletypes()[type];
@ -59,6 +64,13 @@ namespace Cyber.Networking.Serverside {
bool Contains = DirtySyncBases.Contains(SyncBaseID); bool Contains = DirtySyncBases.Contains(SyncBaseID);
if (Contains == Handletype.RequireHash || Contains) { if (Contains == Handletype.RequireHash || Contains) {
QueueSyncBase(SyncBaseID); QueueSyncBase(SyncBaseID);
if (Contains) {
DirtySyncBases.Remove(SyncBaseID);
}
}
if (Handletype.RequireHash) {
checksummedIds.Add(SyncBaseID);
checksums.Add(Database.Get(SyncBaseID).GenerateChecksum());
} }
} }
} }
@ -66,11 +78,10 @@ namespace Cyber.Networking.Serverside {
if (QueuedSyncs.Count > 0) { if (QueuedSyncs.Count > 0) {
int[] SyncIDs = QueuedSyncs.ToArray(); int[] SyncIDs = QueuedSyncs.ToArray();
SyncPkt SyncPacket = new SyncPkt(Database, SyncIDs, SyncPacketID++); SyncPkt SyncPacket = new SyncPkt(Database, SyncIDs, checksummedIds.ToArray(), checksums.ToArray(), SyncPacketID++);
Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable); Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable);
QueuedSyncs.Clear(); QueuedSyncs.Clear();
DirtySyncBases.Clear();
} }