In UDP tutorial from Wiki UDP packets are just human readable strings. What a waste of space, isn't it?
So, i decided to make C-like structs for Love2d in pure Lua. I attached example where I benchmarking 4000 pack/unpack commands. And eventually my functions are sometimes faster than method from tutorial. I also created functions with hardcoded structure and get nice speedup.
How it works:
At first you declare your structure like this
Code: Select all
structure = {
{typ = "int", name = "x"},
{typ = "int", name = "y"},
{typ = "flags", name = "flags 1", names = {"IsActive", "IsHidden", "IsRunning"}},
{typ = "int", name = "angle"}
}
Also, there are no check for input data, so if you provide wrong input there be an error in this code.
Next, you just call BitPack() function with your structure and your data table as parameters. Data table should contain all names defined in your structure. This function returns string of bytes what represents your data.
Of course, there is a reverse function - BitUnpack()
For my project i thinking about 200 unpacks and 20 packs per second on server side and 20 pack/unpack on client side. Average values.
And no more than 10 types of packets.
So at first networking can be done with structures definitions and after stabilization of protocol recreated with hardcoded pack/unpack functions for each packet type.
So, my questions is: what you think about this and how this can be optimized?
p.s. as usual, in demo you can change type of function by pressing space.