changeset 20645:ea3b420db268 draft

(svn r25597) -Fix [FS#5635]: [Content] When the server closed the connection, the client would for eternity try to read a packet and never timeout making it impossible to reconnect
author rubidium <rubidium@openttd.org>
date Sat, 13 Jul 2013 09:26:11 +0000
parents e83c7f64f1d3
children a6bc0442a71b
files src/network/core/tcp_content.cpp src/network/core/tcp_content.h src/network/network_content.cpp
diffstat 3 files changed, 12 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/network/core/tcp_content.cpp
+++ b/src/network/core/tcp_content.cpp
@@ -183,8 +183,9 @@
 
 /**
  * Receive a packet at TCP level
+ * @return Whether at least one packet was received.
  */
-void NetworkContentSocketHandler::ReceivePackets()
+bool NetworkContentSocketHandler::ReceivePackets()
 {
 	/*
 	 * We read only a few of the packets. This as receiving packets can be expensive
@@ -206,12 +207,15 @@
 	 * What arbitrary number to choose is the ultimate question though.
 	 */
 	Packet *p;
-	int i = 42;
+	static const int MAX_PACKETS_TO_RECEIVE = 42;
+	int i = MAX_PACKETS_TO_RECEIVE;
 	while (--i != 0 && (p = this->ReceivePacket()) != NULL) {
 		bool cont = this->HandlePacket(p);
 		delete p;
-		if (!cont) return;
+		if (!cont) return true;
 	}
+
+	return i != MAX_PACKETS_TO_RECEIVE - 1;
 }
 
 
--- a/src/network/core/tcp_content.h
+++ b/src/network/core/tcp_content.h
@@ -206,7 +206,7 @@
 	/** On destructing of this class, the socket needs to be closed */
 	virtual ~NetworkContentSocketHandler() { this->Close(); }
 
-	void ReceivePackets();
+	bool ReceivePackets();
 };
 
 #ifndef OPENTTD_MSU
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -778,8 +778,10 @@
 	}
 
 	if (this->CanSendReceive()) {
-		this->ReceivePackets();
-		this->lastActivity = _realtime_tick;
+		if (this->ReceivePackets()) {
+			/* Only update activity once a packet is received, instead of everytime we try it. */
+			this->lastActivity = _realtime_tick;
+		}
 	}
 
 	this->SendPackets();