Sfoglia il codice sorgente

Merge branch 'master' of https://github.com/taylor001/crown

Daniele Bartolini 13 anni fa
parent
commit
c035797f3a
7 ha cambiato i file con 291 aggiunte e 42 eliminazioni
  1. 1 0
      CMakeLists.txt
  2. 2 0
      TODO.txt
  3. 6 0
      src/core/math/MathUtils.h
  4. 175 37
      src/network/Message.cpp
  5. 4 5
      src/network/Message.h
  6. 3 0
      tests/CMakeLists.txt
  7. 100 0
      tests/messages.cpp

+ 1 - 0
CMakeLists.txt

@@ -29,6 +29,7 @@ set (INCLUDES
 	${CMAKE_SOURCE_DIR}/src/windowing/themes
 	${CMAKE_SOURCE_DIR}/src/windowing/themes
 	${CMAKE_SOURCE_DIR}/src/windowing/layouts
 	${CMAKE_SOURCE_DIR}/src/windowing/layouts
 	${CMAKE_SOURCE_DIR}/src/windowing/templates
 	${CMAKE_SOURCE_DIR}/src/windowing/templates
+	${CMAKE_SOURCE_DIR}/src/network
 )
 )
 
 
 include_directories(${INCLUDES})
 include_directories(${INCLUDES})

+ 2 - 0
TODO.txt

@@ -0,0 +1,2 @@
+TODO list:
+-change struct IPv4Address in struct netAddress

+ 6 - 0
src/core/math/MathUtils.h

@@ -32,6 +32,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #define BIT(i) (1 << i)
 #define BIT(i) (1 << i)
 
 
+#define FLOATSIGNBITSET(f)		((*(const unsigned long *)&(f)) >> 31)
+// #define FLOATSIGNBITNOTSET(f)	((~(*(const unsigned long *)&(f))) >> 31)
+// #define FLOATNOTZERO(f)			((*(const unsigned long *)&(f)) & ~(1<<31) )
+// #define INTSIGNBITSET(i)		(((const unsigned long)(i)) >> 31)
+// #define INTSIGNBITNOTSET(i)		((~((const unsigned long)(i))) >> 31)
+
 namespace crown
 namespace crown
 {
 {
 
 

+ 175 - 37
src/network/Message.cpp

@@ -1,5 +1,7 @@
 #include <cassert>
 #include <cassert>
+#include <cstring>
 
 
+#include "MathUtils.h"
 #include "Message.h"
 #include "Message.h"
 
 
 namespace crown
 namespace crown
@@ -320,49 +322,88 @@ 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_angle(real f)
-{
-	// needs to be implemented
-}
-
 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);
 }
 }
 
 
-void Message::write_string(const char* s, int32_t max_len, bool make7Bit)
+void Message::write_string(const char* s, int32_t max_len, bool make_7_bit)
 {
 {
-  
+	if (!s) 
+	{
+		write_data("", 1);
+	}
+	else 
+	{
+		int32_t i;
+		int32_t l;
+		uint8_t* data_ptr;
+		const uint8_t* byte_ptr;
+		
+		// calculate s length
+		for (l = 0; s[l]; l++) {}
+		
+		if (max_len >= 0 && l >= max_len) 
+		{
+			l = max_len - 1;
+		}
+		
+		data_ptr = get_byte_space(l + 1);
+		byte_ptr = reinterpret_cast<const uint8_t*>(s);
+		if (make_7_bit) 
+		{
+			for (i = 0; i < l; i++) 
+			{
+				if ( byte_ptr[i] > 127 ) 
+				{
+					data_ptr[i] = '.';
+				} 
+				else 
+				{
+					data_ptr[i] = byte_ptr[i];
+				}
+			}
+		}
+		else 
+		{
+			for (i = 0; i < l; i++) 
+			{
+				data_ptr[i] = byte_ptr[i];
+			}
+		}
+		
+		data_ptr[i] = '\0';
+	}  
 }
 }
 
 
 void Message::write_data(const void* data, int32_t length)
 void Message::write_data(const void* data, int32_t length)
 {
 {
-
+	memcpy(get_byte_space(length), data, length);
 }
 }
 
 
 void Message::write_ipv4addr(const os::IPv4Address addr)
 void Message::write_ipv4addr(const os::IPv4Address addr)
 {
 {
-  
+	uint8_t* ptr;
+	
+	ptr = get_byte_space(4);
+	memcpy(ptr, addr.address, 4);
+	write_uint16(addr.port);
 }
 }
 
 
 void Message::begin_reading() const
 void Message::begin_reading() const
 {
 {
-  
+	read_count = 0;
+	read_bit = 0;
 }
 }
 
 
 int32_t Message::get_remaing_data() const
 int32_t Message::get_remaing_data() const
 {
 {
-  
+	cur_size - read_count;
 }
 }
 
 
 void Message::read_byte_align() const
 void Message::read_byte_align() const
 {
 {
-  
+	read_bit = 0;
 }
 }
 
 
 int32_t Message::read_bits(int32_t num_bits) const
 int32_t Message::read_bits(int32_t num_bits) const
@@ -373,12 +414,14 @@ int32_t Message::read_bits(int32_t num_bits) const
 	int32_t		fraction;
 	int32_t		fraction;
 	bool	sgn;
 	bool	sgn;
 
 
-	if ( !r_data ) {
+	if (!r_data) 
+	{
 		printf("cannot read from message");
 		printf("cannot read from message");
 	}
 	}
 
 
 	// check if the number of bits is valid
 	// check if the number of bits is valid
-	if ( num_bits == 0 || num_bits < -31 || num_bits > 32 ) {
+	if ( num_bits == 0 || num_bits < -31 || num_bits > 32 ) 
+	{
 		printf("bad number of bits %i", num_bits );
 		printf("bad number of bits %i", num_bits );
 	}
 	}
 
 
@@ -402,7 +445,7 @@ int32_t Message::read_bits(int32_t num_bits) const
 		return -1;
 		return -1;
 	}
 	}
 
 
-	while (value_bits < num_bits ) 
+	while (value_bits < num_bits) 
 	{
 	{
 		if (read_bit == 0) 
 		if (read_bit == 0) 
 		{
 		{
@@ -438,63 +481,158 @@ int32_t Message::read_bits(int32_t num_bits) const
 
 
 int32_t Message::read_int8() const
 int32_t Message::read_int8() const
 {
 {
+	return (int32_t)read_bits(-8);
 }
 }
 
 
 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
 {
 {
-  
+	float value;
+	*reinterpret_cast<int*>(&value) = read_bits(32);
+	return value;  
 }
 }
 
 
-real Message::read_real(int32_t exp_bits, int32_t mant_bits) const
+Vec3 Message::read_vec3(int32_t num_bits) const
 {
 {
-  
+	return bits_to_vec3(read_bits(num_bits), num_bits);
 }
 }
 
 
-real Message::read_angle() 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;  
 }
 }
 
 
-Vec3 Message::read_vec3(int32_t num_bits) 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);  
 }
 }
 
 
-int32_t Message::read_string(char* buffer, int32_t buffer_size) 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::read_data(void* data, int32_t length) const
+int32_t Message::vec3_to_bits(const Vec3& v, int32_t num_bits)
 {
 {
+	assert(num_bits >= 6 && num_bits <= 32);
+	assert(v.squared_length() - 1.0f < 0.01f);
   
   
+	int32_t max; 
+	int32_t bits;
+	float bias;
+
+	num_bits /= 3;
+	max = (1 << (num_bits - 1)) - 1;
+	bias = 0.5f / max;
+
+	bits = FLOATSIGNBITSET(v.x) << (num_bits * 3 - 1);
+	bits |= ((int32_t)((math::abs(v.x) + bias) * max)) << (num_bits * 2);
+	bits |= FLOATSIGNBITSET(v.y) << (num_bits * 2 - 1);
+	bits |= ((int32_t)((math::abs(v.y) + bias) * max)) << (num_bits * 1);
+	bits |= FLOATSIGNBITSET(v.z) << (num_bits * 1 - 1);
+	bits |= ((int32_t)((math::abs(v.z) + bias) * max)) << (num_bits * 0);
+	
+	return bits;  
 }
 }
 
 
-void Message::read_ipv4addr(os::IPv4Address* addr) const
+Vec3 Message::bits_to_vec3(int32_t bits, int32_t num_bits)
 {
 {
+	assert(num_bits >= 6 && num_bits <= 32);
   
   
+	static float sign[2] = {1.0f, -1.0f};
+	int max;
+	float inv_max;
+	Vec3 v;
+
+	num_bits /= 3;
+	max = (1 << (num_bits - 1)) - 1;
+	inv_max = 1.0f / max;
+
+	v.x = sign[(bits >> (num_bits * 3 - 1)) & 1] * ((bits >> (num_bits * 2)) & max) * inv_max;
+	v.y = sign[(bits >> (num_bits * 2 - 1)) & 1] * ((bits >> (num_bits * 1)) & max) * inv_max;
+	v.z = sign[(bits >> (num_bits * 1 - 1)) & 1] * ((bits >> (num_bits * 0)) & max) * inv_max;
+	v.normalize();
+	
+	return v;
 }
 }
 
 
-// static int32_t		vec3_to_bits(const Vec3& v, int32_t num_bits);
-// static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
 }
 }
 }
 }

+ 4 - 5
src/network/Message.h

@@ -14,6 +14,7 @@ namespace network
 	
 	
 	class Message
 	class Message
 	{
 	{
+	public:
 						Message();
 						Message();
 						~Message();
 						~Message();
 
 
@@ -54,9 +55,8 @@ namespace network
 		void			write_int64(int32_t c);
 		void			write_int64(int32_t c);
 		void			write_real(real f);
 		void			write_real(real f);
 		void			write_real(real f, int32_t exp_bits, int32_t mant_bits);
 		void			write_real(real f, int32_t exp_bits, int32_t mant_bits);
-		void			write_angle(real f);
 		void			write_vec3(const Vec3& v, int32_t num_bits);
 		void			write_vec3(const Vec3& v, int32_t num_bits);
-		void			write_string(const char* s, int32_t max_len = -1, bool make7Bit = true);
+		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_data(const void* data, int32_t length);
 		void			write_ipv4addr(const os::IPv4Address addr);
 		void			write_ipv4addr(const os::IPv4Address addr);
 
 
@@ -71,14 +71,13 @@ namespace network
 		int32_t			read_int64() const;
 		int32_t			read_int64() const;
 		real			read_real() const;
 		real			read_real() const;
 		real			read_real(int32_t exp_bits, int32_t mant_bits) const;
 		real			read_real(int32_t exp_bits, int32_t mant_bits) const;
-		real			read_angle() const;
 		Vec3			read_vec3(int32_t num_bits) const;
 		Vec3			read_vec3(int32_t num_bits) const;
 		int32_t			read_string(char* buffer, int32_t buffer_size) const;
 		int32_t			read_string(char* buffer, int32_t buffer_size) const;
 		int32_t			read_data(void* data, int32_t length) const;
 		int32_t			read_data(void* data, int32_t length) const;
 		void			read_ipv4addr(os::IPv4Address* addr) const;
 		void			read_ipv4addr(os::IPv4Address* addr) const;
 
 
-// 		static int32_t		vec3_to_bits(const Vec3& v, int32_t num_bits);
-// 		static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
+ 		static int32_t	vec3_to_bits(const Vec3& v, int32_t num_bits);
+ 		static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
 
 
 	private:
 	private:
 	  
 	  

+ 3 - 0
tests/CMakeLists.txt

@@ -7,6 +7,9 @@ link_directories(${CROWN_BINARY_DIR})
 #link_libraries()
 #link_libraries()
 add_executable(allocators allocators.cpp)
 add_executable(allocators allocators.cpp)
 add_executable(containers containers.cpp)
 add_executable(containers containers.cpp)
+add_executable(messages messages.cpp)
+
 
 
 target_link_libraries(allocators crown)
 target_link_libraries(allocators crown)
 target_link_libraries(containers crown)
 target_link_libraries(containers crown)
+target_link_libraries(messages crown)

+ 100 - 0
tests/messages.cpp

@@ -0,0 +1,100 @@
+#include <cstdio>
+
+#include "Message.h"
+
+using namespace crown;
+
+int main()
+{
+
+	uint32_t bits_written;
+	uint32_t rem_write_bits;
+	uint32_t bits_read;
+	uint32_t rem_read_bits;
+
+//------------------------------------------------------------------
+  	network::Message m = network::Message();
+
+	uint8_t tmp[4];
+	uint8_t res;
+	
+	m.init(tmp, 4);
+	m.begin_writing();
+	m.write_uint8(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-----------------------------\n");
+	printf("start write and read for UINT8\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");
+	
+//------------------------------------------------------------------	
+	
+  	network::Message m1 = network::Message();
+	
+	uint8_t tmp1[4];
+	int8_t res1;
+
+	m1.init(tmp1, 4);
+	m1.write_int8(-56);
+	bits_written = m1.get_num_bits_written();
+	rem_write_bits = m1.get_remaining_write_bits();
+
+	res1 = m1.read_int8();
+	bits_read = m1.get_num_bits_read();
+	rem_read_bits = m1.get_remaining_read_bits();
+	
+	printf("-----------------------------\n");
+	printf("start write and read for INT8\n");
+	printf("value = %d\n", res1);
+	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");
+	
+//------------------------------------------------------------------	
+
+	network::Message m2 = network::Message();
+	
+	uint8_t tmp2[4];
+	int16_t res2;
+	
+	m2.init(tmp2, 4);
+	m2.write_int16(5555);
+	bits_written = m2.get_num_bits_written();
+	rem_write_bits = m2.get_remaining_write_bits();
+
+	res1 = m2.read_int16();
+	bits_read = m2.get_num_bits_read();
+	rem_read_bits = m2.get_remaining_read_bits();
+	
+	printf("-----------------------------\n");
+	printf("start write and read for INT16\n");
+	printf("value = %d\n", res2);
+	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");
+	
+
+	
+	
+	return 0;
+}