/// <summary> /// Deserializes this packet from a binary stream. /// </summary> /// <param name="writer">PacketWriter used to deserialize the packet.</param> /// <param name="since">The minimum version of updates to pack.</param> public override void Write(PacketWriter writer, ReplicatedVersion since) { // Write the version vector only for root element if (this.Parent == null) { writer.Write(this.Version.NodeId); writer.Write(this.Version); } // Write a length (bookmark it) var bookmark = writer.Position; writer.Write(0); // Iterate through the values matching the 'since' constraint var count = 0; foreach (var entry in this.Map.Values) { // Ignore non-matching versions if (entry.Version < since.Of(entry.From)) { continue; } // Write the header writer.Write(entry.Version); writer.Write(entry.Deleted); writer.Write(entry.From); writer.Write(entry.Key); // Write the value only if we have it if (!entry.Deleted) { // Write the prefix if required this.WriteValuePrefix(entry.Value, writer); // Write the value itself writer.Write(entry.Value, since); } // Increment our pack count count++; } // Go back and write the final count var last = writer.Position; writer.Position = bookmark; writer.Write(count); writer.Position = last; }
/// <summary> /// Attempts to update the version vector. /// </summary> /// <param name="other">The other vector.</param> /// <returns></returns> public bool TryUpdate(ReplicatedVersion other) { // Go through all the other vectors var newer = false; foreach (var kvp in other.Vector) { if (kvp.Key == this.NodeId) { continue; } var v1 = this.Of(kvp.Key); var v2 = kvp.Value; if (v2 > v1) { this.Vector[kvp.Key] = kvp.Value; newer = true; } } // We updated something return(newer); }
/// <summary> /// Deserializes this packet from a binary stream. /// </summary> /// <param name="writer">PacketWriter used to deserialize the packet.</param> /// <param name="since">The minimum version of updates to pack.</param> public abstract void Write(PacketWriter writer, ReplicatedVersion since);