mikymod 13 лет назад
Родитель
Сommit
b4eaeff87b
3 измененных файлов с 27 добавлено и 27 удалено
  1. 19 17
      src/network/AsyncConnection.cpp
  2. 1 3
      src/network/AsyncConnection.h
  3. 7 7
      src/network/BitMessage.cpp

+ 19 - 17
src/network/AsyncConnection.cpp

@@ -70,12 +70,19 @@ void AsyncConnection::stop()
 {
 {
 	// if connection is running
 	// if connection is running
 	assert(m_running);
 	assert(m_running);
-	os::printf("stop connection\n");;
-//  	bool connected = is_connected();
-	_clear_data();
-	// close socket
-	m_socket.close();
-	m_running = false;
+	os::printf("stopping connection...\n");;
+
+	if (is_connected())
+	{
+		os::printf("connection stopped\n");;
+		_clear_data();
+		// close socket
+		m_socket.close();
+		m_running = false;
+		return;
+	}
+	
+	os::printf("connection is running yet");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -168,6 +175,10 @@ void AsyncConnection::send_message(BitMessage& msg, const uint32_t time)
 	assert(m_running);
 	assert(m_running);
 	
 	
 	m_socket.send(m_remote_address, msg.get_data(), msg.get_size());
 	m_socket.send(m_remote_address, msg.get_data(), msg.get_size());
+	
+// 	_packet_sent(msg.get_size());
+	
+	m_remote_sequence++;
 
 
 }
 }
 
 
@@ -175,16 +186,13 @@ void AsyncConnection::send_message(BitMessage& msg, const uint32_t time)
 int32_t AsyncConnection::receive_message(BitMessage& msg, const uint32_t time)
 int32_t AsyncConnection::receive_message(BitMessage& msg, const uint32_t time)
 {
 {
 	assert(m_running);
 	assert(m_running);
-	// init BitMessage handler
-	msg.init(175);
+
 	msg.begin_writing();
 	msg.begin_writing();
-	size_t size = 175;
+	size_t size = msg.get_max_size();
 	// NetAddress handler
 	// NetAddress handler
 	os::NetAddress sender(0, 0, 0, 0, 0);
 	os::NetAddress sender(0, 0, 0, 0, 0);
 	// receive message
 	// receive message
 	int32_t bytes = m_socket.receive(sender, msg.get_data(), size);
 	int32_t bytes = m_socket.receive(sender, msg.get_data(), size);
-	//TODO: why received bytes is zero
-	os::printf("%d bytes received\n", bytes);
 	msg.set_size(size);
 	msg.set_size(size);
 	// sets BitMessage in only-read
 	// sets BitMessage in only-read
 	msg.begin_reading();
 	msg.begin_reading();
@@ -269,12 +277,6 @@ bool AsyncConnection::ready_to_send(const int time) const
 	return ((m_last_data_bytes - ((delta_time * m_max_rate) / 1000)) <= 0);
 	return ((m_last_data_bytes - ((delta_time * m_max_rate) / 1000)) <= 0);
 }
 }
 
 
-//-----------------------------------------------------------------------------
-bool AsyncConnection::process(const os::NetAddress from, int time, BitMessage &msg, int &sequence)
-{
-
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 bool AsyncConnection::is_connecting() const
 bool AsyncConnection::is_connecting() const
 { 
 { 

+ 1 - 3
src/network/AsyncConnection.h

@@ -56,7 +56,7 @@ public:
 	uint16_t						get_remote_sequence() const;
 	uint16_t						get_remote_sequence() const;
 									// Sends message
 									// Sends message
 	void							send_message(BitMessage& msg, const uint32_t time);
 	void							send_message(BitMessage& msg, const uint32_t time);
-									// Receive message
+									// Receive message and process
 	int32_t							receive_message(BitMessage& msg, const uint32_t time);
 	int32_t							receive_message(BitMessage& msg, const uint32_t time);
 									// Removes any pending outgoing or incoming reliable messages.
 									// Removes any pending outgoing or incoming reliable messages.
 	void							clear_reliable_messages();
 	void							clear_reliable_messages();
@@ -64,8 +64,6 @@ public:
 	void 							update(real delta);
 	void 							update(real delta);
 									// Returns true if the connection is ready to send new data based on the maximum rate.
 									// Returns true if the connection is ready to send new data based on the maximum rate.
 	bool							ready_to_send(const int32_t time) const;
 	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);
 	
 	
 	bool 							is_connecting() const;
 	bool 							is_connecting() const;
 	bool 							is_listening() const; 
 	bool 							is_listening() const; 

+ 7 - 7
src/network/BitMessage.cpp

@@ -414,23 +414,23 @@ void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
 	else 
 	else 
 	{
 	{
 		int32_t i;
 		int32_t i;
-		int32_t l;
+		int32_t len = std::strlen(s);
 		uint8_t* data_ptr;
 		uint8_t* data_ptr;
 		const uint8_t* byte_ptr;
 		const uint8_t* byte_ptr;
 		
 		
 		// calculates length
 		// calculates length
-		for (l = 0; s[l]; l++) {}
+		len = std::strlen(s);
 		
 		
-		if (max_len >= 0 && l >= max_len) 
+		if (max_len >= 0 && len >= max_len) 
 		{
 		{
-			l = max_len - 1;
+			len = max_len - 1;
 		}
 		}
 		
 		
-		data_ptr = get_byte_space(l + 1);
+		data_ptr = get_byte_space(len + 1);
 		byte_ptr = reinterpret_cast<const uint8_t*>(s);
 		byte_ptr = reinterpret_cast<const uint8_t*>(s);
 		if (make_7_bit) 
 		if (make_7_bit) 
 		{
 		{
-			for (i = 0; i < l; i++) 
+			for (i = 0; i < len; i++) 
 			{
 			{
 				if ( byte_ptr[i] > 127 ) 
 				if ( byte_ptr[i] > 127 ) 
 				{
 				{
@@ -444,7 +444,7 @@ void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
 		}
 		}
 		else 
 		else 
 		{
 		{
-			for (i = 0; i < l; i++) 
+			for (i = 0; i < len; i++) 
 			{
 			{
 				data_ptr[i] = byte_ptr[i];
 				data_ptr[i] = byte_ptr[i];
 			}
 			}