Compressing Networked State Data
blog.demofox.org
blog.demofox.org
Turns out, if you have a previous version of the state, you can have an enormous results with the most basic algorithm ever: prefix + suffix. I don't even know if it has any official name. Simply put, you find a length of common prefix of the two byte arrays, a common suffix, and send only the bytes in-between with the lengths of the suffxies. Since in our entity model most changes are contained to a couple of bytes in the middle most of the time, it's not uncommon to see compression factor of 10 and more.
And, of course, after compressing any object we compare the message length to the length of uncompressed state change message, and send the shortest one.
Using + and - enables some cute tricks, like binary searches of compressed, sorted data.
Otherwise a similar trick can be done if you have a compressor with stream support, which can be operated in a manner that:
Compress(String1 + String2) == Compress(String1) + X
Where + is concatenation and String1/2 are the serialized states.
Then if you want to send String2, you just send X (which is small if String1 and String2 similar enough).
And on the receiving side just run: Decompress(Compress(String1) + X)
After stripping String1 from the beginning you get String2.
It would be easy to fix: add a base-version number to the update. If the client doesn't know about the base-version it knows it must not update its state, but rather fetch a full state again or something.
Your suggestion "make sure there is a safe base version available and then send a diff against that" makes a lot more sense.
Maybe worth noting that if your world has some known starting state (eg after map load), it might be worth the client and server using that as the implicit initial state, even when a client joins a game in progress.
And after this, you're already half-way to TCP. Why not just use TCP for these updates anyway instead of running your own implementation?
This reminded me of an article I once read about the Doom3 network architecture [0]. Optimizing state transfer to network clients is a lot more difficult in the real world than xor-ing two states.
[0] http://fabiensanglard.net/doom3_documentation/The-DOOM-III-N...