• Advertisement

Archived

This topic is now archived and is closed to further replies.

bit-encoding of data

This topic is 5188 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I am currently working on some networking code and was wondering whether anyone of you already has experience on that topic. For sending data back and forth between peers, I implemented a generic packet format which allows me to store every kind of data (well, ... almost). Basically this works like this: 1. I have information about objects in general such as position, direction, etc ... 2. Every object has special information that can differ in type and value. For the latter, I am bit-encoding the data on the peer and sending it accross the network and the receiver decodes the data again for usage. This looks more or less like this: Sender: dwEncodedData = ( DWORD )whateverthisis; dwEncodedData |= ( ( DWORD )( someotherkindofstuff) ) << 1; dwEncodedData |= ( ( DWORD )( yetanotherpieceofdata) ) << 4; Receiver: whateverthisis = dwEncodedData & 0x01; someotherkindofstuff = ( dwEncodedData & 0x0E ) >> 1; yetanotherpieceofdata = ( dwEncodedData & 0xF0 ) >> 4; Don''t mind if there are any typos in there, but I guess you got the point. Problem is: 1. No compression 2. bitmasks have to be manually calculated, changes are horrible 3. sender''s and receiver''s code has to be in sync Does anyone of you have an idea of how to optimize this? Cheers, Chris.

Share this post


Link to post
Share on other sites
Advertisement
This is untested, but you can abstract the object into a stream form. Like this.



struct Bit_PacketA
{
int get_bits_size( void ) const {return 1;}
DWORD get_raw( void ) const {return 1;}
void set(DWORD val){}
};

struct Bit_PacketB
{
int get_bits_size( void ) const {return 1;}
DWORD get_raw( void ) const {return 1;}
void set(DWORD val){}
};

struct Bit_PacketC
{
int get_bits_size( void ) const {return 1;}
DWORD get_raw( void ) const {return 1;}
void set(DWORD val){}
};

struct Bit_Stream
{
enum { BITS_MAX = 32 };

Bit_Stream() : m_bit_pos(0), m_raw(0) {}

template <class T> void push ( T const& r_data )
{
assert((m_pos+r_data.get_bits_size())<BITS_MAX);

m_raw |= ( ( DWORD )( r_data.get_raw() ) ) << m_bit_pos;

m_bit_pos += r_data.get_bits_size();
}

template <class T> void pop ( T& r_data )
{
assert((m_pos-r_data.get_bits_size())>=0);

r_data.set(( ( DWORD )( m_raw ) ) >> m_bit_pos);

m_bit_pos -= r_data.get_bits_size();
}

private:

DWORD m_raw;
int m_bit_pos;

};

int test()
{
Bit_Stream stream;

Bit_PacketA a;
Bit_PacketB b;
Bit_PacketC c;

stream.push(a);
stream.push(b);
stream.push(c);

stream.pop(a);
stream.pop(b);
stream.pop(c);

return 0;
}


-ddn

Share this post


Link to post
Share on other sites
The impl is full of errrors, and is used just as an example of a bitstream, don''t use it for more than that.

-ddn

Share this post


Link to post
Share on other sites

  • Advertisement