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

 

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

Modified files:
	src            : network_worker.cpp 

Log message:
	Factor send_buf code.

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

Patches:
Index: wesnoth/src/network_worker.cpp
diff -u wesnoth/src/network_worker.cpp:1.33 wesnoth/src/network_worker.cpp:1.34
--- wesnoth/src/network_worker.cpp:1.33	Thu Aug  4 22:05:12 2005
+++ wesnoth/src/network_worker.cpp	Thu Aug  4 22:43:19 2005
@@ -1,4 +1,4 @@
-/* $Id: network_worker.cpp,v 1.33 2005/08/04 22:05:12 j_daniel Exp $ */
+/* $Id: network_worker.cpp,v 1.34 2005/08/04 22:43:19 j_daniel Exp $ */
 /*
    Copyright (C) 2003-5 by David White <davidnwhite@xxxxxxxxxxx>
    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
@@ -64,6 +64,33 @@
 
 std::vector<threading::thread*> threads;
 
+SOCKET_STATE send_buf(TCPsocket sock, std::vector<char>& buf) {
+	size_t upto = 0;
+	size_t size = buf.size();
+
+	current_transfer_stats.first = 0;
+	current_transfer_stats.second = static_cast<int>(size);
+
+	while(upto < size) {
+		{
+			// check if the socket is still locked
+			const threading::lock lock(*global_mutex);
+			if(sockets_locked[sock] != SOCKET_LOCKED)
+				return SOCKET_ERROR;
+		}
+		const int bytes_to_send = static_cast<int>(size - upto);
+		const int res = SDLNet_TCP_Send(sock, &buf[upto], bytes_to_send);
+
+		current_transfer_stats.first += res;
+
+		if(res < 0 || res != bytes_to_send && errno != EAGAIN)
+			return SOCKET_ERROR;
+
+		upto += res;
+	}
+	return SOCKET_READY;
+}
+
 SOCKET_STATE receive_buf(TCPsocket sock, std::vector<char>& buf)
 {
 	char num_buf[4];
@@ -171,25 +198,7 @@
 		std::vector<char> buf;
 
 		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) {
-					result = SOCKET_ERROR;
-				} else {
-					upto += res;
-				}
-			}
-
+			result = send_buf(sent_buf->sock, sent_buf->buf);
 			delete sent_buf;
 			sent_buf = NULL;
 		} else {



You are on the gna.org mail server.

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