[Wesnoth-cvs-commits] wesnoth/src network_worker.cpp (August 05, 2005 - 00:38)

 

CVSROOT:	/cvsroot/wesnoth
Module name:	wesnoth
Branch: 	
Changes by:	Jon Daniel <j_daniel@xxxxxxxxxxxxxxxx>	05/08/04 22:05:12

Modified files:
	src            : network_worker.cpp 

Log message:
	Fix send and receives not being stopped when trying to close a socket which caused it wait until everything is finished.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network_worker.cpp.diff?tr1=1.32&tr2=1.33&r1=text&r2=text

Patches:
Index: wesnoth/src/network_worker.cpp
diff -u wesnoth/src/network_worker.cpp:1.32 wesnoth/src/network_worker.cpp:1.33
--- wesnoth/src/network_worker.cpp:1.32	Wed Jul 20 08:22:37 2005
+++ wesnoth/src/network_worker.cpp	Thu Aug  4 22:05:12 2005
@@ -1,4 +1,4 @@
-/* $Id: network_worker.cpp,v 1.32 2005/07/20 08:22:37 ott Exp $ */
+/* $Id: network_worker.cpp,v 1.33 2005/08/04 22:05:12 j_daniel Exp $ */
 /*
    Copyright (C) 2003-5 by David White <davidnwhite@xxxxxxxxxxx>
    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
@@ -55,7 +55,7 @@
 typedef std::deque<buffer> received_queue;
 received_queue received_data_queue;
 
-enum SOCKET_STATE { SOCKET_READY, SOCKET_LOCKED, SOCKET_ERROR };
+enum SOCKET_STATE { SOCKET_READY, SOCKET_LOCKED, SOCKET_ERROR, SOCKET_INTERRUPT };
 typedef std::map<TCPsocket,SOCKET_STATE> socket_state_map;
 socket_state_map sockets_locked;
 int socket_errors = 0;
@@ -87,6 +87,13 @@
 	current_transfer_stats.second = len;
 
 	while(beg != end) {
+		{
+			// if we are receiving the socket is in sockets_locked
+			// check if it is still locked
+			const threading::lock lock(*global_mutex);
+			if(sockets_locked[sock] != SOCKET_LOCKED)
+				return SOCKET_ERROR;
+		}
 		const int len = SDLNet_TCP_Recv(sock,beg,end - beg);
 		if(len <= 0) {
 			return SOCKET_ERROR;
@@ -166,6 +173,14 @@
 		if(sent_buf != NULL) {
 			std::vector<char> &v = sent_buf->buf;
 			for(size_t upto = 0, size = v.size(); result != SOCKET_ERROR && upto < size; ) {
+				{
+					// check if the socket is still locked
+					const threading::lock lock(*global_mutex);
+					if(sockets_locked[sent_buf->sock] != SOCKET_LOCKED) {
+						result = SOCKET_ERROR;
+						break;
+					}
+				}
 				const int bytes_to_send = int(size - upto);
 				const int res = SDLNet_TCP_Send(sent_buf->sock, &v[upto], bytes_to_send);
 				if(res < 0 || res != bytes_to_send && errno != EAGAIN) {
@@ -338,15 +353,19 @@
 		}
 
 		const socket_state_map::iterator lock_it = sockets_locked.find(sock);
-
-		if(lock_it == sockets_locked.end() || lock_it->second != SOCKET_LOCKED) {
-			if(lock_it != sockets_locked.end()) {
-				sockets_locked.erase(lock_it);
-			}
-
+		if(lock_it == sockets_locked.end()) {
 			remove_buffers(sock);
-
 			break;
+		}
+			
+		if(lock_it->second != SOCKET_LOCKED) {
+			if(lock_it->second != SOCKET_INTERRUPT) {
+				sockets_locked.erase(lock_it);
+				remove_buffers(sock);
+				break;
+			}
+		} else {
+			lock_it->second = SOCKET_INTERRUPT;
 		}
 	}
 }



You are on the gna.org mail server.

Generated by mhonarc, Tue Sep 20 16:52:51 2005