Diff xmit of ID player pools

This commit is contained in:
2021-07-13 16:34:24 -04:00
parent 1904f03dad
commit 4357fd647f
4 changed files with 196 additions and 24 deletions

View File

@@ -301,10 +301,13 @@ void AnimQueue::add(int64_t id, const AnimStep &step) {
}
bool AnimQueue::valid() const {
// Animqueue must have at least one step.
// Animqueue must have at least one step, and no more than 255.
if (steps_.empty()) {
return false;
}
if (steps_.size() > 255) {
return false;
}
// First action should be blank.
if (steps_[0].action_ != "") {
return false;
@@ -342,17 +345,19 @@ void AnimQueue::deserialize(StreamBuffer *sb) {
}
}
void AnimQueue::make_patch(const AnimQueue &auth, StreamBuffer *sb) const {
bool AnimQueue::make_patch(const AnimQueue &auth, StreamBuffer *sb) const {
// Sanity check.
assert(valid());
assert(auth.valid());
// Special case: if we're already a perfect match, output 0 bytes.
if (exactly_equal(auth)) {
return;
sb->write_uint8(0);
return false;
}
// Write the first element.
sb->write_uint8(auth.steps_.size());
const AnimStep &first = auth.steps_[0];
int match = 0;
while ((match < int(steps_.size())) && (!steps_[match].state_equal(first))) {
@@ -388,18 +393,19 @@ void AnimQueue::make_patch(const AnimQueue &auth, StreamBuffer *sb) const {
}
}
}
return true;
}
void AnimQueue::apply_patch(StreamBuffer *sb) {
// Special case: if there are zero bytes, no patch is needed.
if (sb->at_eof()) {
int len = sb->read_uint8();
if (len == 0) {
return;
}
// Decode the diff, stop at eof.
std::deque<AnimStep> old = std::move(steps_);
steps_.clear();
while (!sb->at_eof()) {
for (int i = 0; i < len; i++) {
uint8_t index = sb->read_uint8();
if (index < 255) {
assert(index < old.size());
@@ -412,9 +418,10 @@ void AnimQueue::apply_patch(StreamBuffer *sb) {
int size = steps_.size();
if (size > 1) {
steps_[size-1].echo(steps_[size-2]);
} else {
steps_[0].keep_state_only();
}
}
steps_.front().keep_state_only();
}
const AnimStep &AnimQueue::back() const {
@@ -559,7 +566,7 @@ LuaDefine(unittests_animqueue, "c") {
aq.add(12345, stp);
sb.clear();
aqds.make_patch(aq, &sb);
LuaAssert(L, aqds.make_patch(aq, &sb));
aqds.apply_patch(&sb);
LuaAssert(L, aqds.exactly_equal(aq));
@@ -570,8 +577,19 @@ LuaDefine(unittests_animqueue, "c") {
stp.set_plane("where");
aq.add(232, stp);
// Generate diffs, but add 4 extra bytes.
sb.clear();
aqds.make_patch(aq, &sb);
LuaAssert(L, aqds.make_patch(aq, &sb));
sb.write_uint32(0);
// Apply the diffs, 4 extra bytes should remain.
aqds.apply_patch(&sb);
LuaAssert(L, sb.write_count() - sb.read_count() == 4);
LuaAssert(L, aqds.exactly_equal(aq));
// compare again, should be no differences.
sb.clear();
LuaAssert(L, !aqds.make_patch(aq, &sb));
aqds.apply_patch(&sb);
LuaAssert(L, aqds.exactly_equal(aq));
@@ -579,7 +597,7 @@ LuaDefine(unittests_animqueue, "c") {
aq.keep_only(1);
sb.clear();
aqds.make_patch(aq, &sb);
LuaAssert(L, aqds.make_patch(aq, &sb));
aqds.apply_patch(&sb);
LuaAssert(L, aqds.exactly_equal(aq));