Browse Source

some connection methods implemented

mikymod 13 năm trước cách đây
mục cha
commit
cee46c5acc

+ 8 - 1
src/network/BitMessage.cpp

@@ -8,7 +8,14 @@ namespace crown
 namespace network
 namespace network
 {
 {
   
   
-BitMessage::BitMessage() : w_data(NULL), r_data(NULL), max_size(0), cur_size(0), write_bit(0), read_count(0), read_bit(0)
+BitMessage::BitMessage() : 
+	w_data(NULL), 
+	r_data(NULL), 
+	max_size(0), 
+	cur_size(0), 
+	write_bit(0), 
+	read_count(0), 
+	read_bit(0)
 {
 {
   
   
 }
 }

+ 12 - 13
src/network/BitMessage.h

@@ -25,7 +25,6 @@ namespace network
 		int32_t			get_max_size() const;							// get the maximum message size
 		int32_t			get_max_size() const;							// get the maximum message size
 		bool 			is_overflowed();								// get overflowed flag
 		bool 			is_overflowed();								// get overflowed flag
 
 
-
 		int32_t			get_size() const;								// size of the message in bytes
 		int32_t			get_size() const;								// size of the message in bytes
 		void			set_size(int32_t size);							// set the message size
 		void			set_size(int32_t size);							// set the message size
 		int32_t			get_write_bit() const;							// get current write bit
 		int32_t			get_write_bit() const;							// get current write bit
@@ -59,10 +58,10 @@ namespace network
 		void			write_data(const void* data, int32_t length);
 		void			write_data(const void* data, int32_t length);
 		void			write_ipv4addr(const os::NetAddress addr);
 		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
+		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_int8() const;
 		int32_t			read_uint8() const;
 		int32_t			read_uint8() const;
 		int32_t			read_int16() const;
 		int32_t			read_int16() const;
@@ -79,14 +78,14 @@ namespace network
 
 
 	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*			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
 
 
 	private:
 	private:
 	  
 	  

+ 20 - 20
src/network/Connection.cpp

@@ -37,12 +37,6 @@ void Connection::init(const os::NetAddress addr, const int id)
 	m_incoming_sequence = 0;
 	m_incoming_sequence = 0;
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void Connection::shutdown()
-{
-	//TODO: don't needed with Allocator mechanism
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void Connection::reset_rate()
 void Connection::reset_rate()
 {
 {
@@ -131,12 +125,31 @@ bool Connection::process(const os::NetAddress from, int time, BitMessage &msg, i
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void Connection::send_reliable_message(const BitMessage& msg)
 void Connection::send_reliable_message(const BitMessage& msg)
 {
 {
-	m_reliable_send.push_back(msg);
+	uint32_t id = msg.read_int32();
+
+	msg.begin_reading();
+
+	if (id == m_id)
+	{
+		m_reliable_send.push_back(msg);
+	}
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 bool Connection::receive_reliable_message(BitMessage& msg)
 bool Connection::receive_reliable_message(BitMessage& msg)
 {
 {
+	uint32_t id = msg.read_int32();
+	
+	// check correctness of message id
+	if (id == m_id)	
+	{
+		// save message
+		m_reliable_receive.push_back(msg);
+		
+		return true;
+	}
+	
+	return false;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -146,19 +159,6 @@ void Connection::clear_reliable_messages()
 	m_reliable_receive.clear();
 	m_reliable_receive.clear();
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void Connection::_write_message(BitMessage& out, const BitMessage& msg)
-{
-	 uint8_t* packet; 
-	 
-}
-
-//-----------------------------------------------------------------------------
-bool Connection::_read_message(BitMessage& out, const BitMessage& msg)
-{
- 
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void Connection::_update_outgoing_rate(const int time, const int size)
 void Connection::_update_outgoing_rate(const int time, const int size)
 {
 {

+ 26 - 29
src/network/Connection.h

@@ -10,9 +10,11 @@ namespace crown
 {
 {
 namespace network
 namespace network
 {
 {
+	#define DEFAULT_PROTOCOL_ID 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
 class Connection
 {
 {
@@ -21,26 +23,25 @@ public:
 							Connection(Allocator& allocator);
 							Connection(Allocator& allocator);
 							~Connection();
 							~Connection();
 
 
-	void					init(const os::NetAddress addr, const int id);
-	void					shutdown();
+	void					init(const os::NetAddress addr, const int32_t id = DEFAULT_PROTOCOL_ID);
 	void					reset_rate();
 	void					reset_rate();
 
 
 							// Sets the maximum outgoing rate.
 							// Sets the maximum outgoing rate.
-	void					set_max_outgoing_rate(int rate);
+	void					set_max_outgoing_rate(int32_t rate);
 							// Gets the maximum outgoing rate.
 							// Gets the maximum outgoing rate.
-	int						get_max_outgoing_rate();
+	int32_t					get_max_outgoing_rate();
 							// Returns the address of the entity at the other side of the channel.
 							// Returns the address of the entity at the other side of the channel.
 	os::NetAddress			get_remote_address() const;
 	os::NetAddress			get_remote_address() const;
 							// Returns the average outgoing rate over the last second.
 							// Returns the average outgoing rate over the last second.
-	int						get_outgoing_rate() const;
+	int32_t					get_outgoing_rate() const;
 							// Returns the average incoming rate over the last second.
 							// Returns the average incoming rate over the last second.
-	int						get_incoming_rate() const;
+	int32_t					get_incoming_rate() const;
 							// Returns the average incoming packet loss over the last 5 seconds.
 							// Returns the average incoming packet loss over the last 5 seconds.
-	float					get_incoming_packet_loss() const;		
+	real					get_incoming_packet_loss() const;		
 							// Returns true if the channel is ready to send new data based on the maximum rate.
 							// Returns true if the channel is ready to send new data based on the maximum rate.
-	bool					ready_to_send(const int time) const;
+	bool					ready_to_send(const int32_t time) const;
 							// Processes the incoming message.
 							// Processes the incoming message.
-	bool					process(const os::NetAddress from, int time, BitMessage &msg, int &sequence);
+	bool					process(const os::NetAddress from, int32_t time, BitMessage &msg, int32_t &sequence);
 							// Sends a reliable message, in order and without duplicates.
 							// Sends a reliable message, in order and without duplicates.
 	void					send_reliable_message(const BitMessage &msg);
 	void					send_reliable_message(const BitMessage &msg);
 							// Returns true if a new reliable message is available and stores the message.
 							// Returns true if a new reliable message is available and stores the message.
@@ -49,39 +50,35 @@ public:
 	void					clear_reliable_messages();
 	void					clear_reliable_messages();
 
 
 private:
 private:
-							// methods which provides a pattern for communication
-	void					_write_message(BitMessage &out, const BitMessage &msg);
-	bool					_read_message(BitMessage &out, const BitMessage &msg);
-
 							// methods which provides a reliability system
 							// methods which provides a reliability system
-	void					_update_outgoing_rate(const int time, const int size);
-	void					_update_incoming_rate(const int time, const int size);
-	void					_update_packet_loss(const int time, const int num_recv, const int num_dropped);
+	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);
 
 
 private:
 private:
   
   
 	os::NetAddress			m_remote_address;			// address of remote host
 	os::NetAddress			m_remote_address;			// address of remote host
-	int						m_id;						// our identification used instead of port number
-	int						m_max_rate;					// maximum number of bytes that may go out per second
+	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
 				// variables to control the outgoing rate
-	int						m_last_send_time;			// last time data was sent out
-	int						m_last_data_bytes;			// bytes left to send at last send time
+	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
 				// variables to keep track of the rate
-	int						m_outgoing_rate_time;		// outgoing time rate
-	int						m_outgoing_rate_bytes;		// outgoing bytes rate
-	int						m_incoming_rate_time;		// incoming time rate
-	int						m_incoming_rate_bytes;		// incoming bytes 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
 				// variables to keep track of the incoming packet loss
-	float					m_incoming_recv_packets;
-	float					m_incoming_dropped_packets;
-	int						m_incoming_packet_loss_time;
+	real					m_incoming_recv_packets;
+	real					m_incoming_dropped_packets;
+	int32_t						m_incoming_packet_loss_time;
 
 
 				// sequencing variables
 				// sequencing variables
-	int						m_outgoing_sequence;
-	int						m_incoming_sequence;
+	int32_t					m_outgoing_sequence;
+	int32_t					m_incoming_sequence;
 
 
 				// reliable messages
 				// reliable messages
 	Queue<BitMessage>		m_reliable_send;
 	Queue<BitMessage>		m_reliable_send;