Sfoglia il codice sorgente

AsyncConnection improvements

mikymod 13 anni fa
parent
commit
19edbe2157

+ 2 - 2
src/CMakeLists.txt

@@ -239,13 +239,13 @@ set (WIN_SRC
 
 
 set (NETWORK_SRC
 set (NETWORK_SRC
 	network/BitMessage.cpp
 	network/BitMessage.cpp
-	network/Connection.cpp
+	network/AsyncConnection.cpp
 	
 	
 )
 )
 
 
 set (NETWORK_HEADERS
 set (NETWORK_HEADERS
 	network/BitMessage.h
 	network/BitMessage.h
-	network/Connection.h
+	network/AsyncConnection.h
 )
 )
 
 
 set (SOURCES
 set (SOURCES

+ 29 - 12
src/core/streams/FileStream.cpp

@@ -55,6 +55,31 @@ FileStream::~FileStream()
 	m_file = 0;
 	m_file = 0;
 }
 }
 
 
+//-----------------------------------------------------------------------------
+void FileStream::seek(size_t position)
+{
+	check_valid();
+
+	//flush(); <<<---?
+	fseek(m_file->get_handle(), position, SEEK_SET);
+}
+
+//-----------------------------------------------------------------------------
+void FileStream::seek_to_end()
+{
+	check_valid();
+
+	fseek(m_file->get_handle(), 0, SEEK_END);
+}
+
+//-----------------------------------------------------------------------------
+void FileStream::skip(size_t bytes)
+{
+	check_valid();
+
+	fseek(m_file->get_handle(), bytes, SEEK_CUR);
+}
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 uint8_t FileStream::read_byte()
 uint8_t FileStream::read_byte()
 {
 {
@@ -77,7 +102,7 @@ uint8_t FileStream::read_byte()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void FileStream::read_data_block(void* buffer, size_t size)
+void FileStream::read(void* buffer, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 
 
@@ -121,7 +146,7 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 			{
 			{
 				if (readBytes != 0)
 				if (readBytes != 0)
 				{
 				{
-					stream->write_data_block(buff, readBytes);
+					stream->write(buff, readBytes);
 				}
 				}
 			}
 			}
 
 
@@ -130,7 +155,7 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 			return false;
 			return false;
 		}
 		}
 
 
-		stream->write_data_block(buff, readBytes);
+		stream->write(buff, readBytes);
 		totReadBytes += readBytes;
 		totReadBytes += readBytes;
 	}
 	}
 
 
@@ -175,7 +200,7 @@ void FileStream::write_byte(uint8_t val)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void FileStream::write_data_block(const void* buffer, size_t size)
+void FileStream::write(const void* buffer, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 
 
@@ -198,14 +223,6 @@ void FileStream::flush()
 	fflush(m_file->get_handle());
 	fflush(m_file->get_handle());
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void FileStream::seek(int32_t position, SeekMode mode)
-{
-	check_valid();
-	//flush(); <<<---?
-	fseek(m_file->get_handle(), position, (mode==SM_FROM_BEGIN)?SEEK_SET:(mode==SM_FROM_CURRENT)?SEEK_CUR:SEEK_END);
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 size_t FileStream::position() const
 size_t FileStream::position() const
 {
 {

+ 15 - 10
src/core/streams/FileStream.h

@@ -53,18 +53,23 @@ public:
 						Destructor
 						Destructor
 					*/
 					*/
 	virtual			~FileStream();
 	virtual			~FileStream();
-					/** @copydoc Stream::Seek() */
-	void			seek(int32_t position, SeekMode mode);
+
+					/** @copydoc Stream::seek() */
+	void			seek(size_t position);
+					/** @copydoc Stream::seek_to_end() */
+	void			seek_to_end();
+					/** @copydoc Stream::skip() */
+	void			skip(size_t bytes);
 					/** @copydoc Stream::ReadByte() */
 					/** @copydoc Stream::ReadByte() */
 	uint8_t			read_byte();
 	uint8_t			read_byte();
 					/** @copydoc Stream::ReadDataBlock() */
 					/** @copydoc Stream::ReadDataBlock() */
-	void			read_data_block(void* buffer, size_t size);
-					/** @copydoc Stream::CopyTo() */
-	bool			copy_to(Stream* stream, size_t size = 0);
+	void			read(void* buffer, size_t size);
 					/** @copydoc Stream::WriteByte() */
 					/** @copydoc Stream::WriteByte() */
 	void			write_byte(uint8_t val);
 	void			write_byte(uint8_t val);
 					/** @copydoc Stream::WriteDataBlock() */
 					/** @copydoc Stream::WriteDataBlock() */
-	void			write_data_block(const void* buffer, size_t size);
+	void			write(const void* buffer, size_t size);
+					/** @copydoc Stream::CopyTo() */
+	bool			copy_to(Stream* stream, size_t size = 0);
 					/** @copydoc Stream::Flush() */
 					/** @copydoc Stream::Flush() */
 	void			flush();
 	void			flush();
 					/** @copydoc Stream::EndOfStream() */
 					/** @copydoc Stream::EndOfStream() */
@@ -87,10 +92,10 @@ protected:
 	File*			m_file;
 	File*			m_file;
 	bool			m_last_was_read;
 	bool			m_last_was_read;
 
 
-	inline void check_valid() const
-	{
-		assert(m_file != NULL);
-	}
+	inline void		check_valid() const
+					{
+						assert(m_file != NULL);
+					}
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 25 - 20
src/core/streams/MemoryStream.cpp

@@ -103,28 +103,33 @@ MemoryStream::~MemoryStream()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void MemoryStream::seek(int32_t position, SeekMode mode)
+void MemoryStream::seek(size_t position)
 {
 {
 	check_valid();
 	check_valid();
 	
 	
-	switch (mode)
-	{
-		case SM_FROM_BEGIN:
-			m_memory_offset = position;
-			break;
-		case SM_FROM_CURRENT:
-			m_memory_offset += position;
-			break;
-		case SM_FROM_END:
-			m_memory_offset = m_memory->size()-1;
-			break;
-	}
+	m_memory_offset = position;
 
 
 	//Allow seek to m_memory->getSize() position, that means end of stream, reading not allowed but you can write if it's dynamic
 	//Allow seek to m_memory->getSize() position, that means end of stream, reading not allowed but you can write if it's dynamic
-	if (m_memory_offset > m_memory->size())
-	{
-		Log::E("Seek beyond the end of stream.");
-	}
+	assert(m_memory_offset <= m_memory->size());
+}
+
+//-----------------------------------------------------------------------------
+void MemoryStream::seek_to_end()
+{
+	check_valid();
+
+	m_memory_offset = m_memory->size() - 1;
+}
+
+//-----------------------------------------------------------------------------
+void MemoryStream::skip(size_t bytes)
+{
+	check_valid();
+
+	m_memory_offset += bytes;
+
+	//Allow seek to m_memory->getSize() position, that means end of stream, reading not allowed but you can write if it's dynamic
+	assert(m_memory_offset <= m_memory->size());
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -141,7 +146,7 @@ uint8_t MemoryStream::read_byte()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void MemoryStream::read_data_block(void* buffer, size_t size)
+void MemoryStream::read(void* buffer, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 	uint8_t* src = m_memory->data();
 	uint8_t* src = m_memory->data();
@@ -165,7 +170,7 @@ bool MemoryStream::copy_to(Stream* stream, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 
 
-	stream->write_data_block(&(m_memory->data()[m_memory_offset]), math::min(m_memory->size()-m_memory_offset, size));
+	stream->write(&(m_memory->data()[m_memory_offset]), math::min(m_memory->size()-m_memory_offset, size));
 
 
 	return true;
 	return true;
 }
 }
@@ -179,7 +184,7 @@ void MemoryStream::write_byte(uint8_t val)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void MemoryStream::write_data_block(const void* buffer, size_t size)
+void MemoryStream::write(const void* buffer, size_t size)
 {
 {
 	check_valid();
 	check_valid();
 	m_memory->write((uint8_t*)buffer, m_memory_offset, size);
 	m_memory->write((uint8_t*)buffer, m_memory_offset, size);

+ 9 - 5
src/core/streams/MemoryStream.h

@@ -93,14 +93,18 @@ public:
 						MemoryStream(MemoryBuffer* buffer, StreamOpenMode mode);
 						MemoryStream(MemoryBuffer* buffer, StreamOpenMode mode);
 	virtual				~MemoryStream();
 	virtual				~MemoryStream();
 
 
-	void				seek(int32_t position, SeekMode mode);
+
+	void				seek(size_t position);
+	void				seek_to_end();
+	void				skip(size_t bytes);
 
 
 	uint8_t				read_byte();
 	uint8_t				read_byte();
-	void				read_data_block(void* buffer, size_t size);
+	void				read(void* buffer, size_t size);
+	void				write_byte(uint8_t val);
+	void				write(const void* buffer, size_t size);
+
 	bool				copy_to(Stream* stream, size_t size = 0);
 	bool				copy_to(Stream* stream, size_t size = 0);
 
 
-	void				write_byte(uint8_t val);
-	void				write_data_block(const void* buffer, size_t size);
 	void				flush();
 	void				flush();
 
 
 	bool				end_of_stream() const { return size() == m_memory_offset; }
 	bool				end_of_stream() const { return size() == m_memory_offset; }
@@ -117,7 +121,7 @@ public:
 
 
 protected:
 protected:
 
 
-	inline void check_valid() { assert(m_memory != NULL); }
+	inline void			check_valid() { assert(m_memory != NULL); }
 
 
 	MemoryBuffer*		m_memory;
 	MemoryBuffer*		m_memory;
 	size_t				m_memory_offset;
 	size_t				m_memory_offset;

+ 59 - 60
src/core/streams/NullStream.h

@@ -40,84 +40,83 @@ class NullStream: public Stream
 
 
 public:
 public:
 
 
-				/** @copydoc Stream::Stream() */
+				/// @copydoc Stream::Stream()
 				NullStream(StreamOpenMode mode) : Stream(mode) {}
 				NullStream(StreamOpenMode mode) : Stream(mode) {}
-				/** @copydoc Stream::~Stream() */
+				/// @copydoc Stream::~Stream()
 	virtual		~NullStream() {}
 	virtual		~NullStream() {}
 
 
-				/** @copydoc Stream::Seek() */
-	void		seek(int32_t /*position*/, uint8_t /*mode*/) {}
-				/**
-				@copydoc Stream::ReadByte()
-				@note
-					Returns always zero
-				*/
+				/// @copydoc Stream::seek()
+	void		seek(size_t position) {}
+
+				/// @copydoc Stream::seek_to_end()
+	void		seek_to_end() {}
+
+				/// @copydoc Stream::skip()
+	void		skip(size_t bytes) {}
+
+				/// @copydoc Stream::ReadByte()
+				/// @note
+				///	Returns always zero
 	uint8_t		read_byte() { return 0; }
 	uint8_t		read_byte() { return 0; }
-				/**
-				@copydoc Stream::ReadDataBlock()
-				@note
-					Fills buffer with zeroes
-				*/
-	void		read_data_block(void* buffer, size_t size)
+				
+				/// @copydoc Stream::ReadDataBlock()
+				/// @note
+				///	Fills buffer with zeroes
+	void		read(void* buffer, size_t size)
 				{
 				{
 					for (size_t i = 0; i < size; i++)
 					for (size_t i = 0; i < size; i++)
 					{
 					{
 						((uint8_t*)buffer)[i] = 0;
 						((uint8_t*)buffer)[i] = 0;
 					}
 					}
 				}
 				}
-				/**
-				@copydoc Stream::CopyTo()
-				@note
-					Returns always false
-				*/
-	bool		copy_to(Stream* /*stream*/, size_t /*size = 0*/) { return false; }
-				/** @copydoc Stream::WriteByte() */
+
+				/// @copydoc Stream::WriteByte()
 	void		write_byte(uint8_t /*val*/) {};
 	void		write_byte(uint8_t /*val*/) {};
-				/** @copydoc Stream::WriteDataBlock() */
-	void		write_data_block(const void* /*buffer*/, size_t /*size*/) {};
-				/** @copydoc Stream::Flush() */
+
+				/// @copydoc Stream::WriteDataBlock()
+	void		write(const void* /*buffer*/, size_t /*size*/) {};
+
+				/// @copydoc Stream::CopyTo()
+				/// @note
+				///	Returns always false
+	bool		copy_to(Stream* /*stream*/, size_t /*size = 0*/) { return false; }
+
+				/// @copydoc Stream::Flush()
 	void		flush() {};
 	void		flush() {};
-				/**
-				@copydoc Stream::IsValid()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::IsValid()
+				/// @note
+				///	Returns always true
 	bool		is_valid() { return true; }
 	bool		is_valid() { return true; }
-				/**
-				@copydoc Stream::EndOfStream()
-				@note
-					Returns always false
-				*/
+				
+				/// @copydoc Stream::EndOfStream()
+				/// @note
+				///	Returns always false
 	bool		end_of_stream() { return false; }
 	bool		end_of_stream() { return false; }
-				/**
-				@copydoc Stream::GetSize()
-				@note
-					Returns always 0xFFFFFFFF
-				*/
+				
+				/// @copydoc Stream::GetSize()
+				/// @note
+				///	Returns always 0xFFFFFFFF
 	size_t		size() { return ~0; }
 	size_t		size() { return ~0; }
-				/**
-				@copydoc Stream::GetPosition()
-				@note
-					Returns always zero
-				*/
+				
+				/// @copydoc Stream::GetPosition()
+				/// @note
+				///	Returns always zero
 	size_t		position() { return 0; }
 	size_t		position() { return 0; }
-				/**
-				@copydoc Stream::CanRead()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::CanRead()
+				/// @note
+				///	Returns always true
 	bool		can_read() { return true; }
 	bool		can_read() { return true; }
-				/**
-				@copydoc Stream::CanWrite()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::CanWrite()
+				/// @note
+				///	Returns always true
 	bool		can_write() { return true; }
 	bool		can_write() { return true; }
-				/**
-				@copydoc Stream::CanSeek()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::CanSeek()
+				/// @note
+				///	Returns always true
 	bool		can_seek() { return true; }
 	bool		can_seek() { return true; }
 };
 };
 
 

+ 52 - 127
src/core/streams/Stream.cpp

@@ -25,128 +25,48 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #include "Stream.h"
 #include "Stream.h"
 #include "Types.h"
 #include "Types.h"
-#include "zlib.h"
-#include "MathUtils.h"
+#include "Compressor.h"
+#include "MallocAllocator.h"
 
 
 namespace crown
 namespace crown
 {
 {
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-bool Stream::zip_to(Stream* stream, size_t size, size_t& zipped_size)
-{
-	const size_t CHUNK_SIZE = 16384;
-	int32_t ret, flush;
-	unsigned have;
-	z_stream strm;
-	unsigned char in[CHUNK_SIZE];
-	unsigned char out[CHUNK_SIZE];
-
-	strm.zalloc = Z_NULL;
-	strm.zfree = Z_NULL;
-	strm.opaque = Z_NULL;
-	ret = deflateInit(&strm, 6);
-	if (ret != Z_OK)
-		return false;
-
-	size_t bytes_read = 0;
-	do
-	{
-		size_t this_step_bytes = math::min(CHUNK_SIZE, size - bytes_read);
-		read_data_block(in, this_step_bytes);
+bool Stream::compress_to(Stream* stream, size_t size, size_t& zipped_size, Compressor* compressor)
+{
+	assert(stream != NULL);
+	assert(compressor != NULL);
 
 
-		strm.avail_in = this_step_bytes;
-		strm.next_in = in;
+	MallocAllocator allocator;
+	void* in_buffer = (void*)allocator.allocate(size);
 
 
-		flush = (size - bytes_read) <= CHUNK_SIZE ? Z_FINISH : Z_NO_FLUSH;
+	read(in_buffer, size);
 
 
-		do
-		{
-			strm.avail_out = CHUNK_SIZE;
-			strm.next_out = out;
+	void* compressed_buffer = compressor->compress(in_buffer, size, zipped_size);
 
 
-			ret = deflate(&strm, flush);    /* no bad return value */
-			assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+	stream->write(compressed_buffer, zipped_size);
 
 
-			have = CHUNK_SIZE - strm.avail_out;
-			if (have > 0)
-				stream->write_data_block(out, have);
-			
-		} while (strm.avail_out == 0);
-		assert(strm.avail_in == 0);     /* all input will be used */
+	return true;
+}
 
 
-		bytes_read += this_step_bytes;
-		/* done when last data in file processed */
-	} while (flush != Z_FINISH);
-	assert(ret == Z_STREAM_END);        /* stream will be complete */
+//-----------------------------------------------------------------------------
+bool Stream::uncompress_to(Stream* stream, size_t& unzipped_size, Compressor* compressor)
+{
+	assert(stream != NULL);
+	assert(compressor != NULL);
 
 
-	/* clean up and return */
-	(void)deflateEnd(&strm);
+	MallocAllocator allocator;
 
 
-	zipped_size = strm.total_out;
+	size_t stream_size = size();
+	void* in_buffer = (void*)allocator.allocate(stream_size); 
 
 
-	return true;
-}
+	read(in_buffer, stream_size);
 
 
-//-----------------------------------------------------------------------------
-bool Stream::unzip_to(Stream* stream, size_t& /*unzipped_size*/)
-{
-	const size_t CHUNK_SIZE = 16384;
-	int32_t ret;
-	unsigned have;
-	z_stream strm;
-	unsigned char in[CHUNK_SIZE];
-	unsigned char out[CHUNK_SIZE];
-
-	/* allocate inflate state */
-	strm.zalloc = Z_NULL;
-	strm.zfree = Z_NULL;
-	strm.opaque = Z_NULL;
-	strm.avail_in = 0;
-	strm.next_in = Z_NULL;
-	ret = inflateInit(&strm);
-	if (ret != Z_OK)
-			return false;
-
-	size_t size = this->size();
-	size_t bytes_read = 0;
-
-	/* decompress until deflate stream ends or end of file */
-	do
-	{
-		size_t this_step_bytes = math::min(CHUNK_SIZE, size - bytes_read);
-		read_data_block(in, this_step_bytes);
-
-		strm.avail_in = this_step_bytes;
-		strm.next_in = in;
-		if (strm.avail_in == 0)
-				break;
-
-		/* run inflate() on input until output buffer not full */
-		do {
-				strm.avail_out = CHUNK_SIZE;
-				strm.next_out = out;
-				ret = inflate(&strm, Z_NO_FLUSH);
-				assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
-				switch (ret) {
-				case Z_NEED_DICT:
-						ret = Z_DATA_ERROR;     /* and fall through */
-				case Z_DATA_ERROR:
-				case Z_MEM_ERROR:
-						(void)inflateEnd(&strm);
-						return false;
-				}
-				have = CHUNK_SIZE - strm.avail_out;
-				if (have > 0)
-					stream->write_data_block(out, have);
-		} while (strm.avail_out == 0);
-
-		bytes_read += this_step_bytes;
-		/* done when inflate() says it's done */
-	} while (ret != Z_STREAM_END);
-
-	/* clean up and return */
-	(void)inflateEnd(&strm);
-	return ret == Z_STREAM_END;
+	void* uncompressed_buffer = compressor->uncompress(in_buffer, stream_size, unzipped_size);
+
+	stream->write(uncompressed_buffer, unzipped_size);
+
+	return true;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -165,7 +85,7 @@ BinaryReader::~BinaryReader()
 int8_t BinaryReader::read_byte()
 int8_t BinaryReader::read_byte()
 {
 {
 	int8_t buffer;
 	int8_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int8_t));
+	m_stream->read(&buffer, sizeof(int8_t));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -173,7 +93,7 @@ int8_t BinaryReader::read_byte()
 int16_t BinaryReader::read_int16()
 int16_t BinaryReader::read_int16()
 {
 {
 	int16_t buffer;
 	int16_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int16_t));
+	m_stream->read(&buffer, sizeof(int16_t));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -181,7 +101,7 @@ int16_t BinaryReader::read_int16()
 uint16_t BinaryReader::read_uint16()
 uint16_t BinaryReader::read_uint16()
 {
 {
 	uint16_t buffer;
 	uint16_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(uint16_t));
+	m_stream->read(&buffer, sizeof(uint16_t));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -189,7 +109,7 @@ uint16_t BinaryReader::read_uint16()
 int32_t BinaryReader::read_int32()
 int32_t BinaryReader::read_int32()
 {
 {
 	int32_t buffer;
 	int32_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int32_t));
+	m_stream->read(&buffer, sizeof(int32_t));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -197,7 +117,7 @@ int32_t BinaryReader::read_int32()
 uint32_t BinaryReader::read_uint32()
 uint32_t BinaryReader::read_uint32()
 {
 {
 	uint32_t buffer;
 	uint32_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(uint32_t));
+	m_stream->read(&buffer, sizeof(uint32_t));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -205,7 +125,7 @@ uint32_t BinaryReader::read_uint32()
 int64_t BinaryReader::read_int64()
 int64_t BinaryReader::read_int64()
 {
 {
 	int64_t buffer;
 	int64_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int64_t));
+	m_stream->read(&buffer, sizeof(int64_t));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -213,14 +133,14 @@ int64_t BinaryReader::read_int64()
 double BinaryReader::read_double()
 double BinaryReader::read_double()
 {
 {
 	double buffer;
 	double buffer;
-	m_stream->read_data_block(&buffer, sizeof(double));
+	m_stream->read(&buffer, sizeof(double));
 	return buffer;
 	return buffer;
 }
 }
 
 
 float BinaryReader::read_float()
 float BinaryReader::read_float()
 {
 {
 	float buffer;
 	float buffer;
-	m_stream->read_data_block(&buffer, sizeof(float));
+	m_stream->read(&buffer, sizeof(float));
 	return buffer;
 	return buffer;
 }
 }
 
 
@@ -239,49 +159,49 @@ BinaryWriter::~BinaryWriter()
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_byte(int8_t buffer)
 void BinaryWriter::write_byte(int8_t buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(int8_t));
+	m_stream->write(&buffer, sizeof(int8_t));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_int16(int16_t buffer)
 void BinaryWriter::write_int16(int16_t buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(int16_t));
+	m_stream->write(&buffer, sizeof(int16_t));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_uint16(uint16_t buffer)
 void BinaryWriter::write_uint16(uint16_t buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(uint16_t));
+	m_stream->write(&buffer, sizeof(uint16_t));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_int32(int32_t buffer)
 void BinaryWriter::write_int32(int32_t buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(int32_t));
+	m_stream->write(&buffer, sizeof(int32_t));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_uint32(uint32_t buffer)
 void BinaryWriter::write_uint32(uint32_t buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(uint32_t));
+	m_stream->write(&buffer, sizeof(uint32_t));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_int64(int64_t buffer)
 void BinaryWriter::write_int64(int64_t buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(int64_t));
+	m_stream->write(&buffer, sizeof(int64_t));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_double(double buffer)
 void BinaryWriter::write_double(double buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(double));
+	m_stream->write(&buffer, sizeof(double));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_float(float buffer)
 void BinaryWriter::write_float(float buffer)
 {
 {
-	m_stream->write_data_block(&buffer, sizeof(float));
+	m_stream->write(&buffer, sizeof(float));
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -289,11 +209,16 @@ void BinaryWriter::insert_byte(int8_t val, size_t offset)
 {
 {
 	size_t tmpSize = m_stream->size() - offset;
 	size_t tmpSize = m_stream->size() - offset;
 	int8_t* tmp = new int8_t[tmpSize];
 	int8_t* tmp = new int8_t[tmpSize];
-	m_stream->seek(offset, SM_FROM_BEGIN);
-	m_stream->read_data_block(tmp, tmpSize);
-	m_stream->seek(offset, SM_FROM_BEGIN);
+
+	m_stream->seek(offset);
+	m_stream->read(tmp, tmpSize);
+
+	m_stream->seek(offset);
+
 	m_stream->write_byte(val);
 	m_stream->write_byte(val);
-	m_stream->write_data_block(tmp, tmpSize);
+
+	m_stream->write(tmp, tmpSize);
+
 	delete[] tmp;
 	delete[] tmp;
 }
 }
 
 

+ 70 - 90
src/core/streams/Stream.h

@@ -30,113 +30,93 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 namespace crown
 {
 {
 
 
-enum SeekMode
-{
-	SM_FROM_BEGIN	= 0,
-	SM_FROM_CURRENT	= 1,
-	SM_FROM_END		= 2
-};
-
 enum StreamOpenMode
 enum StreamOpenMode
 {
 {
 	SOM_READ		= 1,
 	SOM_READ		= 1,
 	SOM_WRITE		= 2
 	SOM_WRITE		= 2
 };
 };
 
 
-/**
-	An abstraction to access data streams.
+class Compressor;
 
 
-	It represents a flow of data attached to a 'file' which can be an archived file,
-	a regular file, a location in memory or anything that can be read or wrote.
-	A Stream is an abstraction to interact with these in an uniform way; every stream
-	comes with a convenient set of methods to facilitate reading from it, writing to
-	it and so on.
-*/
+/// An abstraction to access data streams.
+/// 
+/// It represents a flow of data attached to a 'file' which can be an archived file,
+/// a regular file, a location in memory or anything that can be read or wrote.
+/// A Stream is an abstraction to interact with these in an uniform way; every stream
+/// comes with a convenient set of methods to facilitate reading from it, writing to
+/// it and so on.
 class Stream
 class Stream
 {
 {
 public:
 public:
 
 
-						/**
-							Constructor.
-						*/
+						/// Constructor
 						Stream(StreamOpenMode mode) : m_open_mode(mode) {}
 						Stream(StreamOpenMode mode) : m_open_mode(mode) {}
-						/**
-							Destructor.
-						*/
+
+						/// Destructor
 	virtual				~Stream() {};
 	virtual				~Stream() {};
-						/**
-							Sets the position indicator of the stream to position.
-						*/
-	virtual void		seek(int32_t position, SeekMode mode) = 0;
-						/**
-							Reads a byte from the stream starting at current position.
-						*/
+
+						/// Sets the position indicator of the stream to position.
+	virtual void		seek(size_t position) = 0;
+
+						/// Sets the position indicator to the end of the stream
+	virtual void		seek_to_end() = 0;
+
+						/// Sets the position indicator to bytes after current position
+	virtual void		skip(size_t bytes) = 0;
+
+						/// Reads a byte from the stream starting at current position.
 	virtual uint8_t		read_byte() = 0;
 	virtual uint8_t		read_byte() = 0;
-						/**
-							Reads a block of data from the stream.
-						*/
-	virtual void		read_data_block(void* buffer, size_t size) = 0;
-						/**
-							Copies a chunk of 'size' bytes of data from this to another stream.
-						*/
-	virtual bool		copy_to(Stream* stream, size_t size = 0) = 0;
-						/**
-							Zips a chunk of 'size' bytes of data from this to another stream. A default implementation is given
-						*/
-	virtual bool		zip_to(Stream* stream, size_t size, size_t& zipped_size);
-						/**
-							Unzip a zipped stream of data from this to another stream. A default implementation is given
-						*/
-	virtual bool		unzip_to(Stream* stream, size_t& unzipped_size);
-						/**
-							Writes a byte to the stream starting at current position.
-						*/
+
+						/// Reads a block of data from the stream.
+	virtual void		read(void* buffer, size_t size) = 0;
+
+						/// Writes a byte to the stream starting at current position.
 	virtual void		write_byte(uint8_t val) = 0;
 	virtual void		write_byte(uint8_t val) = 0;
-						/**
-							Writes a block of data to the stream.
-						*/
-	virtual void		write_data_block(const void* buffer, size_t size) = 0;
-						/**
-							Forces the previouses write operations to complete.
-							Generally, when a Stream is attached to a file,
-							write operations are not performed instantly, the output data
-							may be stored to a temporary buffer before making its way to
-							the file. This method forces all the pending output operations
-							to be written to the stream.
-						*/
+
+						/// Writes a block of data to the stream.
+	virtual void		write(const void* buffer, size_t size) = 0;
+
+						/// Copies a chunk of 'size' bytes of data from this to another stream.
+	virtual bool		copy_to(Stream* stream, size_t size = 0) = 0;
+
+						/// Zips a chunk of 'size' bytes of data from this to another stream using compressor.
+	virtual bool		compress_to(Stream* stream, size_t size, size_t& compressed_size, Compressor* compressor);
+
+						/// Unzip a zipped stream of data from this to another stream using compressor.
+	virtual bool		uncompress_to(Stream* stream, size_t& uncompressed_size, Compressor* compressor);
+
+						/// Forces the previouses write operations to complete.
+						/// Generally, when a Stream is attached to a file,
+						/// write operations are not performed instantly, the output data
+						/// may be stored to a temporary buffer before making its way to
+						/// the file. This method forces all the pending output operations
+						/// to be written to the stream.
 	virtual void		flush() = 0;
 	virtual void		flush() = 0;
-						/**
-							Returns whether the stream is valid.
-							A stream is valid when the buffer where it operates
-							exists. (i.e. a file descriptor is attached to the stream, 
-							a memory area is attached to the stream etc.)
-						*/
+
+						/// Returns whether the stream is valid.
+						/// A stream is valid when the buffer where it operates
+						/// exists. (i.e. a file descriptor is attached to the stream, 
+						/// a memory area is attached to the stream etc.)
 	virtual bool		is_valid() const = 0;
 	virtual bool		is_valid() const = 0;
-						/**
-							Returns whether the position is at end of stream.
-						*/
+
+						/// Returns whether the position is at end of stream.
 	virtual bool		end_of_stream() const = 0;
 	virtual bool		end_of_stream() const = 0;
-						/**
-							Returns the size of stream in bytes.
-						*/
+
+						/// Returns the size of stream in bytes.
 	virtual size_t		size() const = 0;
 	virtual size_t		size() const = 0;
-						/**
-							Returns the current position in stream.
-							Generally, for binary data, it means the number of bytes
-							from the beginning of the stream.
-						*/
+
+						/// Returns the current position in stream.
+						/// Generally, for binary data, it means the number of bytes
+						/// from the beginning of the stream.
 	virtual size_t		position() const = 0;
 	virtual size_t		position() const = 0;
-						/**
-							Returns whether the stream can be read.
-						*/
+
+						/// Returns whether the stream can be read.
 	virtual bool		can_read() const = 0;
 	virtual bool		can_read() const = 0;
-						/**
-							Returns whether the stream can be wrote.
-						*/
+
+						/// Returns whether the stream can be wrote.
 	virtual bool		can_write() const = 0;
 	virtual bool		can_write() const = 0;
-						/**
-							Returns whether the stream can be sought.
-						*/
+
+						/// Returns whether the stream can be sought.
 	virtual bool		can_seek() const = 0;
 	virtual bool		can_seek() const = 0;
 
 
 protected:
 protected:
@@ -144,7 +124,7 @@ protected:
 	StreamOpenMode		m_open_mode;
 	StreamOpenMode		m_open_mode;
 };
 };
 
 
-//! A reader that offers a convenient way to read from a stream
+///! A reader that offers a convenient way to read from a stream
 class BinaryReader
 class BinaryReader
 {
 {
 
 
@@ -170,7 +150,7 @@ private:
 	Stream*				m_stream;
 	Stream*				m_stream;
 };
 };
 
 
-//! A writer that offers a convenient way to write to a stream
+///! A writer that offers a convenient way to write to a stream
 class BinaryWriter
 class BinaryWriter
 {
 {
 
 
@@ -198,7 +178,7 @@ private:
 	Stream*				m_stream;
 	Stream*				m_stream;
 };
 };
 
 
-//! A reader that offers a convenient way to read text to a stream
+///! A reader that offers a convenient way to read text to a stream
 class TextReader
 class TextReader
 {
 {
 
 
@@ -228,7 +208,7 @@ private:
 	Stream*				m_stream;
 	Stream*				m_stream;
 };
 };
 
 
-//! A reader that offers a convenient way to write text to a stream
+///! A reader that offers a convenient way to write text to a stream
 class TextWriter
 class TextWriter
 {
 {
 
 
@@ -254,5 +234,5 @@ private:
 	Stream*				m_stream;
 	Stream*				m_stream;
 };
 };
 
 
-} // namespace crown
+} /// namespace crown
 
 

+ 8 - 8
src/loaders/BMPImageLoader.cpp

@@ -92,7 +92,7 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 		return NULL;
 		return NULL;
 	}
 	}
 
 
-	fileStream->read_data_block(&magicNumber, 2);
+	fileStream->read(&magicNumber, 2);
 
 
 	if (magicNumber != 19778)
 	if (magicNumber != 19778)
 	{
 	{
@@ -102,9 +102,9 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 		return NULL;
 		return NULL;
 	}
 	}
 
 
-	fileStream->seek(0, SM_FROM_BEGIN);
-	fileStream->read_data_block(&bfh, sizeof(BitmapFileHeader));
-	fileStream->read_data_block(&bih, sizeof(BitmapInfoHeader));
+	fileStream->seek(0);
+	fileStream->read(&bfh, sizeof(BitmapFileHeader));
+	fileStream->read(&bih, sizeof(BitmapInfoHeader));
 
 
 	int32_t bpp = (bih.biBitCount/8);
 	int32_t bpp = (bih.biBitCount/8);
 
 
@@ -132,7 +132,7 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 
 
 		for (int32_t i=0; i<bih.biHeight ; i++)
 		for (int32_t i=0; i<bih.biHeight ; i++)
 		{
 		{
-			fileStream->read_data_block(tmpdata, bih.biWidth*3);
+			fileStream->read(tmpdata, bih.biWidth*3);
 
 
 			int32_t offset = bih.biWidth * 4 * i;
 			int32_t offset = bih.biWidth * 4 * i;
 
 
@@ -146,7 +146,7 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 
 
 			if (padSize)
 			if (padSize)
 			{
 			{
-				fileStream->seek(padSize, SM_FROM_CURRENT);
+				fileStream->skip(padSize);
 			}
 			}
 		}
 		}
 
 
@@ -158,11 +158,11 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 		{
 		{
 			int32_t offset = bih.biWidth * 4 * i;
 			int32_t offset = bih.biWidth * 4 * i;
 
 
-			fileStream->read_data_block(&data[offset], bih.biWidth*4);
+			fileStream->read(&data[offset], bih.biWidth*4);
 
 
 			if (padSize)
 			if (padSize)
 			{
 			{
-				fileStream->seek(padSize, SM_FROM_CURRENT);
+				fileStream->skip(padSize);
 			}
 			}
 		}
 		}
 	}
 	}

+ 9 - 9
src/loaders/TGAImageLoader.cpp

@@ -52,9 +52,9 @@ Image* TGAImageLoader::LoadFile(const char* relativePath)
 		return NULL;
 		return NULL;
 	}
 	}
 
 
-	fileStream->read_data_block(&mTGAHeader, sizeof(mTGAHeader));
+	fileStream->read(&mTGAHeader, sizeof(mTGAHeader));
 	// Skip ID
 	// Skip ID
-	fileStream->seek(mTGAHeader.id_length, SM_FROM_CURRENT);
+	fileStream->skip(mTGAHeader.id_length);
 
 
 	Image* image = NULL;
 	Image* image = NULL;
 
 
@@ -97,7 +97,7 @@ Image* TGAImageLoader::LoadUncompressedData(Stream* fp)
 		for (uint64_t i = 0; i < size * channels; i++)
 		for (uint64_t i = 0; i < size * channels; i++)
 		{
 		{
 			uint16_t pixel_data;
 			uint16_t pixel_data;
-			fp->read_data_block(&pixel_data, sizeof(pixel_data));
+			fp->read(&pixel_data, sizeof(pixel_data));
 			data[j] = (pixel_data & 0x7c) >> 10;
 			data[j] = (pixel_data & 0x7c) >> 10;
 			data[j+1] = (pixel_data & 0x3e) >> 5;
 			data[j+1] = (pixel_data & 0x3e) >> 5;
 			data[j+2] = (pixel_data & 0x1f);
 			data[j+2] = (pixel_data & 0x1f);
@@ -107,7 +107,7 @@ Image* TGAImageLoader::LoadUncompressedData(Stream* fp)
 	else
 	else
 	{
 	{
 		data = new uint8_t[(uint32_t)(size * channels)];
 		data = new uint8_t[(uint32_t)(size * channels)];
-		fp->read_data_block(data, (size_t)(size * channels));
+		fp->read(data, (size_t)(size * channels));
 		SwapRedBlue(data, size * channels, channels);
 		SwapRedBlue(data, size * channels, channels);
 	}
 	}
 
 
@@ -140,12 +140,12 @@ Image* TGAImageLoader::LoadCompressedData(Stream* fp)
 
 
 	while (i < size)
 	while (i < size)
 	{
 	{
-		fp->read_data_block(&rle_id, sizeof(uint8_t));
+		fp->read(&rle_id, sizeof(uint8_t));
 
 
 		if (rle_id & 0x80)   // Se il bit più significativo è ad 1
 		if (rle_id & 0x80)   // Se il bit più significativo è ad 1
 		{
 		{
 			rle_id -= 127;
 			rle_id -= 127;
-			fp->read_data_block(colors, channels);
+			fp->read(colors, channels);
 
 
 			while (rle_id)
 			while (rle_id)
 			{
 			{
@@ -169,7 +169,7 @@ Image* TGAImageLoader::LoadCompressedData(Stream* fp)
 
 
 			while (rle_id)
 			while (rle_id)
 			{
 			{
-				fp->read_data_block(colors, channels);
+				fp->read(colors, channels);
 				data[colors_read] = colors[2];
 				data[colors_read] = colors[2];
 				data[colors_read+1] = colors[1];
 				data[colors_read+1] = colors[1];
 				data[colors_read+2] = colors[0];
 				data[colors_read+2] = colors[0];
@@ -230,8 +230,8 @@ void TGAImageLoader::SaveFile(const Image* image, const char* relativePath)
 
 
 	if (fileStream)
 	if (fileStream)
 	{
 	{
-		fileStream->write_data_block(&header, sizeof(header));
-		fileStream->write_data_block(image->GetBuffer(), image->GetWidth() * image->GetHeight() * image->GetBytesPerPixel());
+		fileStream->write(&header, sizeof(header));
+		fileStream->write(image->GetBuffer(), image->GetWidth() * image->GetHeight() * image->GetBytesPerPixel());
 
 
 		GetFilesystem()->Close(fileStream);
 		GetFilesystem()->Close(fileStream);
 	}
 	}

+ 159 - 46
src/network/AsyncConnection.cpp

@@ -1,31 +1,32 @@
-#include "Connection.h"
+#include <algorithm>
+#include "AsyncConnection.h"
 
 
 namespace crown
 namespace crown
 {
 {
 namespace network
 namespace network
 {
 {
 
 
-Connection::Connection(Allocator& allocator) :
- 	m_reliable_send(allocator),
- 	m_reliable_receive(allocator)
+AsyncConnection::AsyncConnection(Allocator& allocator) :
+ 	m_sent_msg(allocator),
+ 	m_received_msg(allocator),
+ 	m_pending_ack(allocator),
+ 	m_acked(allocator)
 {
 {
   
   
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-Connection::~Connection()
+AsyncConnection::~AsyncConnection()
 {
 {
   
   
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::init(const os::NetAddress addr, const int id)
+void AsyncConnection::init(const os::NetAddress addr, const int id)
 {
 {
 	m_remote_address = addr;
 	m_remote_address = addr;
 	m_id = id;
 	m_id = id;
 	m_max_rate = 64000;
 	m_max_rate = 64000;
-	m_last_send_time = 0;
-	m_last_data_bytes = 0;
 	m_outgoing_rate_time = 0;
 	m_outgoing_rate_time = 0;
 	m_outgoing_rate_bytes = 0;
 	m_outgoing_rate_bytes = 0;
 	m_incoming_rate_time = 0;
 	m_incoming_rate_time = 0;
@@ -33,12 +34,22 @@ void Connection::init(const os::NetAddress addr, const int id)
 	m_incoming_recv_packets = 0.0f;
 	m_incoming_recv_packets = 0.0f;
 	m_incoming_dropped_packets = 0.0f;
 	m_incoming_dropped_packets = 0.0f;
 	m_incoming_packet_loss_time = 0;
 	m_incoming_packet_loss_time = 0;
-	m_outgoing_sequence = 0;
-	m_incoming_sequence = 0;
+	m_outgoing_sent_packet = 0;
+	m_remote_sequence = 0;
+	m_local_sequence = 0;
+	m_max_sequence = 0xFFFFFFFF;
+	m_max_rtt = 1;	//in seconds
+	m_rtt = 0;		
+	m_last_send_time = 0;
+	m_last_data_bytes = 0;
+	
+	// open port
+	m_socket.open(addr.get_port());
+	assert(m_socket.is_open());
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::reset_rate()
+void AsyncConnection::reset_rate()
 {
 {
   	m_last_send_time = 0;
   	m_last_send_time = 0;
 	m_last_data_bytes = 0;
 	m_last_data_bytes = 0;
@@ -49,37 +60,37 @@ void Connection::reset_rate()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::set_max_outgoing_rate(int rate)
+void AsyncConnection::set_max_outgoing_rate(int rate)
 {
 {
 	m_max_rate = rate;
 	m_max_rate = rate;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int Connection::get_max_outgoing_rate()
+int AsyncConnection::get_max_outgoing_rate()
 {
 {
 	return m_max_rate;
 	return m_max_rate;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-os::NetAddress Connection::get_remote_address() const
+os::NetAddress AsyncConnection::get_remote_address() const
 {
 {
 	return m_remote_address;
 	return m_remote_address;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int Connection::get_outgoing_rate() const
+int AsyncConnection::get_outgoing_rate() const
 {
 {
 	return m_outgoing_rate_bytes;
 	return m_outgoing_rate_bytes;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int Connection::get_incoming_rate() const
+int AsyncConnection::get_incoming_rate() const
 {
 {
 	return m_incoming_rate_bytes;
 	return m_incoming_rate_bytes;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-float Connection::get_incoming_packet_loss() const
+float AsyncConnection::get_incoming_packet_loss() const
 {
 {
 	if (m_incoming_recv_packets == 0 && m_incoming_dropped_packets == 0)
 	if (m_incoming_recv_packets == 0 && m_incoming_dropped_packets == 0)
 	{
 	{
@@ -90,7 +101,28 @@ float Connection::get_incoming_packet_loss() const
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-bool Connection::ready_to_send(const int time) const
+void AsyncConnection::send_message(BitMessage& msg, const uint32_t time)
+{
+	m_socket.send(m_remote_address, msg.get_data(), msg.get_size());
+	//TODO
+}
+
+//-----------------------------------------------------------------------------
+bool AsyncConnection::receive_message(BitMessage& msg, const uint32_t time)
+{
+	m_socket.receive(m_remote_address, msg.get_data(), msg.get_size());
+	//TODO
+}
+
+//-----------------------------------------------------------------------------
+void AsyncConnection::clear_reliable_messages()
+{
+	m_sent_msg.clear();
+	m_received_msg.clear();
+}
+
+//-----------------------------------------------------------------------------
+bool AsyncConnection::ready_to_send(const int time) const
 {
 {
 	// if max rate isn't set, send message
 	// if max rate isn't set, send message
 	if (!m_max_rate)
 	if (!m_max_rate)
@@ -105,62 +137,143 @@ bool Connection::ready_to_send(const int time) const
 	{
 	{
 		return true;
 		return true;
 	}
 	}
-	
 	// if last message wasn't sent, sent it!
 	// if last message wasn't sent, sent it!
 	return ((m_last_data_bytes - ((delta_time * m_max_rate) / 1000)) <= 0);
 	return ((m_last_data_bytes - ((delta_time * m_max_rate) / 1000)) <= 0);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-/* 
-Processes the incoming message. Returns true when a complete message
-is ready for further processing. In that case the read pointer of msg
-points to the first byte ready for reading, and sequence is set to
-the sequence number of the message.
-*/
-bool Connection::process(const os::NetAddress from, int time, BitMessage &msg, int &sequence)
+bool AsyncConnection::process(const os::NetAddress from, int time, BitMessage &msg, int &sequence)
 {
 {
 
 
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::send_reliable_message(const BitMessage& msg)
+void AsyncConnection::_packet_sent(size_t size)
 {
 {
-	uint32_t id = msg.read_int32();
+	bool seq_exists_in_sent_queue = false;
+	bool seq_exists_in_pending_ack_queue = false;
+  
+	BitMessage::Header tmp;
+	BitMessage::Header* h_ptr;
+	
+	tmp.sequence = m_local_sequence;
 
 
-	msg.begin_reading();
+	// If local_sequence_number is already in sent_queue
+	h_ptr = std::find(m_sent_msg.begin(), m_sent_msg.end(), tmp);
+	if (h_ptr != m_sent_msg.end())
+	{
+		seq_exists_in_sent_queue = true;
+	}
+	// If local_sequence_number is already in pending_ack_queue
+	h_ptr = std::find(m_pending_ack.begin(), m_pending_ack.end(), tmp);
+	if(h_ptr != m_pending_ack.end())
+	{
+		seq_exists_in_pending_ack_queue = true;
+	}	
+	// Else
+	assert(!seq_exists_in_sent_queue);
+ 	assert(!seq_exists_in_pending_ack_queue);
+	
+	// Creates Header for saving in queues
+	BitMessage::Header header;
+	header.sequence = m_local_sequence;
+	header.time = 0.0f;
+	header.size = size;
+	// Push packet data in sent_queue
+	m_sent_msg.push_back(header);
+	// push packet data in pending_ack_queue
+	m_pending_ack.push_back(header);
+	// Increments sent packet
+	m_outgoing_sent_packet++;
+	// Increments local sequence
+	m_local_sequence++;
 
 
-	if (id == m_id)
+	if (m_local_sequence > m_max_sequence)
 	{
 	{
-		m_reliable_send.push_back(msg);
+		m_local_sequence = 0;
 	}
 	}
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-bool Connection::receive_reliable_message(BitMessage& msg)
+void AsyncConnection::_packet_received(uint32_t sequence, size_t size)
 {
 {
-	uint32_t id = msg.read_int32();
+	BitMessage::Header tmp;
+	BitMessage::Header* h_ptr;
+
+	tmp.sequence = sequence;
+
+	// Increment received packets
+	m_incoming_recv_packets++;
 	
 	
-	// check correctness of message id
-	if (id == m_id)	
+	// If packet's sequence exists, return
+	h_ptr = std::find(m_received_msg.begin(), m_received_msg.end(), tmp);
+	if (h_ptr != m_received_msg.end())
 	{
 	{
-		// save message
-		m_reliable_receive.push_back(msg);
-		
-		return true;
+		return;
 	}
 	}
 	
 	
-	return false;
+	BitMessage::Header header;
+	header.sequence = sequence;
+	header.time = 0.0f;
+	header.size = size;
+	// Push packet data in received_queue
+	m_received_msg.push_back(header);
+	// update m_remote_sequence
+	if (_sequence_more_recent(sequence, m_remote_sequence))
+	{
+		m_remote_sequence = sequence;
+	}  
+}
+
+//-----------------------------------------------------------------------------
+bool AsyncConnection::_sequence_more_recent(uint32_t s1, uint32_t s2)
+{
+	return ((s1 > s2) && (s1 - s2 <= m_max_sequence / 2)) || ((s2 > s1) && (s2 - s1<= m_max_sequence / 2 ));
+}
+
+//-----------------------------------------------------------------------------
+uint32_t AsyncConnection::_bit_index_for_sequence(uint32_t seq, uint32_t ack)
+{
+	assert(seq != ack);
+	assert(!_sequence_more_recent(seq, ack));
+	
+	if (seq > ack)
+	{
+		assert(ack < 33);
+		assert(seq <= m_max_sequence);
+		return ack + (m_max_sequence - seq);
+	}
+	else
+	{
+		assert(ack >= 1);
+		assert(seq <= ack - 1);
+		return ack - 1 - seq;
+	}  
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::clear_reliable_messages()
+uint32_t AsyncConnection::_generate_ack_bits(uint32_t ack)
 {
 {
-	m_reliable_send.clear();
-	m_reliable_receive.clear();
+	uint32_t ack_bits = 0;
+	
+	for (BitMessage::Header* i = m_received_msg.begin(); i != m_received_msg.end(); i++)
+	{
+		if (i->sequence == ack || _sequence_more_recent(i->sequence, ack))
+		{
+			break;
+		}
+		
+        uint32_t bit_index = _bit_index_for_sequence(i->sequence, ack);
+		if (bit_index <= 31)
+		{
+			ack_bits |= 1 << bit_index;
+		}
+	}
+	return ack_bits;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::_update_outgoing_rate(const int time, const int size)
+void AsyncConnection::_update_outgoing_rate(const uint32_t time, const size_t size)
 {
 {
 	// update the outgoing rate control variables
 	// update the outgoing rate control variables
 	int delta_time;
 	int delta_time;
@@ -196,7 +309,7 @@ void Connection::_update_outgoing_rate(const int time, const int size)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::_update_incoming_rate(const int time, const int size)
+void AsyncConnection::_update_incoming_rate(const uint32_t time, const size_t size)
 {
 {
 	// update incoming rate variables
 	// update incoming rate variables
 	if (time - m_incoming_rate_time > 1000) 
 	if (time - m_incoming_rate_time > 1000) 
@@ -212,7 +325,7 @@ void Connection::_update_incoming_rate(const int time, const int size)
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Connection::_update_packet_loss(const int time, const int num_recv, const int num_dropped)
+void AsyncConnection::_update_packet_loss(const uint32_t time, const uint32_t num_recv, const uint32_t num_dropped)
 {
 {
 	// update incoming packet loss variables
 	// update incoming packet loss variables
 	if (time - m_incoming_packet_loss_time > 5000) 
 	if (time - m_incoming_packet_loss_time > 5000) 

+ 78 - 53
src/network/AsyncConnection.h

@@ -11,78 +11,103 @@ namespace crown
 namespace network
 namespace network
 {
 {
 	#define DEFAULT_PROTOCOL_ID 0xFFFFFFFF
 	#define DEFAULT_PROTOCOL_ID 0xFFFFFFFF
+	
+	#define MAX_SEQUENCE		0xFFFFFFFF
 	#define MAX_PACKET_LEN		1400
 	#define MAX_PACKET_LEN		1400
 	#define MAX_MESSAGE_SIZE	16384
 	#define MAX_MESSAGE_SIZE	16384
 	#define MAX_QUEUE_SIZE		16384
 	#define MAX_QUEUE_SIZE		16384
 	
 	
- 
-class Connection
+/**
+ * Reliable connection over UDP
+ */
+class AsyncConnection
 {
 {
 public:
 public:
   
   
-							Connection(Allocator& allocator);
-							~Connection();
+									AsyncConnection(Allocator& allocator);
+									~AsyncConnection();
 
 
-	void					init(const os::NetAddress addr, const int32_t id = DEFAULT_PROTOCOL_ID);
-	void					reset_rate();
+	void							init(const os::NetAddress addr, const int32_t id = DEFAULT_PROTOCOL_ID);
+	void							reset_rate();
 
 
-							// Sets the maximum outgoing rate.
-	void					set_max_outgoing_rate(int32_t rate);
-							// Gets the maximum outgoing rate.
-	int32_t					get_max_outgoing_rate();
-							// Returns the address of the entity at the other side of the channel.
-	os::NetAddress			get_remote_address() const;
-							// Returns the average outgoing rate over the last second.
-	int32_t					get_outgoing_rate() const;
-							// Returns the average incoming rate over the last second.
-	int32_t					get_incoming_rate() const;
-							// Returns the average incoming packet loss over the last 5 seconds.
-	real					get_incoming_packet_loss() const;		
-							// Returns true if the channel is ready to send new data based on the maximum rate.
-	bool					ready_to_send(const int32_t time) const;
-							// Processes the incoming message.
-	bool					process(const os::NetAddress from, int32_t time, BitMessage &msg, int32_t &sequence);
-							// Sends a reliable message, in order and without duplicates.
-	void					send_reliable_message(const BitMessage &msg);
-							// Returns true if a new reliable message is available and stores the message.
-	bool					receive_reliable_message(BitMessage &msg);
-							// Removes any pending outgoing or incoming reliable messages.
-	void					clear_reliable_messages();
+								  // Sets the maximum outgoing rate.
+	void							set_max_outgoing_rate(int32_t rate);
+									// Gets the maximum outgoing rate.
+	int32_t							get_max_outgoing_rate();
+									// Returns the address of the entity at the other side of the channel.
+	os::NetAddress					get_remote_address() const;
+									// Returns the average outgoing rate over the last second.
+	int32_t							get_outgoing_rate() const;
+									// Returns the average incoming rate over the last second.
+	int32_t							get_incoming_rate() const;
+									// Returns the average incoming packet loss over the last 5 seconds.
+	real							get_incoming_packet_loss() const;		
+									// Sends message
+	void							send_message(BitMessage& msg, const uint32_t time);
+									// Receive message
+	bool							receive_message(BitMessage& msg, const uint32_t time);
+									// Removes any pending outgoing or incoming reliable messages.
+	void							clear_reliable_messages();
+									// Update AsyncConnection
+	void 							update(real delta);
+									// Returns true if the channel is ready to send new data based on the maximum rate.
+	bool							ready_to_send(const int32_t time) const;
+									// Processes the incoming message.
+	bool							process(const os::NetAddress from, int32_t time, BitMessage &msg, int32_t &sequence);
 
 
 private:
 private:
-							// methods which provides a reliability system
-	void					_update_outgoing_rate(const int32_t time, const int32_t size);
-	void					_update_incoming_rate(const int32_t time, const int32_t size);
-	void					_update_packet_loss(const int32_t time, const int32_t num_recv, const int32_t num_dropped);
+
+									 // methods which provides a reliability system
+	void 							_packet_sent(size_t size);
+	void 							_packet_received(uint32_t sequence, size_t size);
+	void 							_process_ack(uint32_t ack, uint32_t ack_bits);
+	bool							_sequence_more_recent(uint32_t s1, uint32_t s2);
+	uint32_t						_bit_index_for_sequence(uint32_t seq, uint32_t ack);
+	uint32_t						_generate_ack_bits(uint32_t ack);
+	void							_update_outgoing_rate(const uint32_t time, const size_t size);
+	void							_update_incoming_rate(const uint32_t time, const size_t size);
+	void							_update_packet_loss(const uint32_t time, const uint32_t num_recv, const uint32_t num_dropped);
+
 
 
 private:
 private:
   
   
-	os::NetAddress			m_remote_address;			// address of remote host
-	int32_t					m_id;						// our identification used instead of port number
-	int32_t					m_max_rate;					// maximum number of bytes that may go out per second
+	os::NetAddress					m_remote_address;			// address of remote host
+	os::UDPSocket					m_socket;					// socket
+	int32_t							m_id;						// our identification used instead of port number
+	int32_t							m_max_rate;					// maximum number of bytes that may go out per second
 
 
-				// variables to control the outgoing rate
-	int32_t					m_last_send_time;			// last time data was sent out
-	int32_t					m_last_data_bytes;			// bytes left to send at last send time
+									// variables to keep track of the rate
+	int32_t							m_outgoing_rate_time;		// outgoing time rate
+	int32_t							m_outgoing_rate_bytes;		// outgoing bytes rate
+	int32_t							m_incoming_rate_time;		// incoming time rate
+	int32_t							m_incoming_rate_bytes;		// incoming bytes rate
 
 
-				// variables to keep track of the rate
-	int32_t					m_outgoing_rate_time;		// outgoing time rate
-	int32_t					m_outgoing_rate_bytes;		// outgoing bytes rate
-	int32_t					m_incoming_rate_time;		// incoming time rate
-	int32_t					m_incoming_rate_bytes;		// incoming bytes rate
+									// variables to keep track of the incoming packet loss
+	real							m_incoming_recv_packets;
+	real							m_incoming_dropped_packets;
+	int32_t							m_incoming_packet_loss_time;
+	
+						
+	int32_t							m_outgoing_sent_packet;		// keep track of sent packet
 
 
-				// variables to keep track of the incoming packet loss
-	real					m_incoming_recv_packets;
-	real					m_incoming_dropped_packets;
-	int32_t						m_incoming_packet_loss_time;
+									// sequencing variables
+	int32_t							m_local_sequence;
+	int32_t							m_remote_sequence;
+	int32_t							m_max_sequence;
+	
+	real							m_max_rtt;					// Maximum round trip time
+	real							m_rtt;						// Round trip time
+	
+									// variables to control the outgoing rate
+	int32_t							m_last_send_time;			// last time data was sent out
+	int32_t							m_last_data_bytes;			// bytes left to send at last send time
 
 
-				// sequencing variables
-	int32_t					m_outgoing_sequence;
-	int32_t					m_incoming_sequence;
+									// reliable messages
+	Queue<BitMessage::Header>		m_sent_msg;					// Sent messages queue
+	Queue<BitMessage::Header>		m_received_msg;				// Received messages queue
+	Queue<BitMessage::Header>		m_pending_ack;				// Pending acknokledges queue
+	Queue<BitMessage::Header>		m_acked;					// Acknowledges queue
 
 
-				// reliable messages
-	Queue<BitMessage>		m_reliable_send;
-	Queue<BitMessage>		m_reliable_receive;
 };
 };
 
 
 } // namespace network
 } // namespace network

+ 86 - 106
src/network/BitMessage.cpp

@@ -9,13 +9,14 @@ namespace network
 {
 {
   
   
 BitMessage::BitMessage(Allocator& allocator) : 
 BitMessage::BitMessage(Allocator& allocator) : 
-	w_data(NULL), 
-	r_data(NULL), 
-	max_size(0), 
-	cur_size(0), 
-	write_bit(0), 
-	read_count(0), 
-	read_bit(0),
+	m_data(NULL),
+	m_write(NULL), 
+	m_read(NULL), 
+	m_max_size(0), 
+	m_cur_size(0), 
+	m_write_bit(0), 
+	m_read_count(0), 
+	m_read_bit(0),
 	m_allocator(&allocator)
 	m_allocator(&allocator)
 {
 {
 }
 }
@@ -23,18 +24,10 @@ BitMessage::BitMessage(Allocator& allocator) :
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 BitMessage::~BitMessage()
 BitMessage::~BitMessage()
-{/*
-  if (w_data)
-  {
-		m_allocator->deallocate((void*)w_data);
-  }
-  */
-  /*
-   * w_data and r_data point the same memory location 
-   */
-  if (r_data)
+{
+  if (m_data)
   {
   {
-		m_allocator->deallocate((void*)r_data);
+		m_allocator->deallocate((void*)m_data);
   }
   }
 }
 }
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
@@ -43,9 +36,9 @@ uint8_t* BitMessage::get_byte_space(int32_t len)
 {
 {
 	uint8_t *ptr;
 	uint8_t *ptr;
 
 
-	if (!w_data) 
+	if (!m_write) 
 	{
 	{
-		printf( "idBitMsg::GetByteSpace: cannot write to message" );
+		printf("cannot write to message");
 	}
 	}
 
 
 	// round up to the next byte
 	// round up to the next byte
@@ -55,8 +48,8 @@ uint8_t* BitMessage::get_byte_space(int32_t len)
 	check_overflow(len << 3);
 	check_overflow(len << 3);
 
 
 	// allocate space
 	// allocate space
-	ptr = w_data + cur_size;
-	cur_size += len;
+	ptr = m_write + m_cur_size;
+	m_cur_size += len;
 	
 	
 	return ptr;  
 	return ptr;  
 }
 }
@@ -69,13 +62,13 @@ bool BitMessage::check_overflow(int32_t num_bits)
 	
 	
 	if (num_bits > get_remaining_write_bits()) 
 	if (num_bits > get_remaining_write_bits()) 
 	{
 	{
-		if (num_bits > (max_size << 3)) 
+		if (num_bits > (m_max_size << 3)) 
 		{
 		{
 			printf(" %i bits is > full message size", num_bits );
 			printf(" %i bits is > full message size", num_bits );
 		}
 		}
 		printf("overflow\n");
 		printf("overflow\n");
 		begin_writing();
 		begin_writing();
-		overflowed = true;
+		m_overflowed = true;
 		return true;
 		return true;
 	}
 	}
 	return false;  
 	return false;  
@@ -83,72 +76,61 @@ bool BitMessage::check_overflow(int32_t num_bits)
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
-void BitMessage::init_in_w(int32_t len)
+void BitMessage::init(int32_t len)
 {
 {
-	uint8_t* data = (uint8_t*)m_allocator->allocate(len);
+	m_data = (uint8_t*)m_allocator->allocate(len);
 	
 	
-	w_data = data;
-	r_data = data;
-	max_size = len;
-}
-
-//---------------------------------------------------------------------------------------------
-
-void BitMessage::init_in_r(int32_t len)
-{
-  	const uint8_t* data = (const uint8_t*)m_allocator->allocate(len);
-
-	w_data = NULL;
-	r_data = data;
-	max_size = len;
+	m_write = m_data;
+	m_read = m_data;
+	m_max_size = len;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 uint8_t* BitMessage::get_data()
 uint8_t* BitMessage::get_data()
 {
 {
-	return w_data;
+	return m_write;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 const uint8_t* BitMessage::get_data() const
 const uint8_t* BitMessage::get_data() const
 {
 {
-	return r_data;
+	return m_read;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
-int32_t BitMessage::get_max_size() const
+size_t BitMessage::get_max_size() const
 {
 {
-	return max_size;
+	return m_max_size;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 bool BitMessage::is_overflowed()
 bool BitMessage::is_overflowed()
 {
 {
-	return overflowed;
+	return m_overflowed;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
-int32_t BitMessage::get_size() const
+size_t BitMessage::get_size() const
 {
 {
-	return cur_size;
+	return m_cur_size;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
-void BitMessage::set_size(int32_t size)
+void BitMessage::set_size(size_t size)
 {
 {
-	if (size > max_size)
+	if (size > m_max_size)
 	{
 	{
-		cur_size = max_size;
+		m_cur_size = m_max_size;
 	}
 	}
 	else
 	else
 	{
 	{
-		cur_size = size;
+		m_cur_size = size;
 	}
 	}
 }
 }
 
 
@@ -156,17 +138,17 @@ void BitMessage::set_size(int32_t size)
 
 
 int32_t BitMessage::get_write_bit() const
 int32_t BitMessage::get_write_bit() const
 {
 {
-	return write_bit;
+	return m_write_bit;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::set_write_bit(int32_t bit)
 void BitMessage::set_write_bit(int32_t bit)
 {
 {
-	write_bit = bit & 7;
-	if (write_bit) 
+	m_write_bit = bit & 7;
+	if (m_write_bit) 
 	{
 	{
-		w_data[cur_size-1] &= (1 << write_bit) - 1;
+		m_write[m_cur_size-1] &= (1 << m_write_bit) - 1;
 	}
 	}
 }
 }
 
 
@@ -174,34 +156,34 @@ void BitMessage::set_write_bit(int32_t bit)
 
 
 int32_t BitMessage::get_num_bits_written() const
 int32_t BitMessage::get_num_bits_written() const
 {
 {
-	return ((cur_size << 3) - ((8 - write_bit) & 7));  
+	return ((m_cur_size << 3) - ((8 - m_write_bit) & 7));  
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 int32_t BitMessage::get_remaining_write_bits() const
 int32_t BitMessage::get_remaining_write_bits() const
 {
 {
-	return (max_size << 3) - get_num_bits_written(); 
+	return (m_max_size << 3) - get_num_bits_written(); 
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::save_write_state(int32_t& s,int32_t& b) const
 void BitMessage::save_write_state(int32_t& s,int32_t& b) const
 {
 {
-	s = cur_size;
-	b = write_bit;
+	s = m_cur_size;
+	b = m_write_bit;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::restore_write_state(int32_t s,int32_t b)
 void BitMessage::restore_write_state(int32_t s,int32_t b)
 {
 {
-	cur_size = s;
-	write_bit = b & 7;
+	m_cur_size = s;
+	m_write_bit = b & 7;
 	
 	
-	if (write_bit) 
+	if (m_write_bit) 
 	{
 	{
-		w_data[cur_size-1] &= (1 << write_bit) - 1;
+		m_write[m_cur_size-1] &= (1 << m_write_bit) - 1;
 	}  
 	}  
 }
 }
 
 
@@ -209,81 +191,81 @@ void BitMessage::restore_write_state(int32_t s,int32_t b)
 
 
 int32_t BitMessage::get_read_count() const
 int32_t BitMessage::get_read_count() const
 {
 {
-	return read_count;
+	return m_read_count;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::set_read_count(int32_t bytes)
 void BitMessage::set_read_count(int32_t bytes)
 {
 {
-	read_count = bytes;
+	m_read_count = bytes;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 int32_t BitMessage::get_read_bit() const
 int32_t BitMessage::get_read_bit() const
 {
 {
-	return read_bit;
+	return m_read_bit;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::set_read_bit(int32_t bit)
 void BitMessage::set_read_bit(int32_t bit)
 {
 {
-	read_bit = bit & 7;
+	m_read_bit = bit & 7;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 int32_t BitMessage::get_num_bits_read() const
 int32_t BitMessage::get_num_bits_read() const
 {
 {
-	return ((read_count << 3) - ((8 - read_bit) & 7));  
+	return ((m_read_count << 3) - ((8 - m_read_bit) & 7));  
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 int32_t BitMessage::get_remaining_read_bits() const
 int32_t BitMessage::get_remaining_read_bits() const
 {
 {
-	return (cur_size << 3) - get_num_bits_read();
+	return (m_cur_size << 3) - get_num_bits_read();
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::save_read_state(int32_t& c, int32_t& b) const
 void BitMessage::save_read_state(int32_t& c, int32_t& b) const
 {
 {
-	c = read_count;
-	b = read_bit;
+	c = m_read_count;
+	b = m_read_bit;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::restore_read_state(int32_t c, int32_t b)
 void BitMessage::restore_read_state(int32_t c, int32_t b)
 {
 {
-	read_count = c;
-	read_bit = b & 7;
+	m_read_count = c;
+	m_read_bit = b & 7;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::begin_writing()
 void BitMessage::begin_writing()
 {
 {
-	cur_size = 0;
-	write_bit = 0;
-	overflowed = false;
+	m_cur_size = 0;
+	m_write_bit = 0;
+	m_overflowed = false;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 int32_t BitMessage::get_remaining_space() const
 int32_t BitMessage::get_remaining_space() const
 {
 {
-	return max_size - cur_size;
+	return m_max_size - m_cur_size;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::write_byte_align()
 void BitMessage::write_byte_align()
 {
 {
-	write_bit = 0;
+	m_write_bit = 0;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
@@ -293,8 +275,8 @@ void BitMessage::write_bits(int32_t value, int32_t num_bits)
 	int32_t		put;
 	int32_t		put;
 	int32_t		fraction;
 	int32_t		fraction;
 
 
-	// check if w_data is void
-	if (!w_data) 
+	// check if m_write is void
+	if (!m_write) 
 	{
 	{
 		printf( "cannot write to message" );
 		printf( "cannot write to message" );
 	}
 	}
@@ -348,23 +330,23 @@ void BitMessage::write_bits(int32_t value, int32_t num_bits)
 	// write the bits
 	// write the bits
 	while(num_bits) 
 	while(num_bits) 
 	{
 	{
-		if (write_bit == 0) 
+		if (m_write_bit == 0) 
 		{
 		{
-			w_data[cur_size] = 0;
-			cur_size++;
+			m_write[m_cur_size] = 0;
+			m_cur_size++;
 		}
 		}
 		
 		
-		put = 8 - write_bit;
+		put = 8 - m_write_bit;
 		if (put > num_bits) 
 		if (put > num_bits) 
 		{
 		{
 			put = num_bits;
 			put = num_bits;
 		}
 		}
 		
 		
 		fraction = value & ((1 << put) - 1);
 		fraction = value & ((1 << put) - 1);
-		w_data[cur_size - 1] |= fraction << write_bit;
+		m_write[m_cur_size - 1] |= fraction << m_write_bit;
 		num_bits -= put;
 		num_bits -= put;
 		value >>= put;
 		value >>= put;
-		write_bit = (write_bit + put) & 7;
+		m_write_bit = (m_write_bit + put) & 7;
 	}
 	}
 }
 }
 
 
@@ -492,22 +474,22 @@ void BitMessage::write_ipv4addr(const os::NetAddress addr)
 
 
 void BitMessage::begin_reading() const
 void BitMessage::begin_reading() const
 {
 {
-	read_count = 0;
-	read_bit = 0;
+	m_read_count = 0;
+	m_read_bit = 0;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 int32_t BitMessage::get_remaing_data() const
 int32_t BitMessage::get_remaing_data() const
 {
 {
-	cur_size - read_count;
+	m_cur_size - m_read_count;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
 
 
 void BitMessage::read_byte_align() const
 void BitMessage::read_byte_align() const
 {
 {
-	read_bit = 0;
+	m_read_bit = 0;
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
@@ -520,7 +502,7 @@ int32_t BitMessage::read_bits(int32_t num_bits) const
 	int32_t		fraction;
 	int32_t		fraction;
 	bool		sgn;
 	bool		sgn;
 
 
-	if (!r_data) 
+	if (!m_read) 
 	{
 	{
 		printf("cannot read from message");
 		printf("cannot read from message");
 	}
 	}
@@ -553,25 +535,25 @@ int32_t BitMessage::read_bits(int32_t num_bits) const
 
 
 	while (value_bits < num_bits) 
 	while (value_bits < num_bits) 
 	{
 	{
-		if (read_bit == 0) 
+		if (m_read_bit == 0) 
 		{
 		{
-			read_count++;
+			m_read_count++;
 		}
 		}
 		
 		
-		get = 8 - read_bit;
+		get = 8 - m_read_bit;
 		
 		
 		if (get > (num_bits - value_bits)) 
 		if (get > (num_bits - value_bits)) 
 		{
 		{
 			get = num_bits - value_bits;
 			get = num_bits - value_bits;
 		}
 		}
 		
 		
-		fraction = r_data[read_count - 1];
-		fraction >>= read_bit;
+		fraction = m_read[m_read_count - 1];
+		fraction >>= m_read_bit;
 		fraction &= (1 << get) - 1;
 		fraction &= (1 << get) - 1;
 		value |= fraction << value_bits;
 		value |= fraction << value_bits;
 
 
 		value_bits += get;
 		value_bits += get;
-		read_bit = (read_bit + get) & 7;
+		m_read_bit = (m_read_bit + get) & 7;
 	}
 	}
 
 
 	if (sgn) 
 	if (sgn) 
@@ -687,26 +669,26 @@ int32_t BitMessage::read_data(void* data, int32_t length) const
 
 
 	read_byte_align();
 	read_byte_align();
 	
 	
-	count = read_count;
+	count = m_read_count;
 
 
-	if (read_count + length > cur_size) 
+	if (m_read_count + length > m_cur_size) 
 	{
 	{
 		if (data) 
 		if (data) 
 		{
 		{
-			memcpy(data, r_data + read_count, get_remaing_data());
+			memcpy(data, m_read + m_read_count, get_remaing_data());
 		}
 		}
-		read_count = cur_size;
+		m_read_count = m_cur_size;
 	} 
 	} 
 	else 
 	else 
 	{
 	{
 		if (data) 
 		if (data) 
 		{
 		{
-			memcpy(data, r_data + read_count, length);
+			memcpy(data, m_read + m_read_count, length);
 		}
 		}
-		read_count += length;
+		m_read_count += length;
 	}
 	}
 
 
-	return (read_count - count);  
+	return (m_read_count - count);  
 }
 }
 
 
 //---------------------------------------------------------------------------------------------
 //---------------------------------------------------------------------------------------------
@@ -722,7 +704,5 @@ void BitMessage::read_ipv4addr(os::NetAddress* addr) const
 	addr->port = read_uint16();  
 	addr->port = read_uint16();  
 }
 }
 
 
-//---------------------------------------------------------------------------------------------
-
 }	//namespace network
 }	//namespace network
 }	//namespace crown
 }	//namespace crown

+ 81 - 69
src/network/BitMessage.h

@@ -12,88 +12,100 @@ namespace network
 	/**
 	/**
 	* bit-packet reliable message
 	* bit-packet reliable message
 	*/
 	*/
-	
+
 	class BitMessage
 	class BitMessage
 	{
 	{
-	public:
-						BitMessage(Allocator& allocator);
-						~BitMessage();
 
 
-		void			init_in_w(int32_t len);
-		void			init_in_r(int32_t len);
-		uint8_t*		get_data();										// get data for writing
-		const uint8_t*	get_data() const;								// get data for reading
-		int32_t			get_max_size() const;							// get the maximum message size
-		bool 			is_overflowed();								// get overflowed flag
+	public:
 
 
-		int32_t			get_size() const;								// size of the message in bytes
-		void			set_size(int32_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
+		struct Header
+		{
+			uint32_t		sequence;
+			uint32_t		time;
+			uint16_t		size;
+			
+			bool operator==(const Header& header)
+			{
+				return this->sequence == header.sequence;
+			}
+		}; 
+		
+	public:
+							BitMessage(Allocator& allocator);
+							~BitMessage();
 
 
-		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
+		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			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);
+		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			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;
+		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
 
 
- 		static int32_t	vec3_to_bits(const Vec3& v);
- 		static Vec3		bits_to_vec3(int32_t 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);
+		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);
 
 
+		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;		
+	
 	private:
 	private:
 	  
 	  
-		uint8_t*			w_data;										// pointer to data for writing
-		const uint8_t*		r_data;										// point32_ter to data for reading
-		int32_t				max_size;									// maximum size of message in bytes
-		int32_t				cur_size;									// current size of message in bytes
-		int32_t				write_bit;									// number of bits written to the last written byte
-		mutable int32_t		read_count;									// number of bytes read so far
-		mutable int32_t		read_bit;									// number of bits read from the last read byte
-		bool 				overflowed;									// overflow flag
+		uint8_t*			get_byte_space(int32_t len);
+		bool 				check_overflow(int32_t num_bits);			// check buffer overflow	
 		
 		
-		Allocator*			m_allocator;								// memory allocator
-
 	private:
 	private:
-	  
-		uint8_t*		get_byte_space(int32_t len);
-		bool 			check_overflow(int32_t num_bits);			// check buffer overflow	
+				  //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
+					
   };
   };
 } // namespace network
 } // namespace network
 } // namespace crown
 } // namespace crown

+ 5 - 5
src/os/OS.h

@@ -146,7 +146,7 @@ struct NetAddress
 		return addr;
 		return addr;
 	}
 	}
 	
 	
-	uint16_t get_port()
+	uint16_t get_port() const
 	{
 	{
 		return port;
 		return port;
 	}
 	}
@@ -185,9 +185,9 @@ public:
 				// Open connection
 				// Open connection
 	bool 		open(uint16_t port);
 	bool 		open(uint16_t port);
 				 // Send data through socket
 				 // Send data through socket
-	bool 		send(NetAddress &receiver, const void* data, int32_t size );
+	bool 		send(NetAddress &receiver, const void* data, size_t size );
 				// Receive data through socket
 				// Receive data through socket
-	int32_t 	receive(NetAddress &sender, void* data, int32_t size);
+	int32_t 	receive(NetAddress &sender, void* data, size_t size);
 				// Close connection
 				// Close connection
 	void 		close();
 	void 		close();
 				// Is connection open?
 				// Is connection open?
@@ -214,9 +214,9 @@ public:
 				// Close connection
 				// Close connection
 	int32_t		close();
 	int32_t		close();
 				// Send data through socket
 				// Send data through socket
-	bool 		send(const void* data, int32_t size);
+	bool 		send(const void* data, size_t size);
 				// Receive data through socket
 				// Receive data through socket
-	int32_t		receive(void* data, int32_t size);
+	int32_t		receive(void* data, size_t size);
 				// Is connection open?
 				// Is connection open?
 	bool 		is_open();
 	bool 		is_open();
 				// Getter method for socket descriptor
 				// Getter method for socket descriptor

+ 2 - 2
src/os/linux/LinuxTCPSocket.cpp

@@ -109,7 +109,7 @@ int32_t	TCPSocket::close()
 	}
 	}
 }
 }
 
 
-bool TCPSocket::send(const void* data, int32_t size)
+bool TCPSocket::send(const void* data, size_t size)
 {
 {
 	assert(data);
 	assert(data);
 	assert(size > 0);
 	assert(size > 0);
@@ -132,7 +132,7 @@ bool TCPSocket::send(const void* data, int32_t size)
 	return true;  
 	return true;  
 }
 }
 
 
-int32_t TCPSocket::receive(void* data, int32_t size)
+int32_t TCPSocket::receive(void* data, size_t size)
 {
 {
 	assert(data);
 	assert(data);
 	assert(size > 0);
 	assert(size > 0);

+ 2 - 2
src/os/linux/LinuxUDPSocket.cpp

@@ -58,7 +58,7 @@ bool UDPSocket::open(uint16_t port)
 		return true;
 		return true;
 }
 }
 
 
-bool UDPSocket::send(NetAddress &receiver, const void* data, int32_t size)
+bool UDPSocket::send(NetAddress &receiver, const void* data, size_t size)
 {
 {
 	assert(data);
 	assert(data);
 	assert(size > 0);
 	assert(size > 0);
@@ -81,7 +81,7 @@ bool UDPSocket::send(NetAddress &receiver, const void* data, int32_t size)
 	return sent_bytes == size;
 	return sent_bytes == size;
 }
 }
 
 
-int32_t UDPSocket::receive(NetAddress &sender, void* data, int32_t size)
+int32_t UDPSocket::receive(NetAddress &sender, void* data, size_t size)
 {
 {
 	assert(data);
 	assert(data);
 	assert(size > 0);
 	assert(size > 0);

+ 1 - 2
tests/compressors.cpp

@@ -43,8 +43,7 @@ int main(int argc, char** argv)
 	printf("\n\n");
 	printf("\n\n");
 	
 	
 	allocator.deallocate(compressed_string);
 	allocator.deallocate(compressed_string);
-  	allocator.deallocate(result); //FIX: invalid pointer -> check header in MallocAllocator*/
+  	allocator.deallocate(result); 
 	
 	
-	delete uncompressed_string;
 	return 0;
 	return 0;
 }
 }

+ 1 - 1
tests/connections.cpp

@@ -1,6 +1,6 @@
 #include <cstdio>
 #include <cstdio>
 
 
-#include "Connection.h"
+#include "AsyncConnection.h"
 
 
 using namespace crown;
 using namespace crown;
 
 

+ 11 - 10
tests/messages.cpp

@@ -19,7 +19,7 @@ void test_int8()
 	
 	
 	int8_t res;
 	int8_t res;
 	
 	
-	m.init_in_w(4);
+	m.init(4);
 	m.begin_writing();
 	m.begin_writing();
 	m.write_int8(-56);
 	m.write_int8(-56);
 	bits_written = m.get_num_bits_written();
 	bits_written = m.get_num_bits_written();
@@ -54,12 +54,13 @@ void test_uint8()
 
 
 	uint8_t res;
 	uint8_t res;
 	
 	
-	m.init_in_w(4);
+	m.init(4);
 	m.begin_writing();
 	m.begin_writing();
 	m.write_uint8(255);
 	m.write_uint8(255);
 	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();
 
 
+	m.begin_reading();
 	res = m.read_uint8();
 	res = m.read_uint8();
 	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();
@@ -88,7 +89,7 @@ void test_int16()
 	
 	
 	int16_t res;
 	int16_t res;
 	
 	
-	m.init_in_w(4);
+	m.init(4);
 	m.write_int16(-5555);
 	m.write_int16(-5555);
 	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();
@@ -120,7 +121,7 @@ void test_uint16()
 
 
 	uint16_t res;
 	uint16_t res;
 	
 	
-	m.init_in_w(4);
+	m.init(4);
 	m.write_uint16(5555);
 	m.write_uint16(5555);
 	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();
@@ -152,7 +153,7 @@ void test_int32()
 	
 	
 	int32_t res;
 	int32_t res;
 	
 	
-	m.init_in_w(4);
+	m.init(4);
 	m.write_int32(4000000);
 	m.write_int32(4000000);
 	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();
@@ -185,7 +186,7 @@ void test_real()
 
 
 	real res;
 	real res;
 	
 	
-	m.init_in_w(4);
+	m.init(4);
 	m.write_real(4.5342f);
 	m.write_real(4.5342f);
 	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();
@@ -221,7 +222,7 @@ void test_vec3()
 	Vec3 v(0.525f, 0.432f, 0.234f);
 	Vec3 v(0.525f, 0.432f, 0.234f);
 	Vec3 res;
 	Vec3 res;
 	
 	
-	m.init_in_w(12);
+	m.init(12);
 	m.write_vec3(v);
 	m.write_vec3(v);
 	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();
@@ -256,7 +257,7 @@ void test_string()
 
 
 	char s[] = "test";
 	char s[] = "test";
 	
 	
-	m.init_in_w(16);
+	m.init(16);
 
 
 	m.write_string(s, sizeof(s), true);
 	m.write_string(s, sizeof(s), true);
  	bits_written = m.get_num_bits_written();
  	bits_written = m.get_num_bits_written();
@@ -291,7 +292,7 @@ void test_data()
 	uint8_t tmp[] = "test generic";
 	uint8_t tmp[] = "test generic";
 	uint8_t res[16];
 	uint8_t res[16];
 	
 	
-	m.init_in_w(16);
+	m.init(16);
 	
 	
 	m.write_data(tmp, 16);
 	m.write_data(tmp, 16);
  	bits_written = m.get_num_bits_written();
  	bits_written = m.get_num_bits_written();
@@ -331,7 +332,7 @@ void test_net_address()
 	
 	
 	addr.set(192, 168, 0, 1, 80);
 	addr.set(192, 168, 0, 1, 80);
 	
 	
-	m.init_in_w(16);
+	m.init(16);
 	m.write_ipv4addr(addr);
 	m.write_ipv4addr(addr);
 	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();