mikymod 13 роки тому
батько
коміт
b5206f02a3
2 змінених файлів з 102 додано та 22 видалено
  1. 69 17
      src/network/Message.cpp
  2. 33 5
      tests/messages.cpp

+ 69 - 17
src/network/Message.cpp

@@ -322,11 +322,6 @@ void Message::write_real(real f)
 	write_bits(*reinterpret_cast<int32_t *>(&f), 32);  
 	write_bits(*reinterpret_cast<int32_t *>(&f), 32);  
 }
 }
 
 
-void Message::write_real(real f, int32_t exp_bits, int32_t mant_bits)
-{
-	//TODO:need to implement floatToBits function
-}
-
 void Message::write_vec3(const Vec3& v, int32_t num_bits)
 void Message::write_vec3(const Vec3& v, int32_t num_bits)
 {
 {
 	write_bits(vec3_to_bits(v, num_bits), num_bits);
 	write_bits(vec3_to_bits(v, num_bits), num_bits);
@@ -491,31 +486,30 @@ int32_t Message::read_int8() const
 
 
 int32_t Message::read_uint8() const
 int32_t Message::read_uint8() const
 {
 {
+  	return (int32_t)read_bits(8);
+
 }
 }
 
 
 int32_t Message::read_int16() const
 int32_t Message::read_int16() const
 {
 {
-  
+	return (int32_t)read_bits(-16);  
 }
 }
 
 
 int32_t Message::read_uint16() const
 int32_t Message::read_uint16() const
 {
 {
-  
+	return (int32_t)read_bits(16);  
 }
 }
 
 
 int32_t Message::read_int64() const
 int32_t Message::read_int64() const
 {
 {
-  
+	return (int32_t)read_bits(32);
 }
 }
 
 
 real Message::read_real() const
 real Message::read_real() const
 {
 {
-  
-}
-
-real Message::read_real(int32_t exp_bits, int32_t mant_bits) const
-{
-  
+	float value;
+	*reinterpret_cast<int*>(&value) = read_bits(32);
+	return value;  
 }
 }
 
 
 Vec3 Message::read_vec3(int32_t num_bits) const
 Vec3 Message::read_vec3(int32_t num_bits) const
@@ -525,17 +519,75 @@ Vec3 Message::read_vec3(int32_t num_bits) const
 
 
 int32_t Message::read_string(char* buffer, int32_t buffer_size) const
 int32_t Message::read_string(char* buffer, int32_t buffer_size) const
 {
 {
-  
+	int	l = 0;
+	int c;
+	
+	read_byte_align();
+	
+	while(1) 
+	{
+		c = read_uint8();
+		if (c <= 0 || c >= 255) 
+		{
+			break;
+		}
+		// translate all fmt spec to avoid crash bugs in string routines
+		if ( c == '%' ) 
+		{
+			c = '.';
+		}
+
+		// we will read past any excessively long string, so
+		// the following data can be read, but the string will
+		// be truncated
+		if (l < buffer_size - 1) 
+		{
+			buffer[l] = c;
+			l++;
+		}
+	}
+	
+	buffer[l] = 0;
+	return l;  
 }
 }
 
 
 int32_t Message::read_data(void* data, int32_t length) const
 int32_t Message::read_data(void* data, int32_t length) const
 {
 {
-  
+	int count;
+
+	read_byte_align();
+	
+	count = read_count;
+
+	if (read_count + length > cur_size) 
+	{
+		if (data) 
+		{
+			memcpy(data, r_data + read_count, get_remaing_data());
+		}
+		read_count = cur_size;
+	} 
+	else 
+	{
+		if (data) 
+		{
+			memcpy(data, r_data + read_count, length);
+		}
+		read_count += length;
+	}
+
+	return (read_count - count);  
 }
 }
 
 
 void Message::read_ipv4addr(os::IPv4Address* addr) const
 void Message::read_ipv4addr(os::IPv4Address* addr) const
 {
 {
-  
+
+	for (int i = 0; i < 4; i++) 
+	{
+		addr->address[i] = read_uint8();
+	}
+	
+	addr->port = read_uint16();  
 }
 }
 
 
 int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)
 int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)

+ 33 - 5
tests/messages.cpp

@@ -12,12 +12,16 @@ int main()
 	uint32_t rem_write_bits;
 	uint32_t rem_write_bits;
 	uint32_t bits_read;
 	uint32_t bits_read;
 	uint32_t rem_read_bits;
 	uint32_t rem_read_bits;
-	
-	uint8_t tmp[10];
+
+//------------------------------------------------------------------
+	uint8_t tmp[4];
 	uint8_t res;
 	uint8_t res;
 	
 	
-	m.init(tmp, 10);
-	m.write_int8(1);
+	m.init(tmp, 4);
+	m.write_uint8(255);
+// 	m.write_byte_align();
+	m.write_uint8(255);
+	m.write_byte_align();
 	bits_written = m.get_num_bits_written();
 	bits_written = m.get_num_bits_written();
 	rem_write_bits = m.get_remaining_write_bits();
 	rem_write_bits = m.get_remaining_write_bits();
 
 
@@ -25,7 +29,7 @@ int main()
 	bits_read = m.get_num_bits_read();
 	bits_read = m.get_num_bits_read();
 	rem_read_bits = m.get_remaining_read_bits();
 	rem_read_bits = m.get_remaining_read_bits();
 	
 	
-	printf("-----------------------------\n");
+	printf("\n-----------------------------\n");
 	printf("start write and read for INT8\n");
 	printf("start write and read for INT8\n");
 	printf("value = %d\n", res);
 	printf("value = %d\n", res);
 	printf("bits written = %d\n", bits_written);
 	printf("bits written = %d\n", bits_written);
@@ -35,6 +39,30 @@ int main()
 	printf("-----------------------------\n");
 	printf("-----------------------------\n");
 
 
 	printf("\n");
 	printf("\n");
+//------------------------------------------------------------------
+	/*int8_t tmp[1];
+	int8_t res;
+	
+	m.init(tmp, 1);
+	m.write_int8(255);
+	bits_written = m.get_num_bits_written();
+	rem_write_bits = m.get_remaining_write_bits();
+
+	res = m.read_int8();
+	bits_read = m.get_num_bits_read();
+	rem_read_bits = m.get_remaining_read_bits();
+	
+	printf("-----------------------------\n");
+	printf("start write and read for INT8\n");
+	printf("value = %d\n", res);
+	printf("bits written = %d\n", bits_written);
+	printf("remaining write bits = %d\n", rem_write_bits);
+	printf("bits read = %d\n", bits_read);
+	printf("remaining read bits = %d\n", rem_read_bits);
+	printf("-----------------------------\n");
+
+	printf("\n");*/	
+