wesnoth-cvs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-cvs-commits] wesnoth/src network.cpp network_worker.cpp


From: Jon Daniel
Subject: [Wesnoth-cvs-commits] wesnoth/src network.cpp network_worker.cpp
Date: Wed, 17 Aug 2005 16:37:20 -0400

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     Jon Daniel <address@hidden>     05/08/17 20:37:20

Modified files:
        src            : network.cpp network_worker.cpp 

Log message:
        Use non-blocking IO for TCP sockets in order to be able to receive more 
then 1 byte at a time while still being able to cancel a receive.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network.cpp.diff?tr1=1.74&tr2=1.75&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network_worker.cpp.diff?tr1=1.41&tr2=1.42&r1=text&r2=text

Patches:
Index: wesnoth/src/network.cpp
diff -u wesnoth/src/network.cpp:1.74 wesnoth/src/network.cpp:1.75
--- wesnoth/src/network.cpp:1.74        Tue Aug  9 08:00:26 2005
+++ wesnoth/src/network.cpp     Wed Aug 17 20:37:20 2005
@@ -1,4 +1,4 @@
-/* $Id: network.cpp,v 1.74 2005/08/09 08:00:26 j_daniel Exp $ */
+/* $Id: network.cpp,v 1.75 2005/08/17 20:37:20 j_daniel Exp $ */
 /*
    Copyright (C) 2003-5 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
@@ -33,6 +33,12 @@
 #include <vector>
 
 #include <signal.h>
+#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
+#include <windows.h>
+#else
+#include <fcntl.h>
+#define SOCKET int
+#endif
 
 #define LOG_NW LOG_STREAM(info, network)
 #define WRN_NW LOG_STREAM(warn, network)
@@ -273,14 +279,22 @@
        }
 }
 
+namespace {
+       struct _TCPsocket {
+               int ready;
+               SOCKET channel;
+               IPaddress remoteAddress;
+               IPaddress localAddress;
+               int sflag;
+       };
+}
+
 void connect_operation::run()
 {
        char* const hostname = host_.empty() ? NULL : 
const_cast<char*>(host_.c_str());
        IPaddress ip;
        if(SDLNet_ResolveHost(&ip,hostname,port_) == -1) {
                error_ = "Could not connect to host";
-               const threading::lock l(get_mutex());
-               while(!notify_finished());
                return;
        }
 
@@ -288,16 +302,25 @@
        if(!sock) {
                error_ = hostname == NULL ? "Could not bind to port" :
                                            "Could not connect to host";
-               const threading::lock l(get_mutex());
-               while(!notify_finished());
                return;
        }
 
+// use non blocking IO
+#ifdef O_NONBLOCK
+       fcntl(((_TCPsocket*)sock)->channel, F_SETFL, O_NONBLOCK);
+#else
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+       {
+               unsigned long mode = 1;
+               ioctlsocket (((_TCPsocket*)sock)->channel, FIONBIO, &mode);
+       }
+#endif
+#endif
+
        //if this is a server socket
        if(hostname == NULL) {
                const threading::lock l(get_mutex());
                connect_ = create_connection(sock,"",port_);
-               while(!notify_finished());
                return;
        }
 
@@ -307,20 +330,17 @@
        const int nbytes = SDLNet_TCP_Send(sock,buf,4);
        if(nbytes != 4) {
                SDLNet_TCP_Close(sock);
-
                error_ = "Could not send initial handshake";
-               const threading::lock l(get_mutex());
-               while(!notify_finished());
                return;
        }
 
+       //no blocking operations from here on
+       const threading::lock l(get_mutex());
        LOG_NW << "sent handshake...\n";
 
-       const threading::lock l(get_mutex());
        if(is_aborted()) {
                LOG_NW << "connect operation aborted by calling thread\n";
                SDLNet_TCP_Close(sock);
-               while(!notify_finished());
                return;
        }
 
@@ -331,7 +351,6 @@
        if(res == -1) {
                SDLNet_TCP_Close(sock);
                error_ = "Could not add socket to socket set";
-               while(!notify_finished());
                return;
        }
 
Index: wesnoth/src/network_worker.cpp
diff -u wesnoth/src/network_worker.cpp:1.41 wesnoth/src/network_worker.cpp:1.42
--- wesnoth/src/network_worker.cpp:1.41 Sun Aug  7 12:49:08 2005
+++ wesnoth/src/network_worker.cpp      Wed Aug 17 20:37:20 2005
@@ -1,4 +1,4 @@
-/* $Id: network_worker.cpp,v 1.41 2005/08/07 12:49:08 j_daniel Exp $ */
+/* $Id: network_worker.cpp,v 1.42 2005/08/17 20:37:20 j_daniel Exp $ */
 /*
    Copyright (C) 2003-5 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
@@ -133,6 +133,13 @@
                transfer_stats[sock].second.fresh_current(len);
        }
 
+       // wait for a maximum of 15 seconds for the socket to have activity
+       if(SDLNet_CheckSockets(set, 15000) <= 0) {
+               ERR_NW << "SDLNet_CheckSockets: " << strerror(errno) << "\n";
+               SDLNet_TCP_DelSocket(set, sock);
+               SDLNet_FreeSocketSet(set);
+               return SOCKET_ERROR;
+       }
        while(beg != end) {
                {
                        // if we are receiving the socket is in sockets_locked
@@ -144,25 +151,24 @@
                                return SOCKET_ERROR;
                        }
                }
-               // wait for a maximum of 15 seconds for the socket to have 
activity
-               if(SDLNet_CheckSockets(set, 15000) <= 0) {
-                       ERR_NW << "SDLNet_CheckSockets: " << strerror(errno) << 
"\n";
-                       SDLNet_TCP_DelSocket(set, sock);
-                       SDLNet_FreeSocketSet(set);
-                       return SOCKET_ERROR;
-               }
 
-               if(SDLNet_TCP_Recv(sock, beg, 1) <= 0) {
-                       SDLNet_TCP_DelSocket(set, sock);
-                       SDLNet_FreeSocketSet(set);
-                       return SOCKET_ERROR;
+               const ssize_t res = SDLNet_TCP_Recv(sock, beg, end - beg);
+               if(res <= 0) {
+                       if(SDLNet_CheckSockets(set, 15000) <= 0) {
+                               ERR_NW << "SDLNet_CheckSockets: " << 
strerror(errno) << "\n";
+                               SDLNet_TCP_DelSocket(set, sock);
+                               SDLNet_FreeSocketSet(set);
+                               return SOCKET_ERROR;
+                       }
+                       continue;
                }
-               ++beg;
+
+               beg += res;
                {
                        const threading::lock lock(*global_mutex);
                        network::statistics& stats = 
transfer_stats[sock].second;
-                       ++stats.total;
-                       ++stats.current;
+                       stats.total += res;
+                       stats.current += res;
                }
        }
        SDLNet_TCP_DelSocket(set, sock);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]