Sfoglia il codice sorgente

message test implemented

mikymod 13 anni fa
parent
commit
89a94c8364
5 ha cambiato i file con 61 aggiunte e 9 eliminazioni
  1. 1 0
      CMakeLists.txt
  2. 13 9
      src/network/Message.cpp
  3. 1 0
      src/network/Message.h
  4. 3 0
      tests/CMakeLists.txt
  5. 43 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/layouts
 	${CMAKE_SOURCE_DIR}/src/windowing/templates
+	${CMAKE_SOURCE_DIR}/src/network
 )
 
 include_directories(${INCLUDES})

+ 13 - 9
src/network/Message.cpp

@@ -342,8 +342,8 @@ void Message::write_string(const char* s, int32_t max_len, bool make_7_bit)
 	{
 		int32_t i;
 		int32_t l;
-		int8_t* data_ptr;
-		const int8_t* byte_ptr;
+		uint8_t* data_ptr;
+		const uint8_t* byte_ptr;
 		
 		// calculate s length
 		for (l = 0; s[l]; l++) {}
@@ -354,7 +354,7 @@ void Message::write_string(const char* s, int32_t max_len, bool make_7_bit)
 		}
 		
 		data_ptr = get_byte_space(l + 1);
-		byte_ptr = reinterpret_cast<const int8_t*>(s);
+		byte_ptr = reinterpret_cast<const uint8_t*>(s);
 		if (make_7_bit) 
 		{
 			for (i = 0; i < l; i++) 
@@ -397,17 +397,18 @@ void Message::write_ipv4addr(const os::IPv4Address addr)
 
 void Message::begin_reading() const
 {
-  
+	read_count = 0;
+	read_bit = 0;
 }
 
 int32_t Message::get_remaing_data() const
 {
-  
+	cur_size - read_count;
 }
 
 void Message::read_byte_align() const
 {
-  
+	read_bit = 0;
 }
 
 int32_t Message::read_bits(int32_t num_bits) const
@@ -418,12 +419,14 @@ int32_t Message::read_bits(int32_t num_bits) const
 	int32_t		fraction;
 	bool	sgn;
 
-	if ( !r_data ) {
+	if (!r_data) 
+	{
 		printf("cannot read from message");
 	}
 
 	// 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 );
 	}
 
@@ -447,7 +450,7 @@ int32_t Message::read_bits(int32_t num_bits) const
 		return -1;
 	}
 
-	while (value_bits < num_bits ) 
+	while (value_bits < num_bits) 
 	{
 		if (read_bit == 0) 
 		{
@@ -483,6 +486,7 @@ int32_t Message::read_bits(int32_t num_bits) const
 
 int32_t Message::read_int8() const
 {
+	return (int32_t)read_bits(-8);
 }
 
 int32_t Message::read_uint8() const

+ 1 - 0
src/network/Message.h

@@ -14,6 +14,7 @@ namespace network
 	
 	class Message
 	{
+	public:
 						Message();
 						~Message();
 

+ 3 - 0
tests/CMakeLists.txt

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

+ 43 - 0
tests/messages.cpp

@@ -0,0 +1,43 @@
+#include <cstdio>
+
+#include "Message.h"
+
+using namespace crown;
+
+int main()
+{
+
+  	network::Message m = network::Message();
+	uint32_t bits_written;
+	uint32_t rem_write_bits;
+	uint32_t bits_read;
+	uint32_t rem_read_bits;
+	
+	uint8_t tmp[10];
+	uint8_t res;
+	
+	m.init(tmp, 10);
+	m.write_int8(1);
+	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");
+	
+
+	
+	
+	return 0;
+}