Packing data for UDP (and some questions)

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
flashkot
Prole
Posts: 27
Joined: Sun Jul 29, 2012 4:56 pm
Location: ru

Packing data for UDP (and some questions)

Post by flashkot »

Today I was thinking about optimal data representation in UDP packets. I know what it is not a good idea to make UDP packet bigger than 512 bytes or so.

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"}
}
at the moment only two types are supported. Integer (-32,768 to 32,767) and bit field within one byte (8 booleans - true or false)

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. :)
Attachments
BitsPack.love
(1.54 KiB) Downloaded 149 times
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests