[ENet-discuss] Sending numerical values in packets
Rene Dudfield
illumen at yahoo.com
Sat Nov 8 06:02:43 PST 2003
Rene Dudfield wrote:
> Mika Halttunen wrote:
>
>> Greetings!
>> I was wondering, how could I send numerical values, i.e. short ints,
>> ints and floats, using ENetPackets?
>>
>> I would also need to read them in on the receiver's side of course.
>> And what about big- and little-endianess?
>>
>> For example: I want to sent player position packet, containing id (=
>> byte) and a pair of floats.
>> ------------------------------------------
>> char array[128] = "";
>> int data_len = 1 + 4 + 4; // 1 byte, two floats
>>
>> // Modify the array here to contain the values
>> array[0] = PLAYER_POS_ID; // For example 1
>> // ...
>>
>> ENetPacket *packet = enet_packet_create(array, data_len, 0);
>>
>> // Send it away
>> // ...
>> ------------------------------------------
>
>
>
> If both your machines are of the same architecture, or use the same
> float format, you can just send the data as is. Or you could make
> your own function to cast floats into a network format, and another
> one to convert into the machines format. Another option is to use
> sprintf/sscanf to convert to and from strings containing floats.
>
> The sprintf/sscanf will be larger than necessary but at least easily
> portable. You do not need to write float2network, network2float
> functions for each platform.
>
> Maybe there is a better way which is portable allready?
>
Twistedmatrix.com has a banana serialization module which supports a
number of types. There are c, and python implementations.
Here are the specifications:
http://www.twistedmatrix.com/documents/specifications/banana
It sends floats as 8 byte doubles.
It also has support for different profiles, which seems good. If you
need to encode different things. It is slightly faster than the python
cPickle to encode 100 element lists of floats. Being able to encode 10,
100 element lists in 0.000442 seconds.
Another option may be to use a fixed point math library. That way you
can save a few bits here and there if you really need to.
Have fun!
http://www.holepit.com/
More information about the ENet-discuss
mailing list