Browse Source

header implemented as a pointer in BitMessage

mikymod 13 years ago
parent
commit
e6805b49bd
3 changed files with 119 additions and 42 deletions
  1. 46 6
      src/network/BitMessage.cpp
  2. 38 33
      src/network/BitMessage.h
  3. 35 3
      tests/messages.cpp

+ 46 - 6
src/network/BitMessage.cpp

@@ -19,7 +19,8 @@ BitMessage::BitMessage(Allocator& allocator) :
 	m_read_count(0),
 	m_write_bit(0), 
 	m_read_bit(0),
-	m_overflowed(false)
+	m_overflowed(false),
+	m_init(false)
 {
 }
 
@@ -81,7 +82,6 @@ bool BitMessage::check_overflow(int32_t num_bits)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::init(int32_t len)
 {
 	m_header = (uint8_t*)m_allocator->allocate(12);
@@ -91,38 +91,78 @@ void BitMessage::init(int32_t len)
 	m_write = m_data;
 	m_read = m_data;
 	m_max_size = len;
+	
+	m_init = true;
+}
+
+//---------------------------------------------------------------------------------------------
+void BitMessage::set_header(uint32_t id, uint16_t sequence, uint16_t ack, uint32_t ack_bits)
+{
+	uint8_t header[12];
+	header[0]	= (uint8_t)(id >> 24);
+	header[1]	= (uint8_t)(id >> 16);
+	header[2]	= (uint8_t)(id >> 8);
+	header[3]	= (uint8_t)id;
+	header[4]	= (uint8_t)(sequence >> 8);
+	header[5]	= (uint8_t)sequence;
+	header[6]	= (uint8_t)(ack >> 8);
+	header[7]	= (uint8_t)ack;
+	header[8]	= (uint8_t)(ack_bits >> 24);
+	header[9]	= (uint8_t)(ack_bits >> 16);
+	header[10]	= (uint8_t)(ack_bits >> 8);
+	header[11]	= (uint8_t)(ack_bits);
+	
+	memcpy(m_header, header, 12);
 }
 
 //---------------------------------------------------------------------------------------------
+uint8_t* BitMessage::get_header()
+{
+	return m_header;
+}
 
+//---------------------------------------------------------------------------------------------
+const uint8_t* BitMessage::get_header() const
+{
+	return m_header;
+}
+//---------------------------------------------------------------------------------------------
 uint8_t* BitMessage::get_data()
 {
 	return m_write;
 }
 
 //---------------------------------------------------------------------------------------------
-
 const uint8_t* BitMessage::get_data() const
 {
 	return m_read;
 }
 
 //---------------------------------------------------------------------------------------------
-
 size_t BitMessage::get_max_size() const
 {
 	return m_max_size;
 }
 
 //---------------------------------------------------------------------------------------------
-
 bool BitMessage::is_overflowed()
 {
 	return m_overflowed;
 }
 
 //---------------------------------------------------------------------------------------------
+bool BitMessage::is_init()
+{
+	return m_init;
+}
 
+//---------------------------------------------------------------------------------------------
+size_t BitMessage::get_header_size() const
+{
+	return 12;
+}
+
+//---------------------------------------------------------------------------------------------
 size_t BitMessage::get_size() const
 {
 	return m_cur_size;
@@ -715,7 +755,7 @@ void BitMessage::read_netaddr(os::NetAddress* addr) const
 void BitMessage::print() const
 {
 	os::printf("MAX_SIZE: %d\n", m_max_size);
-	os::printf("CUR_SIZE: %d\n", m_cur_size);	
+	os::printf("CUR_SIZE: %d\n", m_cur_size);
 }
   
 }	//namespace network

+ 38 - 33
src/network/BitMessage.h

@@ -21,35 +21,39 @@ public:
 						BitMessage(Allocator& allocator);
 						~BitMessage();
 
-	void				init(int32_t len);								// init with data length in byte
-	const uint8_t*		get_header();									// get message header
-	uint8_t*			get_data();										// get data for writing
-	const uint8_t*		get_data() const;								// get data for reading
-	size_t				get_max_size() const;							// get the maximum message size
-	bool 				is_overflowed();								// get overflowed flag
-
-	size_t				get_size() const;								// size of the message in bytes
-	void				set_size(size_t size);							// set the message size
-	int32_t				get_write_bit() const;							// get current write bit
-	void				set_write_bit(int32_t bit);						// set current write bit
-	int32_t				get_num_bits_written() const;					// returns number of bits written
-	int32_t				get_remaining_write_bits() const;				// space left in bits for writing
-	void				save_write_state(int32_t& s,int32_t& b) const;	// save the write state
-	void				restore_write_state(int32_t s,int32_t b);		// restore the write state
+	void				init(int32_t len);									// init with data length in byte
+	void				set_header(uint32_t id, uint16_t sequence, uint16_t ack, uint32_t ack_bits);
+	uint8_t*			get_header();										// get message header for writing
+	const uint8_t*		get_header() const;									// get message header for reading
+	uint8_t*			get_data();											// get message data for writing
+	const uint8_t*		get_data() const;									// get message data for reading
+	size_t				get_max_size() const;								// get the maximum message size
+	bool 				is_overflowed();									// is message overflowed
+	bool				is_init();											// is message initialized
+	
+	size_t				get_header_size() const;							// return 12 bytes
+	size_t				get_size() const;									// size of the message in bytes
+	void				set_size(size_t size);								// set the message size
+	int32_t				get_write_bit() const;								// get current write bit
+	void				set_write_bit(int32_t bit);							// set current write bit
+	int32_t				get_num_bits_written() const;						// returns number of bits written
+	int32_t				get_remaining_write_bits() const;					// space left in bits for writing
+	void				save_write_state(int32_t& s,int32_t& b) const;		// save the write state
+	void				restore_write_state(int32_t s,int32_t b);			// restore the write state
 
-	int32_t				get_read_count() const;							// bytes read so far
-	void				set_read_count(int32_t bytes);					// set the number of bytes and bits read
-	int32_t				get_read_bit() const;							// get current read bit
-	void				set_read_bit(int32_t bit);						// set current read bit
-	int32_t				get_num_bits_read() const;						// returns number of bits read
-	int32_t				get_remaining_read_bits() const;				// number of bits left to read
-	void				save_read_state(int32_t& c, int32_t& b) const;	// save the read state
-	void				restore_read_state(int32_t c, int32_t b);		// restore the read state
+	int32_t				get_read_count() const;								// bytes read so far
+	void				set_read_count(int32_t bytes);						// set the number of bytes and bits read
+	int32_t				get_read_bit() const;								// get current read bit
+	void				set_read_bit(int32_t bit);							// set current read bit
+	int32_t				get_num_bits_read() const;							// returns number of bits read
+	int32_t				get_remaining_read_bits() const;					// number of bits left to read
+	void				save_read_state(int32_t& c, int32_t& b) const;		// save the read state
+	void				restore_read_state(int32_t c, int32_t b);			// restore the read state
 						// write state utilities
-	void				begin_writing();								// begin writing
-	int32_t				get_remaining_space() const;					// space left in bytes
-	void				write_byte_align();								// write up to the next byte boundary
-	void				write_bits(int32_t value, int32_t num_bits);	// write the specified number of bits
+	void				begin_writing();									// begin writing
+	int32_t				get_remaining_space() const;						// space left in bytes
+	void				write_byte_align();									// write up to the next byte boundary
+	void				write_bits(int32_t value, int32_t num_bits);		// write the specified number of bits
 	void				write_int8(int32_t c);
 	void				write_uint8(int32_t c);
 	void				write_int16(int32_t c);
@@ -62,10 +66,10 @@ public:
 	void				write_netaddr(const os::NetAddress addr);
 
 						// read state utilities
-	void				begin_reading() const;							// begin reading.
-	int32_t				get_remaing_data() const;						// number of bytes left to read
-	void				read_byte_align() const;						// read up to the next byte boundary
-	int32_t				read_bits(int32_t num_bits) const;				// read the specified number of bits
+	void				begin_reading() const;								// begin reading.
+	int32_t				get_remaing_data() const;							// number of bytes left to read
+	void				read_byte_align() const;							// read up to the next byte boundary
+	int32_t				read_bits(int32_t num_bits) const;					// read the specified number of bits
 	int32_t				read_int8() const;
 	int32_t				read_uint8() const;
 	int32_t				read_int16() const;
@@ -88,7 +92,7 @@ private:
 	
 	Allocator*			m_allocator;								// memory allocator
 	
-	uint8_t*				m_header;
+	uint8_t*			m_header;
 	uint8_t*			m_data;
 	uint8_t*			m_write;									// pointer to data for writing
 	const uint8_t*		m_read;										// point32_ter to data for reading
@@ -101,7 +105,8 @@ private:
 	mutable int32_t		m_read_bit;									// number of bits read from the last read byte
 	
 	bool 				m_overflowed;								// overflow flag
-				
+	bool				m_init;										// is init flag
+	
 };
 } // namespace network
 } // namespace crown

+ 35 - 3
tests/messages.cpp

@@ -317,7 +317,7 @@ void test_data()
 
 void test_net_address()
 {
-  	uint32_t bits_written;
+	uint32_t bits_written;
 	uint32_t rem_write_bits;
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
@@ -350,12 +350,12 @@ void test_net_address()
 	printf("bits read = %d\n", bits_read);
 	printf("remaining read bits = %d\n", rem_read_bits);
 	printf("-----------------------------\n");
-	printf("\n");	
+	printf("\n");
 }
 
 int main()
 {
-	
+/*	
 	test_int8();
 	test_uint8();
 	test_int16();
@@ -366,6 +366,38 @@ int main()
 	test_string();
 	test_data();
 	test_net_address();
+*/
+	MallocAllocator allocator;
+	network::BitMessage msg = network::BitMessage(allocator);
+	
+	uint32_t protocol_id = 0xFFFFFFFF;
+	uint16_t sequence = 12345;
+	uint16_t ack	  = 12344;
+	uint32_t ack_bits = 1234543;
+	
+	msg.init(6);
+	msg.set_header(protocol_id, sequence, ack, ack_bits);
+	msg.begin_writing();
+	msg.write_string("prova", 6);
+	
+	msg.begin_reading();
+	uint8_t* header = msg.get_header();
+ 	char data[6];
+ 	msg.read_string(data, 6);
+	
+	uint32_t tmp1 = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
+	uint16_t tmp2 = header[4] << 8 | header[5];
+	uint16_t tmp3 = header[6] << 8 | header[7];
+	uint32_t tmp4 = header[8] << 24 | header[9] << 16 | header[10] << 8 | header[11];
+	
+	os::printf("protocol_id: %d\n", tmp1);
+	os::printf("sequence: %d\n", tmp2);
+	os::printf("ack: %d\n", tmp3);
+	os::printf("ack_bits: %d\n", tmp4);
+ 	os::printf("data: %s\n", data);
+	os::printf("\n");
+
+	
 	
 	return 0;
 }