mikymod 13 лет назад
Родитель
Сommit
b4b364e233
2 измененных файлов с 179 добавлено и 120 удалено
  1. 116 34
      src/network/Connection.cpp
  2. 63 86
      src/network/Connection.h

+ 116 - 34
src/network/Connection.cpp

@@ -12,11 +12,13 @@ Connection::Connection(Allocator& allocator) :
   
 }
 
+//-----------------------------------------------------------------------------
 Connection::~Connection()
 {
   
 }
 
+//-----------------------------------------------------------------------------
 void Connection::init(const os::NetAddress addr, const int id)
 {
 	m_remote_address = addr;
@@ -33,19 +35,15 @@ void Connection::init(const os::NetAddress addr, const int id)
 	m_incoming_packet_loss_time = 0;
 	m_outgoing_sequence = 0;
 	m_incoming_sequence = 0;
-	m_unsent_fragments = false;
-	m_unsent_fragment_start = 0;
-	m_fragment_sequence = 0;
-	m_fragment_length = 0;
-	
-	//TODO: init queues
 }
 
+//-----------------------------------------------------------------------------
 void Connection::shutdown()
 {
-	//TODO: dealloc all
+	//TODO: don't needed with Allocator mechanism
 }
 
+//-----------------------------------------------------------------------------
 void Connection::reset_rate()
 {
   	m_last_send_time = 0;
@@ -56,100 +54,184 @@ void Connection::reset_rate()
 	m_incoming_rate_bytes = 0;
 }
 
+//-----------------------------------------------------------------------------
 void Connection::set_max_outgoing_rate(int rate)
 {
 	m_max_rate = rate;
 }
 
+//-----------------------------------------------------------------------------
 int Connection::get_max_outgoing_rate()
 {
 	return m_max_rate;
 }
 
+//-----------------------------------------------------------------------------
 os::NetAddress Connection::get_remote_address() const
 {
 	return m_remote_address;
 }
 
+//-----------------------------------------------------------------------------
 int Connection::get_outgoing_rate() const
 {
 	return m_outgoing_rate_bytes;
 }
 
+//-----------------------------------------------------------------------------
 int Connection::get_incoming_rate() const
 {
 	return m_incoming_rate_bytes;
 }
 
+//-----------------------------------------------------------------------------
 float Connection::get_incoming_packet_loss() const
 {
-	
+	if (m_incoming_recv_packets == 0 && m_incoming_dropped_packets == 0)
+	{
+		return 0.0f;
+	}
+	// return loss packet %
+	return m_incoming_dropped_packets * 100 / (m_incoming_recv_packets + m_incoming_dropped_packets);
 }
 
+//-----------------------------------------------------------------------------
 bool Connection::ready_to_send(const int time) const
 {
-  
-}
-
-int Connection::send_message(os::UDPSocket &socket, const int time, const BitMessage &msg)
-{
-  
-}
-
-void Connection::send_next_fragment(os::UDPSocket &socket, const int time)
-{
-  
+	// if max rate isn't set, send message
+	if (!m_max_rate)
+	{
+		return true;
+	}
+	
+	int delta_time;
+	
+	delta_time = time - m_last_send_time;
+	if (delta_time > 1000)
+	{
+		return true;
+	}
+	
+	// if last message wasn't sent, sent it!
+	return ((m_last_data_bytes - ((delta_time * m_max_rate) / 1000)) <= 0);
 }
 
-bool Connection::unsent_fragments_left() const
+//-----------------------------------------------------------------------------
+/* 
+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 Connection::process(const os::NetAddress from, BitMessage &msg, int &sequence, int time)
-{
-  
 }
 
-void Connection::send_reliable_message(const BitMessage &msg)
+//-----------------------------------------------------------------------------
+void Connection::send_reliable_message(const BitMessage& msg)
 {
 	m_reliable_send.push_back(msg);
 }
 
-bool Connection::get_reliable_message(BitMessage &msg)
+//-----------------------------------------------------------------------------
+bool Connection::receive_reliable_message(BitMessage& msg)
 {
-	
 }
 
+//-----------------------------------------------------------------------------
 void Connection::clear_reliable_messages()
 {
 	m_reliable_send.clear();
 	m_reliable_receive.clear();
 }
 
-void Connection::_write_message(BitMessage &out, const BitMessage &msg)
+//-----------------------------------------------------------------------------
+void Connection::_write_message(BitMessage& out, const BitMessage& msg)
 {
-  
+	 uint8_t* packet; 
+	 
 }
 
-bool Connection::_read_message(BitMessage &out, const BitMessage &msg)
+//-----------------------------------------------------------------------------
+bool Connection::_read_message(BitMessage& out, const BitMessage& msg)
 {
  
 }
 
+//-----------------------------------------------------------------------------
 void Connection::_update_outgoing_rate(const int time, const int size)
 {
-  
+	// update the outgoing rate control variables
+	int delta_time;
+	delta_time = time - m_last_send_time;
+	if (delta_time > 1000) 
+	{
+		m_last_data_bytes = 0;
+	}
+	else 
+	{
+		m_last_data_bytes -= (delta_time * m_max_rate) / 1000;
+		if ( m_last_data_bytes < 0 ) 
+		{
+			m_last_data_bytes = 0;
+		}
+	}
+	
+	m_last_data_bytes += size;
+	m_last_send_time = time;  
+	
+	// update outgoing rate variables
+	if (time - m_outgoing_rate_time > 1000) 
+	{
+		m_outgoing_rate_bytes -= m_outgoing_rate_bytes * (time - m_outgoing_rate_time - 1000) / 1000;
+		if (m_outgoing_rate_bytes < 0) 
+		{
+			m_outgoing_rate_bytes = 0;
+		}
+	}
+	
+	m_outgoing_rate_time = time - 1000;
+	m_outgoing_rate_bytes += size;
 }
 
+//-----------------------------------------------------------------------------
 void Connection::_update_incoming_rate(const int time, const int size)
 {
-  
+	// update incoming rate variables
+	if (time - m_incoming_rate_time > 1000) 
+	{
+		m_incoming_rate_bytes -= m_incoming_rate_bytes * (time - m_incoming_rate_time - 1000) / 1000;
+		if (m_incoming_rate_bytes < 0) 
+		{
+			m_incoming_rate_bytes = 0;
+		}
+	}
+	m_incoming_rate_time = time - 1000;
+	m_incoming_rate_bytes += size;  
 }
 
+//-----------------------------------------------------------------------------
 void Connection::_update_packet_loss(const int time, const int num_recv, const int num_dropped)
 {
-  
+	// update incoming packet loss variables
+	if (time - m_incoming_packet_loss_time > 5000) 
+	{
+		float scale = (time - m_incoming_packet_loss_time - 5000) * (1.0f / 5000.0f);
+		m_incoming_recv_packets -= m_incoming_recv_packets * scale;
+		if (m_incoming_recv_packets < 0.0f) 
+		{
+			m_incoming_recv_packets = 0.0f;
+		}
+		m_incoming_dropped_packets -= m_incoming_dropped_packets * scale;
+		if (m_incoming_dropped_packets < 0.0f) 
+		{
+			m_incoming_dropped_packets = 0.0f;
+		}
+	}
+	m_incoming_packet_loss_time = time - 5000;
+	m_incoming_recv_packets += num_recv;
+	m_incoming_dropped_packets += num_dropped;
 }
 
 } // namespace network

+ 63 - 86
src/network/Connection.h

@@ -10,106 +10,83 @@ namespace crown
 {
 namespace network
 {
+	#define MAX_PACKET_LEN		1400
 	#define MAX_MESSAGE_SIZE	16384
 	#define MAX_QUEUE_SIZE		16384
  
-	class Connection
-	{
-	public:
-	  
-								Connection(Allocator& allocator);
-								~Connection();
+class Connection
+{
+public:
   
-		void					init(const os::NetAddress addr, const int id);
-		void					shutdown();
-		void					reset_rate();
-
-								// Sets the maximum outgoing rate.
-		void					set_max_outgoing_rate(int rate);
-								// Gets the maximum outgoing rate.
-		int						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.
-		int						get_outgoing_rate() const;
-								// Returns the average incoming rate over the last second.
-		int						get_incoming_rate() const;
-								// Returns the average incoming packet loss over the last 5 seconds.
-		float					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 int time) const;
-								// Sends an unreliable message, in order and without duplicates.
-		int						send_message(os::UDPSocket &socket, const int time, const BitMessage &msg);
-		
-								// Sends the next fragment if the last message was too large to send at once.
-		void					send_next_fragment(os::UDPSocket &socket, const int time);
-								// Returns true if there are unsent fragments left.
-		bool					unsent_fragments_left() const;
+							Connection(Allocator& allocator);
+							~Connection();
 
-								// 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					process(const os::NetAddress from, BitMessage &msg, int &sequence, int time);
+	void					init(const os::NetAddress addr, const int id);
+	void					shutdown();
+	void					reset_rate();
 
-								// 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					get_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(int rate);
+							// Gets the maximum outgoing rate.
+	int						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.
+	int						get_outgoing_rate() const;
+							// Returns the average incoming rate over the last second.
+	int						get_incoming_rate() const;
+							// Returns the average incoming packet loss over the last 5 seconds.
+	float					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 int time) const;
+							// Processes the incoming message.
+	bool					process(const os::NetAddress from, int time, BitMessage &msg, int &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();
 
-	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
-		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);
+private:
+							// methods which provides a pattern for communication
+	void					_write_message(BitMessage &out, const BitMessage &msg);
+	bool					_read_message(BitMessage &out, const BitMessage &msg);
 
-	private:
-	  
-		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
+							// 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);
 
-					// 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
+private:
+  
+	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
 
-					// 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
+				// 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
 
-					// 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;
-  
-					// sequencing variables
-		int						m_outgoing_sequence;
-		int						m_incoming_sequence;
+				// 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
 
-					// outgoing fragment buffer
-		bool					m_unsent_fragments;
-		int						m_unsent_fragment_start;
-		uint8_t					m_unsent_buffer[MAX_MESSAGE_SIZE];
-		BitMessage				m_unsent_msg;
+				// 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;
 
-					// incoming fragment assembly buffer
-		int						m_fragment_sequence;
-		int						m_fragment_length;
-		uint8_t					m_fragment_buffer[MAX_MESSAGE_SIZE];
+				// sequencing variables
+	int						m_outgoing_sequence;
+	int						m_incoming_sequence;
 
-					// reliable messages
-		Queue<BitMessage>		m_reliable_send;
-		Queue<BitMessage>		m_reliable_receive;
-	  
-	};
+				// reliable messages
+	Queue<BitMessage>		m_reliable_send;
+	Queue<BitMessage>		m_reliable_receive;
+};
 
 } // namespace network
 } // namespace crown