Quellcode durchsuchen

Merge branch 'compilers-2'

Daniele Bartolini vor 12 Jahren
Ursprung
Commit
e114ea323a

+ 1 - 0
src/CMakeLists.txt

@@ -220,6 +220,7 @@ set (RENDERERS_HEADERS
 
 set (OS_HEADERS
 	os/OS.h
+	os/NetAddress.h
 )
 
 set (OS_SRC

+ 1 - 1
src/Filesystem.h

@@ -143,7 +143,7 @@ public:
 
 	/// Closes a previously opened file @stream
 	void				close(FileStream* stream);
-	
+
 private:
 
 	// Builds the OS-dependent path from base_path and relative_path

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

@@ -33,26 +33,16 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 FileStream::FileStream(StreamOpenMode mode, const char* filename) :
-	Stream(mode), m_file(NULL),
+	Stream(mode),
 	m_last_was_read(true)
 {
-	//Takes ownership
-	FileOpenMode file_open_mode = (FileOpenMode)0;
-
-	if (math::test_bitmask(mode, SOM_READ))
-		file_open_mode = (FileOpenMode)(file_open_mode | FOM_READ);
-	if (math::test_bitmask(mode, SOM_WRITE))
-		file_open_mode = (FileOpenMode)(file_open_mode | FOM_WRITE);
-
-	m_file = File::open(filename, file_open_mode);
+	m_file.open(filename, mode);
 }
 
 //-----------------------------------------------------------------------------
 FileStream::~FileStream()
 {
-	delete m_file;
-
-	m_file = 0;
+	//m_file.close();
 }
 
 //-----------------------------------------------------------------------------
@@ -60,7 +50,7 @@ void FileStream::seek(size_t position)
 {
 	check_valid();
 
-	m_file->seek(position, SEEK_SET);
+	m_file.seek(position);
 }
 
 //-----------------------------------------------------------------------------
@@ -68,7 +58,7 @@ void FileStream::seek_to_end()
 {
 	check_valid();
 
-	m_file->seek(0, SEEK_END);
+	m_file.seek_to_end();
 }
 
 //-----------------------------------------------------------------------------
@@ -76,7 +66,7 @@ void FileStream::skip(size_t bytes)
 {
 	check_valid();
 
-	m_file->seek(bytes, SEEK_CUR);
+	m_file.skip(bytes);
 }
 
 //-----------------------------------------------------------------------------
@@ -87,15 +77,12 @@ uint8_t FileStream::read_byte()
 	if (!m_last_was_read)
 	{
 		m_last_was_read = true;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
 	uint8_t buffer;
-	
-	if (m_file->read(&buffer, 1, 1) != 1)
-	{
-		Log::e("Could not read from file");
-	}
+
+	assert(m_file.read(&buffer, 1));
 
 	return buffer;
 }
@@ -108,13 +95,10 @@ void FileStream::read(void* buffer, size_t size)
 	if (!m_last_was_read)
 	{
 		m_last_was_read = true;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
-	if (m_file->read(buffer, size, 1) != 1)
-	{
-		Log::e("Could not read from file.");
-	}
+	assert(m_file.read(buffer, size));
 }
 
 //-----------------------------------------------------------------------------
@@ -131,22 +115,22 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 
 	char* buff = new char[chunksize];
 
-	size_t totReadBytes = 0;
+	size_t tot_read_bytes = 0;
 
-	while (totReadBytes < size)
+	while (tot_read_bytes < size)
 	{
-		int32_t readBytes;
-		int32_t expectedReadBytes = math::min(size - totReadBytes, chunksize);
+		size_t read_bytes;
+		size_t expected_read_bytes = math::min(size - tot_read_bytes, chunksize);
 
-		readBytes = m_file->read(buff, 1, expectedReadBytes);
+		read_bytes = m_file.read(buff, expected_read_bytes);
 
-		if (readBytes < expectedReadBytes)
+		if (read_bytes < expected_read_bytes)
 		{
-			if (m_file->eof())
+			if (m_file.eof())
 			{
-				if (readBytes != 0)
+				if (read_bytes != 0)
 				{
-					stream->write(buff, readBytes);
+					stream->write(buff, read_bytes);
 				}
 			}
 
@@ -155,8 +139,8 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 			return false;
 		}
 
-		stream->write(buff, readBytes);
-		totReadBytes += readBytes;
+		stream->write(buff, read_bytes);
+		tot_read_bytes += read_bytes;
 	}
 
 	delete [] buff;
@@ -172,14 +156,7 @@ bool FileStream::end_of_stream() const
 //-----------------------------------------------------------------------------
 bool FileStream::is_valid() const
 {
-	{
-		if (!m_file)
-		{
-			return false;
-		}
-
-		return m_file->is_valid();
-	}
+	return m_file.is_open();
 }
 
 //-----------------------------------------------------------------------------
@@ -190,13 +167,10 @@ void FileStream::write_byte(uint8_t val)
 	if (m_last_was_read)
 	{
 		m_last_was_read = false;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
-	if (m_file->write(&val, 1, 1) != 1)
-	{
-		Log::e("Could not write to file.");
-	}
+	assert(m_file.write(&val, 1) == 1);
 }
 
 //-----------------------------------------------------------------------------
@@ -207,13 +181,10 @@ void FileStream::write(const void* buffer, size_t size)
 	if (m_last_was_read)
 	{
 		m_last_was_read = false;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
-	if (m_file->write(buffer, size, 1) != 1)
-	{
-		Log::e("Could not write to file.");
-	}
+	assert(m_file.write(buffer, size) == size);
 }
 
 //-----------------------------------------------------------------------------
@@ -229,7 +200,7 @@ size_t FileStream::position() const
 {
 	check_valid();
 
-	return m_file->tell();
+	return m_file.position();
 }
 
 //-----------------------------------------------------------------------------
@@ -237,7 +208,7 @@ size_t FileStream::size() const
 {
 	check_valid();
 	
-	return m_file->size();
+	return m_file.size();
 }
 
 //-----------------------------------------------------------------------------

+ 4 - 3
src/core/streams/FileStream.h

@@ -25,9 +25,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <cassert>
+
 #include "Stream.h"
 #include "File.h"
-#include <cassert>
 
 namespace crown
 {
@@ -93,14 +94,14 @@ public:
 
 protected:
 
-	File*			m_file;
+	File			m_file;
 	bool			m_last_was_read;
 
 protected:
 
 	inline void		check_valid() const
 	{
-		assert(m_file != NULL);
+		assert(m_file.is_open());
 	}
 };
 

+ 39 - 57
src/network/BitMessage.cpp

@@ -1,15 +1,42 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #include <cassert>
 #include <cstring>
 
+#include "NetAddress.h"
 #include "BitMessage.h"
 
 namespace crown
 {
 namespace network
 {
-  
+
+//---------------------------------------------------------------------------------------------
 BitMessage::BitMessage(Allocator& allocator) :
-	m_allocator(&allocator),
+	m_allocator(allocator),
 	m_header(NULL),
 	m_data(NULL),
 	m_write(NULL), 
@@ -25,20 +52,19 @@ BitMessage::BitMessage(Allocator& allocator) :
 }
 
 //---------------------------------------------------------------------------------------------
-
 BitMessage::~BitMessage()
 {
 	if (m_header)
 	{
-		m_allocator->deallocate((void*)m_header);
+		m_allocator.deallocate((void*)m_header);
 	}
 	if (m_data)
 	{
-		  m_allocator->deallocate((void*)m_data);
+		  m_allocator.deallocate((void*)m_data);
 	}
 }
-//---------------------------------------------------------------------------------------------
 
+//---------------------------------------------------------------------------------------------
 uint8_t* BitMessage::get_byte_space(int32_t len)
 {
 	uint8_t *ptr;
@@ -62,7 +88,6 @@ uint8_t* BitMessage::get_byte_space(int32_t len)
 }
 
 //---------------------------------------------------------------------------------------------
-
 bool BitMessage::check_overflow(int32_t num_bits)
 {
 	assert(num_bits >= 0);
@@ -78,15 +103,15 @@ bool BitMessage::check_overflow(int32_t num_bits)
 		m_overflowed = true;
 		return true;
 	}
-	return false;  
+	return false;
 }
 
 //---------------------------------------------------------------------------------------------
 void BitMessage::init(int32_t len)
 {
-	m_header = (uint8_t*)m_allocator->allocate(12);
+	m_header = (uint8_t*)m_allocator.allocate(12);
 	
-	m_data = (uint8_t*)m_allocator->allocate(len);
+	m_data = (uint8_t*)m_allocator.allocate(len);
 	
 	m_write = m_data;
 	m_read = m_data;
@@ -169,7 +194,6 @@ size_t BitMessage::get_size() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_size(size_t size)
 {
 	if (size > m_max_size)
@@ -183,14 +207,12 @@ void BitMessage::set_size(size_t size)
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_write_bit() const
 {
 	return m_write_bit;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_write_bit(int32_t bit)
 {
 	m_write_bit = bit & 7;
@@ -201,21 +223,18 @@ void BitMessage::set_write_bit(int32_t bit)
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_num_bits_written() const
 {
 	return ((m_cur_size << 3) - ((8 - m_write_bit) & 7));  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaining_write_bits() const
 {
 	return (m_max_size << 3) - get_num_bits_written(); 
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::save_write_state(int32_t& s,int32_t& b) const
 {
 	s = m_cur_size;
@@ -223,7 +242,6 @@ void BitMessage::save_write_state(int32_t& s,int32_t& b) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::restore_write_state(int32_t s,int32_t b)
 {
 	m_cur_size = s;
@@ -236,49 +254,42 @@ void BitMessage::restore_write_state(int32_t s,int32_t b)
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_read_count() const
 {
 	return m_read_count;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_read_count(int32_t bytes)
 {
 	m_read_count = bytes;
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_read_bit() const
 {
 	return m_read_bit;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_read_bit(int32_t bit)
 {
 	m_read_bit = bit & 7;
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_num_bits_read() const
 {
 	return ((m_read_count << 3) - ((8 - m_read_bit) & 7));  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaining_read_bits() const
 {
 	return (m_cur_size << 3) - get_num_bits_read();
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::save_read_state(int32_t& c, int32_t& b) const
 {
 	c = m_read_count;
@@ -286,7 +297,6 @@ void BitMessage::save_read_state(int32_t& c, int32_t& b) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::restore_read_state(int32_t c, int32_t b)
 {
 	m_read_count = c;
@@ -294,7 +304,6 @@ void BitMessage::restore_read_state(int32_t c, int32_t b)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::begin_writing()
 {
 	m_cur_size = 0;
@@ -303,21 +312,18 @@ void BitMessage::begin_writing()
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaining_space() const
 {
 	return m_max_size - m_cur_size;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_byte_align()
 {
 	m_write_bit = 0;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_bits(int32_t value, int32_t num_bits)
 {
 	int32_t		put;
@@ -399,49 +405,42 @@ void BitMessage::write_bits(int32_t value, int32_t num_bits)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_int8(int32_t c)
 {
 	write_bits(c, -8);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_uint8(int32_t c)
 {
 	write_bits(c, 8);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_int16(int32_t c)
 {
 	write_bits(c, -16);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_uint16(int32_t c)
 {
 	write_bits(c, 16);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_int32(int32_t c)
 {
 	write_bits(c, 32);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_real(real f)
 {
 	write_bits(*reinterpret_cast<int32_t *>(&f), 32);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_vec3(const Vec3& v)
 {
 	write_real(v.x);
@@ -450,7 +449,6 @@ void BitMessage::write_vec3(const Vec3& v)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
 {
 	if (!s) 
@@ -501,25 +499,22 @@ void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_data(const void* data, int32_t length)
 {
 	memcpy(get_byte_space(length), data, length);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_netaddr(const os::NetAddress addr)
 {
 	uint8_t* ptr;
 	
 	ptr = get_byte_space(4);
-	memcpy(ptr, addr.address, 4);
-	write_uint16(addr.port);
+	memcpy(ptr, addr.m_address, 4);
+	write_uint16(addr.port());
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::begin_reading() const
 {
 	m_read_count = 0;
@@ -527,21 +522,18 @@ void BitMessage::begin_reading() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaing_data() const
 {
 	m_cur_size - m_read_count;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::read_byte_align() const
 {
 	m_read_bit = 0;
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_bits(int32_t num_bits) const
 {
 	int32_t		value;
@@ -616,14 +608,12 @@ int32_t BitMessage::read_bits(int32_t num_bits) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_int8() const
 {
 	return (int32_t)read_bits(-8);
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_uint8() const
 {
   	return (int32_t)read_bits(8);
@@ -631,28 +621,24 @@ int32_t BitMessage::read_uint8() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_int16() const
 {
 	return (int32_t)read_bits(-16);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_uint16() const
 {
 	return (int32_t)read_bits(16);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_int32() const
 {
 	return (int32_t)read_bits(32);
 }
 
 //---------------------------------------------------------------------------------------------
-
 real BitMessage::read_real() const
 {
 	float value;
@@ -661,7 +647,6 @@ real BitMessage::read_real() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 Vec3 BitMessage::read_vec3() const
 {
 	Vec3 v;
@@ -674,7 +659,6 @@ Vec3 BitMessage::read_vec3() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_string(char* buffer, int32_t buffer_size) const
 {
 	int	l = 0;
@@ -710,7 +694,6 @@ int32_t BitMessage::read_string(char* buffer, int32_t buffer_size) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_data(void* data, int32_t length) const
 {
 	int count;
@@ -742,13 +725,12 @@ int32_t BitMessage::read_data(void* data, int32_t length) const
 //---------------------------------------------------------------------------------------------
 void BitMessage::read_netaddr(os::NetAddress* addr) const
 {
-
 	for (int i = 0; i < 4; i++) 
 	{
-		addr->address[i] = read_uint8();
+		addr->m_address[i] = read_uint8();
 	}
 	
-	addr->port = read_uint16();  
+	addr->m_port = read_uint16();  
 }
 
 //---------------------------------------------------------------------------------------------

+ 31 - 10
src/network/BitMessage.h

@@ -1,7 +1,31 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #pragma once
 
 #include "Types.h"
-#include "OS.h"
 #include "Allocator.h"
 #include "Vec3.h"
 
@@ -9,15 +33,12 @@ namespace crown
 {
 namespace network
 {
-	/**
-	* bit-packet reliable message.
-	* Usage: After every instantition, must be initialized with @init(len)
-	*	TODO: rework as POD is needed; this feature provides compatibility with queue
-	*/
 
+/// Bit-packet reliable message.
+/// Usage: After every instantition, must be initialized with @init(len)
+/// TODO: rework as POD is needed; this feature provides compatibility with queue
 class BitMessage
 {
-
 public:
 						BitMessage(Allocator& allocator);
 						~BitMessage();
@@ -91,7 +112,7 @@ private:
 	
 private:
 	
-	Allocator*			m_allocator;								// memory allocator
+	Allocator&			m_allocator;								// memory allocator
 	
 	uint8_t*			m_header;
 	uint8_t*			m_data;
@@ -106,9 +127,9 @@ private:
 	mutable int32_t		m_read_bit;									// number of bits read from the last read byte
 	
 	bool 				m_overflowed;								// overflow flag
-	bool				m_init;										// is init flag
-	
+	bool				m_init;										// is init flag	
 };
+
 } // namespace network
 } // namespace crown
 

+ 0 - 146
src/os/OS.h

@@ -209,152 +209,6 @@ void			cond_destroy(OSCond* cond);
 void			cond_signal(OSCond* cond);
 void			cond_wait(OSCond* cond, OSMutex* mutex);
 
-//-----------------------------------------------------------------------------
-// Networking
-//-----------------------------------------------------------------------------
-struct NetAddress
-{
-	uint8_t 	address[4];
-	uint16_t 	port;
-
-	NetAddress()
-	{
-		
-	}
-	
-	NetAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t p)
-	{
-		address[0] = a;
-		address[1] = b;
-		address[2] = c;
-		address[3] = d;
-		
-		port = p;
-	}
-	
-	uint32_t get_address()
-	{
-		uint32_t addr = (address[0] << 24) | (address[1] << 16) | (address[2] << 8) | (address[3]);
-		
-		return addr;
-	}
-	
-	uint16_t get_port() const
-	{
-		return port;
-	}
-	
-	void set(uint32_t a, uint16_t p)
-	{
-		address[0] = a >> 24;
-		address[1] = a >> 16;
-		address[2] = a >> 8;
-		address[3] = a;
-
-		port = p;
-	}
-	
-	void set(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t p)
-	{
-		address[0] = a;
-		address[1] = b;
-		address[2] = c;
-		address[3] = d;
-		
-		port = p;
-	}
-	
-	bool operator==(NetAddress& addr)
-	{
-		return address[0] == addr.address[0] &&
-			   address[1] == addr.address[1] &&
-			   address[2] == addr.address[2] &&
-			   address[3] == addr.address[3] &&
-			   port == addr.port;
-	}
-	
-	NetAddress& operator=(const NetAddress& addr)
-	{
-		address[0] = addr.address[0];
-		address[1] = addr.address[1];
-		address[2] = addr.address[2];
-		address[3] = addr.address[3];
-		
-		port = addr.port;
-		
-		return *this;
-	}
-	
-	void print()
-	{
-		printf("NetAddress: %i.%i.%i.%i:%i\n", address[0], address[1], address[2], address[3], port);
-	}
-};
-
-//-----------------------------------------------------------------------------
-class UDPSocket
-{
-public:
-
-				// Constructor
-				UDPSocket();
-				// Destructor
-				~UDPSocket();
-				// Open connection
-	bool 		open(uint16_t port);
-				 // Send data through socket
-	bool 		send(NetAddress &receiver, const void* data, size_t size );
-				// Receive data through socket
-	int32_t 	receive(NetAddress &sender, void* data, size_t size);
-				// Close connection
-	void 		close();
-				// Is connection open?
-	bool 		is_open();
-
-private:
-				// Socket descriptor
-	int32_t 	m_socket;
-};
-
-//-----------------------------------------------------------------------------
-class TCPSocket
-{
-public:
-
-				// Constructor
-				TCPSocket();
-				// Destructor
-				~TCPSocket();
-				// Open connection (server side)
-	bool 		open(uint16_t port);
-				// Connect (client side)
-	bool		connect(NetAddress& destination);
-				// Close connection
-	int32_t		close();
-				// Send data through socket
-	bool 		send(const void* data, size_t size);
-				// Receive data through socket
-	int32_t		receive(void* data, size_t size);
-				// Is connection open?
-	bool 		is_open();
-				// Getter method for socket descriptor
-	int32_t 	get_socket_id();
-				// Getter method for active socket descriptor
-	int32_t 	get_active_socket_id();
-				// Setter method for socket descriptor
-
-private:
-
-	void 		set_socket_id(int32_t socket);
-				// Setter method for ative socket descriptor
-	void 		set_active_socket_id(int32_t socket);
-	
-				// Generated by ::socket
-	int32_t 	m_socket;
-			  	// Generated by ::accept
-	int32_t 	m_active_socket;
-}; 
-
 } // namespace os
 } // namespace crown
 

+ 4 - 2
src/os/linux/CMakeLists.txt

@@ -6,12 +6,14 @@ set (LINUX_SRC
 	GLXRenderWindow.cpp
 	LinuxOS.cpp
 	Input.cpp
-	LinuxTCPSocket.cpp
-	LinuxUDPSocket.cpp	
+	TCPSocket.cpp
+	UDPSocket.cpp	
 	File.cpp
 )
 
 set (LINUX_HEADERS
+	TCPSocket.h
+	UDPSocket.h
 	File.h
 )
 

+ 61 - 44
src/os/linux/File.cpp

@@ -23,124 +23,141 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "File.h"
-#include "Log.h"
-#include "MathUtils.h"
 #include <cassert>
+#include <stdio.h>
+
+#include "OS.h"
+#include "File.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 File::File() :
-	m_file_handle(NULL), m_mode(FOM_READ)
+	m_file_handle(NULL),
+	m_mode(SOM_READ)
 {
 }
 
 //-----------------------------------------------------------------------------
 File::~File()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+void File::close()
 {
 	if (m_file_handle != NULL)
 	{
 		fclose(m_file_handle);
+		m_file_handle = NULL;
 	}
 }
 
 //-----------------------------------------------------------------------------
-bool File::is_valid()
+bool File::is_open() const
 {
-	return m_file_handle != 0;
+	return m_file_handle != NULL;
 }
 
 //-----------------------------------------------------------------------------
-FileOpenMode File::mode()
+StreamOpenMode File::mode()
 {
 	return m_mode;
 }
 
 //-----------------------------------------------------------------------------
-FILE* File::get_handle()
+size_t File::size() const
 {
-	return m_file_handle;
+	long pos = ftell(m_file_handle);
+
+	fseek(m_file_handle, 0, SEEK_END);
+
+	long size = ftell(m_file_handle);
+
+	fseek(m_file_handle, pos, SEEK_SET);
+
+	return (size_t) size;
 }
 
 //-----------------------------------------------------------------------------
-File* File::open(const char* path, FileOpenMode mode)
+bool File::open(const char* path, StreamOpenMode mode)
 {
-	File* f = new File();
-	f->m_file_handle = fopen(path, 
+	const char* c_mode = mode == SOM_READ ? "rb" : SOM_WRITE ? "wb" : "x";
 
-	/*
-		TestFlag(mode, FOM_READ) ?
-			(TestFlag(mode, FOM_WRITE) ?
-				(TestFlag(mode, FOM_CREATENEW) ? "wb+" : "rb+") : "rb") : (TestFlag(mode, FOM_WRITE) ? "wb" : "rb"));
-	*/
+	assert(c_mode[0] != 'x');
 
-	math::test_bitmask(mode, FOM_READ) ?
-		(math::test_bitmask(mode, FOM_WRITE) ? "rb+" : "rb") : (math::test_bitmask(mode, FOM_WRITE) ? "wb" : "rb")); 
+	m_file_handle = fopen(path, c_mode);
 
-	if (f->m_file_handle == NULL)
+	if (m_file_handle == NULL)
 	{
-		Log::e("File::Open: Could not open file %s", path);
-		return NULL;
+		os::printf("Could not open file %s", path);
+
+		return false;
 	}
 
-	f->m_mode = mode;
+	m_mode = mode;
 
-	return f;
+	return true;
 }
 
 //-----------------------------------------------------------------------------
-size_t File::read(void* ptr, size_t size, size_t nmemb)
+size_t File::read(void* data, size_t size)
 {
 	assert(m_file_handle != NULL);
-	assert(ptr != NULL);
+	assert(data != NULL);
 
-	return fread(ptr, size, nmemb, m_file_handle);
+	return fread(data, size, 1, m_file_handle);
 }
 
 //-----------------------------------------------------------------------------
-size_t File::write(const void* ptr, size_t size, size_t nmemb)
+size_t File::write(const void* data, size_t size)
 {
 	assert(m_file_handle != NULL);
-	assert(ptr != NULL);
+	assert(data != NULL);
 
-	return fwrite(ptr, size, nmemb, m_file_handle);
+	return fwrite(data, size, 1, m_file_handle);
 }
 
 //-----------------------------------------------------------------------------
-int File::seek(int32_t offset, int whence)
+void File::seek(size_t position)
 {
 	assert(m_file_handle != NULL);
 
-	return fseek(m_file_handle, offset, whence);
+	assert(fseek(m_file_handle, (long) position, SEEK_SET) == 0);
 }
 
 //-----------------------------------------------------------------------------
-int32_t File::tell()
+void File::seek_to_end()
 {
 	assert(m_file_handle != NULL);
-	
-	return ftell(m_file_handle);
+
+	assert(fseek(m_file_handle, 0, SEEK_END) == 0);
 }
 
 //-----------------------------------------------------------------------------
-int File::eof()
+void File::skip(size_t bytes)
 {
 	assert(m_file_handle != NULL);
-	
-	return feof(m_file_handle);
+
+	assert(fseek(m_file_handle, bytes, SEEK_CUR) == 0);
 }
 
 //-----------------------------------------------------------------------------
-size_t File::size()
+size_t File::position() const
 {
-	size_t pos = ftell(m_file_handle);
-	fseek(m_file_handle, 0, SEEK_END);
-	size_t size = ftell(m_file_handle);
-	fseek(m_file_handle, pos, SEEK_SET);
+	assert(m_file_handle != NULL);
+
+	return (size_t) ftell(m_file_handle);
+}
+
+//-----------------------------------------------------------------------------
+bool File::eof() const
+{
+	assert(m_file_handle != NULL);
 
-	return size;
+	return feof(m_file_handle) != 0;
 }
 
 } // namespace crown

+ 39 - 28
src/os/linux/File.h

@@ -26,53 +26,64 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include <cstdio>
+
 #include "Types.h"
+#include "Stream.h"
 
 namespace crown
 {
 
-/**
-	Enumerates file opening modes.
-*/
-enum FileOpenMode
-{
-	FOM_READ	= 1,
-	FOM_WRITE	= 2
-};
-
-/**
-	Standard C file wrapper.
-*/
+/// Standard C file wrapper
 class File
 {
-
 public:
 
-						~File();
+							File();
+							~File();
 
-	bool				is_valid();
+	/// Opens the file located at @path with the given @mode.
+	bool					open(const char* path, StreamOpenMode mode);
 
-	FileOpenMode		mode();
+	/// Closes the file.
+	void					close();
 
-	FILE*				get_handle();
+	bool					is_open() const;
 
-	size_t				read(void* ptr, size_t size, size_t nmemb);
-	size_t				write(const void* ptr, size_t size, size_t nmemb);
-	int					seek(int32_t offset, int whence);
-	int32_t				tell();
+	/// Return the size of the file in bytes.
+	size_t					size() const;
 
-	int					eof();
+	/// Returs the mode used to open the file.
+	StreamOpenMode			mode();
 
-	size_t				size();
+	/// Reads @size bytes from the file and stores it into @data.
+	/// Returns the number of bytes read.
+	size_t					read(void* data, size_t size);
 
-	static File*		open(const char* path, FileOpenMode mode);
+	/// Writes @size bytes of data stored in @data and returns the
+	/// number of bytes written.
+	size_t					write(const void* data, size_t size);
 
-private:
+	/// Moves the file pointer to the given @position.
+	void					seek(size_t position);
+
+	/// Moves the file pointer to the end of the file.
+	void					seek_to_end();
+
+	/// Moves the file pointer @bytes bytes ahead the current
+	/// file pointer position.
+	void					skip(size_t bytes);
 
-	FILE*				m_file_handle;
-	FileOpenMode		m_mode;
+	/// Returns the position of the file pointer from the
+	/// start of the file in bytes.
+	size_t					position() const;
+
+	/// Returns whether the file pointer is at the end of the file.
+	bool					eof() const;
+
+private:
 
-						File();
+	FILE*					m_file_handle;
+	StreamOpenMode			m_mode;
 };
 
 } // namespace crown

+ 0 - 182
src/os/linux/LinuxTCPSocket.cpp

@@ -1,182 +0,0 @@
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <cassert>
-
-#include "Types.h"
-#include "OS.h"
-
-namespace crown
-{
-namespace os
-{
-
-TCPSocket::TCPSocket()
-{
-	set_socket_id(0);
-	set_active_socket_id(0);
-}
-
-TCPSocket::~TCPSocket()
-{
-	close();
-}
-
-bool TCPSocket::open(uint16_t port)
-{
-	int32_t sd = socket(AF_INET, SOCK_STREAM, 0);
-
-	if (sd <= 0)
-	{
-		os::printf("failed to open socket\n");
-		set_socket_id(0);
-		return false;
-	}
-
-	set_socket_id(sd);
-
-	// Bind socket
-	sockaddr_in address;
-	address.sin_family = AF_INET;
-	address.sin_addr.s_addr = htonl(INADDR_ANY);
-	address.sin_port = htons(port);
-
-	if (bind(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
-	{
-		os::printf("failed to bind socket\n");
-		close();
-		return false;
-	}
-
-	listen(sd, 5);
-	os::printf("listening on port %d\n", port);
-
-	sockaddr_in client;
-	uint32_t client_length = sizeof(client);
-
-	int32_t asd = accept(sd, (sockaddr*)&client, &client_length);
-	if (asd < 0)
-	{
-		os::printf("failed to accept connection\n");
-	}
-
-	set_active_socket_id(asd);
-
-	return true;  
-}
-
-bool TCPSocket::connect(NetAddress& destination)
-{
-	int32_t sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-
-	if (sd <= 0)
-	{
-		os::printf("failed to open socket\n");
-		set_socket_id(0);
-		return false;
-	}
-
-	set_socket_id(sd);
-
-	sockaddr_in address;
-	address.sin_family = AF_INET;
-	address.sin_addr.s_addr =  htonl(destination.get_address());
-	address.sin_port = htons(destination.get_port());
-
-	if (::connect(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
-	{
-		os::printf("failed to connect socket\n");
-		close();
-		return false;
-	}  
-}
-
-int32_t	TCPSocket::close()
-{
-	int32_t asd = get_active_socket_id();
-	if (asd != 0)
-	{
-		::close(asd);
-		set_active_socket_id(0);  
-	}
-	int32_t sd = get_socket_id();
-	if (sd != 0)
-	{
-		::close(sd);
-		set_socket_id(0);
-	}
-}
-
-bool TCPSocket::send(const void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	int32_t sd = get_active_socket_id();
-	if (sd <= 0)
-	{
-		set_socket_id(0);
-		set_active_socket_id(0);
-		return false;
-	}
-
-	int32_t sent_bytes = ::send(sd, (const char*)data, size, 0);
-	if (sent_bytes <= 0)
-	{
-		os::printf("Unable to send data");
-		return false;
-	}
-
-	return true;  
-}
-
-int32_t TCPSocket::receive(void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	int32_t sd = get_active_socket_id();
-
-	if ( sd <= 0 )
-	{
-		return false;
-	}
-
-	int32_t received_bytes = ::recv(sd, (char*)data, size, 0);
-	if ( received_bytes <= 0 )
-	{
-		return 0;
-	}
-
-	return received_bytes;
-}
-
-bool TCPSocket::is_open()
-{
-	return m_active_socket != 0 || m_socket != 0;
-}
-
-int32_t	TCPSocket::get_socket_id()
-{
-	return m_socket;
-}
-
-int32_t	TCPSocket::get_active_socket_id()
-{
-	return m_active_socket;
-}
-
-void TCPSocket::set_socket_id(int32_t socket)
-{
-	m_socket = socket;
-}
-
-void TCPSocket::set_active_socket_id(int32_t socket)
-{
-	m_active_socket = socket;
-}
-		
-}
-}

+ 0 - 127
src/os/linux/LinuxUDPSocket.cpp

@@ -1,127 +0,0 @@
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <cassert>
-
-#include "Types.h"
-#include "OS.h"
-
-namespace crown
-{ 
-namespace os
-{
-
-UDPSocket::UDPSocket()
-{
-	m_socket = 0;  
-}
-UDPSocket::~UDPSocket()
-{
-	close();
-}
-
-bool UDPSocket::open(uint16_t port)
-{
-  		assert(!is_open());
-
-		m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-		if (m_socket <= 0)
-		{
-			os::printf("Failed to create socket.\n");;
-			m_socket = 0;
-			return false;
-		}
-
-		// bind to port
-		sockaddr_in address;
-		address.sin_family = AF_INET;
-		address.sin_addr.s_addr = INADDR_ANY;
-		address.sin_port = htons(port);
-
-		if ( bind( m_socket, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
-		{
-			os::printf("Failed to bind socket\n");
-			close();
-			return false;
-		}
-
-		int32_t non_blocking = 1;
-		if (fcntl( m_socket, F_SETFL, O_NONBLOCK, non_blocking ) == -1)
-		{
-			os::printf("Failed to set non-blocking socket\n");
-			close();
-			return false;
-		}
-		
-		return true;
-}
-
-bool UDPSocket::send(NetAddress &receiver, const void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	if (m_socket == 0)
-	{
-		return false;
-	}
-	
-	assert(receiver.address);
-	assert(receiver.port);
-
-	sockaddr_in address;
-	address.sin_family = AF_INET;
-	address.sin_addr.s_addr = htonl(receiver.get_address());
-	address.sin_port = htons(receiver.get_port());
-
-	int32_t sent_bytes = sendto(m_socket, (const char*)data, size, 0, (sockaddr*)&address, sizeof(sockaddr_in));
-
-	return sent_bytes == size;
-}
-
-int32_t UDPSocket::receive(NetAddress& sender, void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	if (m_socket == 0)
-	{
-		return false;
-	}
-
-	sockaddr_in from;
-	socklen_t from_length = sizeof(from);
-
-	int32_t received_bytes = recvfrom(m_socket, (char*)data, size, 0, (sockaddr*)&from, &from_length);
-
-	if (received_bytes <= 0)
-	{
-			return 0;
-	}
-
-	uint32_t address = ntohl(from.sin_addr.s_addr);
-	uint16_t port = ntohs(from.sin_port);
-	
-	sender.set(address, port);
-	
-	return received_bytes;	
-}
-
-void UDPSocket::close()
-{
-	if ( m_socket != 0 )
-	{
-		::close(m_socket);
-		m_socket = 0; 
-	}
-}
-
-bool UDPSocket::is_open()
-{
-	return m_socket != 0;
-}
-
-} // namespace os
-} // namespace crown

+ 216 - 0
src/os/linux/TCPSocket.cpp

@@ -0,0 +1,216 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <cassert>
+
+#include "Types.h"
+#include "OS.h"
+#include "TCPSocket.h"
+#include "NetAddress.h"
+
+namespace crown
+{
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+TCPSocket::TCPSocket() :
+	m_socket(0),
+	m_active_socket(0)
+
+{
+}
+
+//-----------------------------------------------------------------------------
+TCPSocket::~TCPSocket()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::open(uint16_t port)
+{
+	int sd = socket(AF_INET, SOCK_STREAM, 0);
+
+	if (sd <= 0)
+	{
+		os::printf("failed to open socket\n");
+		m_socket = 0;
+
+		return false;
+	}
+
+	m_socket = sd;
+
+	// Bind socket
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = htonl(INADDR_ANY);
+	address.sin_port = htons(port);
+
+	if (bind(sd, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
+	{
+		os::printf("Failed to bind socket\n");
+		close();
+
+		return false;
+	}
+
+	listen(sd, 5);
+	os::printf("Listening on port %d\n", port);
+
+	sockaddr_in client;
+	size_t client_length = sizeof(client);
+
+	int asd = accept(sd, (sockaddr*)&client, &client_length);
+
+	if (asd < 0)
+	{
+		os::printf("failed to accept connection\n");
+
+		return false;
+	}
+
+	m_active_socket = asd;
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::connect(const NetAddress& destination)
+{
+	int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+	if (sd <= 0)
+	{
+		os::printf("Failed to open socket\n");
+		m_socket = 0;
+
+		return false;
+	}
+
+	m_socket = sd;
+
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr =  htonl(destination.address());
+	address.sin_port = htons(destination.port());
+
+	if (::connect(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
+	{
+		os::printf("Failed to connect socket\n");
+		close();
+
+		return false;
+	}
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+void TCPSocket::close()
+{
+	if (m_active_socket != 0)
+	{
+		::close(m_active_socket);
+		m_active_socket = 0;  
+	}
+
+	if (m_socket != 0)
+	{
+		::close(m_socket);
+		m_socket = 0;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::send(const void* data, size_t size)
+{
+	assert(data != NULL);
+	assert(size > 0);
+
+	if (m_active_socket <= 0)
+	{
+		m_socket = 0;
+		m_active_socket = 0;
+
+		return false;
+	}
+
+	ssize_t sent_bytes = ::send(m_active_socket, (const char*) data, size, 0);
+	if (sent_bytes <= 0)
+	{
+		os::printf("Unable to send data");
+		return false;
+	}
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+size_t TCPSocket::receive(void* data, size_t size)
+{
+	assert(data);
+	assert(size > 0);
+
+	if (m_active_socket <= 0)
+	{
+		return false;
+	}
+
+	ssize_t received_bytes = ::recv(m_active_socket, (char*) data, size, 0);
+	if (received_bytes <= 0)
+	{
+		return 0;
+	}
+
+	return received_bytes;
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::is_open()
+{
+	return m_active_socket != 0 || m_socket != 0;
+}
+
+//-----------------------------------------------------------------------------
+int	TCPSocket::socket_id()
+{
+	return m_socket;
+}
+
+//-----------------------------------------------------------------------------
+int	TCPSocket::active_socket_id()
+{
+	return m_active_socket;
+}
+		
+} // namespace os
+} // namespace crown

+ 37 - 50
src/os/win/WinInputManager.h → src/os/linux/TCPSocket.h

@@ -25,68 +25,55 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "InputManager.h"
-#include <windows.h>
+#include "Types.h"
 
 namespace crown
 {
-
-class WinInputManager : public InputManager
+namespace os
 {
 
-	friend class WinMouse;
-	friend class WinKeyboard;
+class NetAddress;
 
+/// OS level TCP socket.
+class TCPSocket
+{
 public:
 
-	/** @copydoc InputManager::InputManager() */
-	WinInputManager();
-
-	/** @copydoc InputManager::~InputManager() */
-	virtual ~WinInputManager();
-
-	/** @copydoc InputManager::Init() */
-	virtual void Init(const EventSource& source);
-
-	/** @copydoc InputManager::IsMouseAvailable() */
-	virtual bool IsMouseAvailable()
-	{
-		return mIsMouseAvailable;
-	}
-
-	/** @copydoc InputManager::IsKeyboardAvailable() */
-	virtual bool IsKeyboardAvailable()
-	{
-		return mIsKeyboardAvailable;
-	}
-
-	/** @copydoc InputManager::IsTouchAvailable() */
-	virtual bool IsTouchAvailable()
-	{
-		return false;
-	}
-
-	/**
-		Returns the Window Handle "attached" to this manager.
-	@return
-		The Window Handle
-	*/
-	inline unsigned int64_t GetWindowHandle()
-	{
-		return (unsigned int64_t)mWindowHandle;
-	}
+				TCPSocket();
+				~TCPSocket();
 
-private:
+	// Open connection (server side)
+	bool 		open(uint16_t port);
+
+	// Connect (client side)
+	bool		connect(const NetAddress& destination);
+
+	// Close connection
+	void		close();
+
+	// Send data through socket
+	bool 		send(const void* data, size_t size);
 
-	void SetMouseAvailable(bool available) { mIsMouseAvailable = available; }
-	void SetKeyboardAvailable(bool available) { mIsKeyboardAvailable = available; }
+	// Receive data through socket
+	size_t		receive(void* data, size_t size);
 
-	bool mIsMouseAvailable		: 1;
-	bool mIsKeyboardAvailable	: 1;
+	// Is connection open?
+	bool 		is_open();
 
-	// Win related
-	HWND mWindowHandle;
+	// Getter method for socket descriptor
+	int 		socket_id();
+
+	// Getter method for active socket descriptor
+	int 		active_socket_id();
+
+private:
+	
+	// Generated by ::socket
+	int 		m_socket;
+
+	// Generated by ::accept
+	int 		m_active_socket;
 };
 
+} // namespace os
 } // namespace crown
-

+ 161 - 0
src/os/linux/UDPSocket.cpp

@@ -0,0 +1,161 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <cassert>
+
+#include "Types.h"
+#include "OS.h"
+#include "UDPSocket.h"
+#include "NetAddress.h"
+
+namespace crown
+{ 
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+UDPSocket::UDPSocket() :
+	m_socket(0)
+{
+}
+
+//-----------------------------------------------------------------------------
+UDPSocket::~UDPSocket()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+bool UDPSocket::open(uint16_t port)
+{
+	assert(!is_open());
+
+	m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+
+	if (m_socket <= 0)
+	{
+		os::printf("Failed to create socket.\n");
+		m_socket = 0;
+
+		return false;
+	}
+
+	// Bind to port
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = INADDR_ANY;
+	address.sin_port = htons(port);
+
+	if (bind(m_socket, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
+	{
+		os::printf("Failed to bind socket\n");
+		close();
+
+		return false;
+	}
+
+	int non_blocking = 1;
+
+	if (fcntl(m_socket, F_SETFL, O_NONBLOCK, non_blocking) == -1)
+	{
+		os::printf("Failed to set non-blocking socket\n");
+		close();
+
+		return false;
+	}
+	
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+bool UDPSocket::send(const NetAddress &receiver, const void* data, size_t size)
+{
+	assert(is_open());
+	assert(data != NULL);
+	assert(size > 0);
+
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = htonl(receiver.address());
+	address.sin_port = htons(receiver.port());
+
+	ssize_t sent_bytes = sendto(m_socket, (const char*) data, size, 0, (sockaddr*) &address, sizeof(sockaddr_in));
+
+	if (sent_bytes < 0)
+	{
+		return false;
+	}
+
+	return (size_t) sent_bytes == size;
+}
+
+//-----------------------------------------------------------------------------
+size_t UDPSocket::receive(NetAddress& sender, void* data, size_t size)
+{
+	assert(is_open());
+	assert(data);
+	assert(size > 0);
+
+	sockaddr_in from;
+	socklen_t from_length = sizeof(from);
+
+	ssize_t received_bytes = recvfrom(m_socket, (char*)data, size, 0, (sockaddr*)&from, &from_length);
+
+	if (received_bytes <= 0)
+	{
+		return 0;
+	}
+
+	uint32_t address = ntohl(from.sin_addr.s_addr);
+	uint16_t port = ntohs(from.sin_port);
+
+	sender.set(address, port);
+
+	return (size_t) received_bytes;
+}
+
+//-----------------------------------------------------------------------------
+void UDPSocket::close()
+{
+	if (m_socket != 0)
+	{
+		::close(m_socket);
+		m_socket = 0;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool UDPSocket::is_open()
+{
+	return m_socket != 0;
+}
+
+} // namespace os
+} // namespace crown

+ 32 - 37
src/os/win/WinInputManager.cpp → src/os/linux/UDPSocket.h

@@ -23,50 +23,45 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "WinInputManager.h"
-#include "WinMouse.h"
-#include "WinKeyboard.h"
+#pragma once
+
+#include "Types.h"
 
 namespace crown
 {
-
-//-----------------------------------------------------------------------------
-WinInputManager::WinInputManager() :
-	mIsMouseAvailable(false),
-	mIsKeyboardAvailable(false),
-	mWindowHandle(0)
+namespace os
 {
-}
 
-//-----------------------------------------------------------------------------
-WinInputManager::~WinInputManager()
-{
-	if (mMouse)
-	{
-		delete mMouse;
-	}
-
-	if (mKeyboard)
-	{
-		delete mKeyboard;
-	}
-}
-
-//-----------------------------------------------------------------------------
-void WinInputManager::Init(const EventSource& source)
+class NetAddress;
+
+/// OS level UDP socket.
+class UDPSocket
 {
-	mWindowHandle = (HWND)source.WindowHandle;
+public:
 
-	if (!mMouse)
-	{
-		mMouse = new WinMouse(this);
-	}
+				UDPSocket();
+				~UDPSocket();
 
-	if (!mKeyboard)
-	{
-		mKeyboard = new WinKeyboard(this);
-	}
-}
+	// Open connection
+	bool 		open(uint16_t port);
 
-} // namespace crown
+	// Send data through socket
+	bool 		send(const NetAddress& receiver, const void* data, size_t size);
 
+	// Receive data through socket
+	size_t	 	receive(NetAddress& sender, void* data, size_t size);
+
+	// Close connection
+	void 		close();
+
+	// Is connection open?
+	bool 		is_open();
+
+private:
+	
+	// Socket descriptor
+	int 	m_socket;
+};
+
+} // namespace os
+} // namespace crown