Просмотр исходного кода

write/read header methods implemented in BitMessage

mikymod 13 лет назад
Родитель
Сommit
61a5db870c
2 измененных файлов с 141 добавлено и 90 удалено
  1. 34 6
      src/network/BitMessage.cpp
  2. 107 84
      src/network/BitMessage.h

+ 34 - 6
src/network/BitMessage.cpp

@@ -8,16 +8,18 @@ namespace crown
 namespace network
 {
   
-BitMessage::BitMessage(Allocator& allocator) : 
+BitMessage::BitMessage(Allocator& allocator) :
+	m_allocator(&allocator),
+	m_header(NULL),
 	m_data(NULL),
 	m_write(NULL), 
 	m_read(NULL), 
 	m_max_size(0), 
 	m_cur_size(0), 
+	m_read_count(0),
 	m_write_bit(0), 
-	m_read_count(0), 
 	m_read_bit(0),
-	m_allocator(&allocator)
+	m_overflowed(false)
 {
 }
 
@@ -461,7 +463,7 @@ void BitMessage::write_data(const void* data, int32_t length)
 
 //---------------------------------------------------------------------------------------------
 
-void BitMessage::write_ipv4addr(const os::NetAddress addr)
+void BitMessage::write_netaddr(const os::NetAddress addr)
 {
 	uint8_t* ptr;
 	
@@ -692,8 +694,7 @@ int32_t BitMessage::read_data(void* data, int32_t length) const
 }
 
 //---------------------------------------------------------------------------------------------
-
-void BitMessage::read_ipv4addr(os::NetAddress* addr) const
+void BitMessage::read_netaddr(os::NetAddress* addr) const
 {
 
 	for (int i = 0; i < 4; i++) 
@@ -704,5 +705,32 @@ void BitMessage::read_ipv4addr(os::NetAddress* addr) const
 	addr->port = read_uint16();  
 }
 
+//---------------------------------------------------------------------------------------------
+void BitMessage::write_header(Header& header)
+{
+	// write header only on top of BitMessage
+	if (m_cur_size == 0)
+	{
+		write_int32(header.protocol_id);
+		write_uint16(header.sequence);
+		write_uint16(header.ack);
+		write_int32(header.ack_bits);
+		write_uint16(header.size);
+	}
+}
+
+//---------------------------------------------------------------------------------------------
+size_t BitMessage::read_header(Header& header)
+{
+	header.protocol_id = read_int32();
+	header.sequence = read_uint16();
+	header.ack = read_uint16();
+	header.ack_bits = read_int32();
+	header.size = (size_t)read_uint16();
+}
+
+
+
+
 }	//namespace network
 }	//namespace crown

+ 107 - 84
src/network/BitMessage.h

@@ -13,100 +13,123 @@ namespace network
 	* bit-packet reliable message
 	*/
 
-	class BitMessage
-	{
+class BitMessage
+{
+
+public:
 
-	public:
+	struct Header
+	{	
+		// Header fields in BitMessage - 14 Bytes
+		uint32_t		protocol_id;
+		uint16_t		sequence;
+		uint16_t		ack;
+		uint32_t		ack_bits;
+		uint16_t		size;
+		// Used to checks BitMessage's validity
+		uint32_t		time;
 
-		struct Header
+		void set(uint32_t protocol_id, uint16_t sequence, uint16_t ack, uint32_t ack_bits, size_t size)
 		{
-			uint32_t		sequence;
-			uint32_t		time;
-			uint16_t		size;
-			
-			bool operator==(const Header& header)
-			{
-				return this->sequence == header.sequence;
-			}
-		}; 
+			this->protocol_id = protocol_id;
+			this->sequence = sequence;
+			this->ack = ack;
+			this->ack_bits = ack_bits;
+			this->size = size;
+		}
 		
-	public:
-							BitMessage(Allocator& allocator);
-							~BitMessage();
+		bool operator==(const Header& header)
+		{
+			return this->sequence == header.sequence;
+		}
+	}; 
+		
+public:
+						BitMessage(Allocator& allocator);
+						~BitMessage();
 
-		void				init(int32_t len);
-		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
+	void				init(int32_t len);
+	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
+	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				write_int8(int32_t c);
+	void				write_uint8(int32_t c);
+	void				write_int16(int32_t c);
+	void				write_uint16(int32_t c);
+	void				write_int32(int32_t c);
+	void				write_real(real f);
+	void				write_vec3(const Vec3& v);
+	void				write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
+	void				write_data(const void* data, int32_t length);
+	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
+	int32_t				read_int8() const;
+	int32_t				read_uint8() const;
+	int32_t				read_int16() const;
+	int32_t				read_uint16() const;
+	int32_t				read_int32() const;
+	real				read_real() const;
+	Vec3				read_vec3() const;
+	int32_t				read_string(char* buffer, int32_t buffer_size) const;
+	int32_t				read_data(void* data, int32_t length) const;
+	void				read_netaddr(os::NetAddress* addr) const;	
+						// header utilities
+	void				write_header(Header& header);
+	size_t				read_header(Header& header);
 
-		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);
-		void				write_uint16(int32_t c);
-		void				write_int32(int32_t c);
-		void				write_real(real f);
-		void				write_vec3(const Vec3& v);
-		void				write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
-		void				write_data(const void* data, int32_t length);
-		void				write_ipv4addr(const os::NetAddress addr);
+private:
+  
+	uint8_t*			get_byte_space(int32_t len);
+	bool 				check_overflow(int32_t num_bits);			// check buffer overflow	
+	
+private:
+	
+	Allocator*			m_allocator;								// memory allocator
 
-		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;
-		int32_t				read_uint16() const;
-		int32_t				read_int32() const;
-		real				read_real() const;
-		Vec3				read_vec3() const;
-		int32_t				read_string(char* buffer, int32_t buffer_size) const;
-		int32_t				read_data(void* data, int32_t length) const;
-		void				read_ipv4addr(os::NetAddress* addr) const;		
+	Header*				m_header;
 	
-	private:
-	  
-		uint8_t*			get_byte_space(int32_t len);
-		bool 				check_overflow(int32_t num_bits);			// check buffer overflow	
-		
-	private:
-				  //memory attributes
-		uint8_t*			m_data;
-		uint8_t*			m_write;										// pointer to data for writing
-		const uint8_t*		m_read;										// point32_ter to data for reading
-		size_t				m_max_size;									// maximum size of message in bytes
-		size_t				m_cur_size;									// current size of message in bytes
-		int32_t				m_write_bit;									// number of bits written to the last written byte
-		mutable int32_t		m_read_count;									// number of bytes read so far
-		mutable int32_t		m_read_bit;									// number of bits read from the last read byte
-		bool 				m_overflowed;									// overflow flag
-		Allocator*			m_allocator;								// memory allocator
-					
-  };
+	uint8_t*			m_data;
+	uint8_t*			m_write;									// pointer to data for writing
+	const uint8_t*		m_read;										// point32_ter to data for reading
+	
+	size_t				m_max_size;									// maximum size of message in bytes
+	size_t				m_cur_size;									// current size of message in bytes
+	mutable int32_t		m_read_count;								// number of bytes read so far
+	
+	int32_t				m_write_bit;								// number of bits written to the last written byte
+	mutable int32_t		m_read_bit;									// number of bits read from the last read byte
+	
+	bool 				m_overflowed;								// overflow flag
+				
+};
 } // namespace network
 } // namespace crown