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

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

[Wesnoth-cvs-commits] wesnoth/src Makefile.am ai.cpp multiplayer_clie...


From: David White
Subject: [Wesnoth-cvs-commits] wesnoth/src Makefile.am ai.cpp multiplayer_clie...
Date: Mon, 11 Oct 2004 19:53:41 -0400

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     David White <address@hidden>    04/10/11 23:46:44

Modified files:
        src            : Makefile.am ai.cpp multiplayer_client.cpp 
                         network.cpp network.hpp playlevel.cpp 
                         replay.cpp show_dialog.cpp 
        src/server     : Makefile.am server.cpp 
Added files:
        src            : network_worker.cpp network_worker.hpp 
                         thread.cpp thread.hpp 

Log message:
        added experimental threading code

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network_worker.cpp?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network_worker.hpp?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/thread.cpp?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/thread.hpp?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/Makefile.am.diff?tr1=1.51&tr2=1.52&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/ai.cpp.diff?tr1=1.120&tr2=1.121&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/multiplayer_client.cpp.diff?tr1=1.69&tr2=1.70&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network.cpp.diff?tr1=1.43&tr2=1.44&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/network.hpp.diff?tr1=1.19&tr2=1.20&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/playlevel.cpp.diff?tr1=1.155&tr2=1.156&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/replay.cpp.diff?tr1=1.81&tr2=1.82&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/show_dialog.cpp.diff?tr1=1.96&tr2=1.97&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/server/Makefile.am.diff?tr1=1.12&tr2=1.13&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/server/server.cpp.diff?tr1=1.57&tr2=1.58&r1=text&r2=text

Patches:
Index: wesnoth/src/Makefile.am
diff -u wesnoth/src/Makefile.am:1.51 wesnoth/src/Makefile.am:1.52
--- wesnoth/src/Makefile.am:1.51        Sun Sep 12 22:39:57 2004
+++ wesnoth/src/Makefile.am     Mon Oct 11 23:46:39 2004
@@ -67,6 +67,7 @@
                  multiplayer_connect.cpp \
                  multiplayer_lobby.cpp \
                  network.cpp \
+                 network_worker.cpp \
                  pathfind.cpp \
                  pathutils.cpp \
                  playlevel.cpp \
@@ -83,6 +84,7 @@
                  team.cpp \
                  terrain.cpp \
                  theme.cpp \
+                 thread.cpp \
                  titlescreen.cpp \
                  tooltips.cpp \
                  unit.cpp \
@@ -139,6 +141,7 @@
                  multiplayer_connect.hpp \
                  multiplayer_lobby.hpp \
                  network.hpp \
+                 network_worker.hpp \
                  pathfind.hpp \
                  pathutils.hpp \
                  playlevel.hpp \
@@ -156,6 +159,7 @@
                  team.hpp \
                  terrain.hpp \
                  theme.hpp \
+                 thread.hpp \
                  titlescreen.hpp \
                  tooltips.hpp \
                  unit.hpp \
Index: wesnoth/src/ai.cpp
diff -u wesnoth/src/ai.cpp:1.120 wesnoth/src/ai.cpp:1.121
--- wesnoth/src/ai.cpp:1.120    Mon Oct  4 22:45:37 2004
+++ wesnoth/src/ai.cpp  Mon Oct 11 23:46:39 2004
@@ -1,4 +1,4 @@
-/* $Id: ai.cpp,v 1.120 2004/10/04 22:45:37 Sirp Exp $ */
+/* $Id: ai.cpp,v 1.121 2004/10/11 23:46:39 Sirp Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -241,12 +241,19 @@
 bool ai_interface::recruit(const std::string& unit_name, location loc)
 {
        const std::set<std::string>& recruits = current_team().recruits();
-       const std::set<std::string>::const_iterator i = 
std::find(recruits.begin(),recruits.end(),unit_name);
+       std::cerr << "recruits: ";
+       for(std::set<std::string>::const_iterator mi = recruits.begin(); mi != 
recruits.end(); ++mi) {
+               std::cerr << "'" << *mi << "', ";
+       }
+       std::cerr << "\n";
+
+       const std::set<std::string>::const_iterator i = 
recruits.find(unit_name);
        if(i == recruits.end()) {
                return false;
        }
 
        const int num = std::distance(recruits.begin(),i);
+       std::cerr << "recruit '" << unit_name << "' number " << num << "\n";
 
        recorder.add_recruit(num,loc);
        replay_undo replay_guard(recorder);
Index: wesnoth/src/multiplayer_client.cpp
diff -u wesnoth/src/multiplayer_client.cpp:1.69 
wesnoth/src/multiplayer_client.cpp:1.70
--- wesnoth/src/multiplayer_client.cpp:1.69     Thu Oct  7 19:37:02 2004
+++ wesnoth/src/multiplayer_client.cpp  Mon Oct 11 23:46:39 2004
@@ -245,7 +245,7 @@
 
        if(host.empty()) {
                host = preferences::network_host();
-               const int res = gui::show_dialog(disp,NULL,"","",
+               const int res = gui::show_dialog(disp,NULL,_("Connect to 
Host"),"",
                                                 gui::OK_CANCEL,NULL,NULL,
                                                 _("Choose host to connect to") 
+ std::string(": "),&host);
                if(res != 0 || host.empty()) {
Index: wesnoth/src/network.cpp
diff -u wesnoth/src/network.cpp:1.43 wesnoth/src/network.cpp:1.44
--- wesnoth/src/network.cpp:1.43        Sun Sep 12 21:55:20 2004
+++ wesnoth/src/network.cpp     Mon Oct 11 23:46:39 2004
@@ -1,5 +1,6 @@
 #include "log.hpp"
 #include "network.hpp"
+#include "network_worker.hpp"
 #include "util.hpp"
 
 #include "SDL_net.h"
@@ -79,6 +80,18 @@
        get_connection_details(handle).remote_handle = remote_handle;
 }
 
+void check_error()
+{
+       const TCPsocket sock = network_worker_pool::detect_error();
+       if(sock) {
+               for(connection_map::const_iterator i = connections.begin(); i 
!= connections.end(); ++i) {
+                       if(i->second.sock == sock) {
+                               throw network::error("Error sending 
data",i->first);
+                       }
+               }
+       }
+}
+
 SDLNet_SocketSet socket_set = 0;
 typedef std::vector<network::connection> sockets_list;
 sockets_list sockets;
@@ -102,14 +115,13 @@
 partial_map received_data;
 partial_map::const_iterator current_connection = received_data.end();
 
-typedef std::multimap<network::connection,partial_buffer> send_queue_map;
-send_queue_map send_queue;
-
 TCPsocket server_socket;
 
 std::deque<network::connection> disconnection_queue;
 std::set<network::connection> bad_sockets;
 
+network_worker_pool::manager* worker_pool_man = NULL;
+
 }
 
 namespace network {
@@ -138,7 +150,7 @@
        network::disconnect(socket);
 }
 
-manager::manager() : free_(true)
+manager::manager(size_t nthreads) : free_(true)
 {
        //if the network is already being managed
        if(socket_set) {
@@ -158,12 +170,16 @@
        }
 
        socket_set = SDLNet_AllocSocketSet(64);
+
+       worker_pool_man = new network_worker_pool::manager(nthreads);
 }
 
 manager::~manager()
 {
        if(free_) {
                disconnect();
+               delete worker_pool_man;
+               worker_pool_man = NULL;
                SDLNet_FreeSocketSet(socket_set);
                socket_set = 0;
                SDLNet_Quit();
@@ -359,6 +375,11 @@
                return;
        }
 
+       const connection_map::iterator info = connections.find(s);
+       if(info != connections.end()) {
+               network_worker_pool::close_socket(info->second.sock);
+       }
+
        schemas.erase(s);
        bad_sockets.erase(s);
        received_data.erase(s);
@@ -393,6 +414,12 @@
 
 connection receive_data(config& cfg, connection connection_num, int timeout)
 {
+       if(!socket_set) {
+               return 0;
+       }
+
+       check_error();
+
        if(disconnection_queue.empty() == false) {
                const network::connection sock = disconnection_queue.front();
                disconnection_queue.pop_front();
@@ -526,6 +553,7 @@
 
 void send_data(const config& cfg, connection connection_num, size_t max_size, 
SEND_TYPE mode)
 {
+       std::cerr << "in send_data()...\n";
        if(cfg.empty()) {
                return;
        }
@@ -558,26 +586,20 @@
        const schema_map::iterator schema = schemas.find(connection_num);
        assert(schema != schemas.end());
 
-       std::string value(4,'x');
-       value += cfg.write_compressed(schema->second.outgoing);
+       const std::string& value = 
cfg.write_compressed(schema->second.outgoing);
 
 //     std::cerr << "--- SEND DATA to " << ((int)connection_num) << ": '"
 //               << cfg.write() << "'\n--- END SEND DATA\n";
 
-       char buf[4];
-       SDLNet_Write32(value.size()+1-4,buf);
-       std::copy(buf,buf+4,value.begin());
-
-       //place the data in the send queue
-       const send_queue_map::iterator itor = 
send_queue.insert(std::pair<network::connection,partial_buffer>(connection_num,partial_buffer()));
+       std::vector<char> buf(4 + value.size() + 1);
+       SDLNet_Write32(value.size()+1,&buf[0]);
+       std::copy(value.begin(),value.end(),buf.begin()+4);
+       buf.back() = 0;
 
-       itor->second.buf.resize(value.size()+1);
-       std::copy(value.begin(),value.end(),itor->second.buf.begin());
-       itor->second.buf.back() = 0;
+       const connection_map::iterator info = connections.find(connection_num);
+       assert(info != connections.end());
 
-       if(mode == SEND_DATA) {
-               process_send_queue(connection_num,max_size);
-       }
+       network_worker_pool::queue_data(info->second.sock,buf);
 }
 
 void queue_data(const config& cfg, connection connection_num)
@@ -587,62 +609,6 @@
 
 void process_send_queue(connection connection_num, size_t max_size)
 {
-       if(connection_num == 0) {
-               for(sockets_list::iterator i = sockets.begin(); i != 
sockets.end(); ++i) {
-                       process_send_queue(*i,max_size);
-               }
-
-               return;
-       }
-
-       if(max_size == 0) {
-               max_size = default_max_send_size;
-       }
-       
-       if(max_size != 0 && max_size < 8) {
-               max_size = 8;
-       }
-
-       connection_details& details = get_connection_details(connection_num);
-       const TCPsocket sock = details.sock;
-
-       std::pair<send_queue_map::iterator,send_queue_map::iterator> itor = 
send_queue.equal_range(connection_num);
-       if(itor.first != itor.second) {
-               std::vector<char>& buf = itor.first->second.buf;
-               size_t& upto = itor.first->second.upto;
-
-               size_t bytes_to_send = buf.size() - upto;
-               if(max_size != 0 && bytes_to_send > max_size) {
-                       bytes_to_send = max_size;
-               }
-
-               std::cerr << "sending " << bytes_to_send << " from send 
queue\n";
-
-               const int res = SDLNet_TCP_Send(sock,&buf[upto],bytes_to_send);
-               if(res < 0 || res != int(bytes_to_send) && errno != EAGAIN) {
-                       std::cerr << "sending data failed: " << res << "/" << 
bytes_to_send << "\n";
-                       throw error("Sending queued data 
failed",connection_num);
-               }
-
-               std::cerr << "sent.\n";
-
-               upto += res;
-               details.sent += res;
-
-               //if we've now sent the entire item, erase it from the send 
queue
-               if(upto == buf.size()) {
-                       std::cerr << "erasing item from the send queue\n";
-                       send_queue.erase(itor.first);
-               } else if(upto > buf.size()) {
-                       std::cerr << "ERROR: buffer overrun sending data\n";
-               }
-
-               //if we haven't sent 'max_size' bytes yet, try to go onto the 
next item in
-               //the queue by recursing
-               if(bytes_to_send < max_size || max_size == 0) {
-                       
process_send_queue(connection_num,max_size-bytes_to_send);
-               }
-       }
 }
 
 void send_data_all_except(const config& cfg, connection connection_num, size_t 
max_size)
@@ -682,6 +648,4 @@
                return 
std::pair<int,int>(current_connection->second.upto,current_connection->second.buf.size());
 }
 
-bool sends_queued() { return send_queue.empty() == false; }
-
 } //end namespace network
Index: wesnoth/src/network.hpp
diff -u wesnoth/src/network.hpp:1.19 wesnoth/src/network.hpp:1.20
--- wesnoth/src/network.hpp:1.19        Sun Sep 12 21:55:20 2004
+++ wesnoth/src/network.hpp     Mon Oct 11 23:46:40 2004
@@ -14,11 +14,14 @@
 //a network manager must be created before networking can be used.
 //it must be destroyed only after all networking activity stops.
 struct manager {
-       manager();
+       explicit manager(size_t nthreads=1);
        ~manager();
 
 private:
        bool free_;
+       
+       manager(const manager&);
+       void operator=(const manager&);
 };
 
 //a server manager causes listening on a given port to occur
@@ -134,8 +137,6 @@
        void disconnect();
 };
 
-bool sends_queued();
-
 }
 
 #endif
Index: wesnoth/src/playlevel.cpp
diff -u wesnoth/src/playlevel.cpp:1.155 wesnoth/src/playlevel.cpp:1.156
--- wesnoth/src/playlevel.cpp:1.155     Sun Oct  3 18:00:15 2004
+++ wesnoth/src/playlevel.cpp   Mon Oct 11 23:46:40 2004
@@ -1,4 +1,4 @@
-/* $Id: playlevel.cpp,v 1.155 2004/10/03 18:00:15 gruikya Exp $ */
+/* $Id: playlevel.cpp,v 1.156 2004/10/11 23:46:40 Sirp Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -187,6 +187,8 @@
        std::cerr << "initializing teams..." << unit_cfg.size() << "\n";;
        std::cerr << (SDL_GetTicks() - ticks) << "\n";
 
+       std::set<std::string> seen_save_ids;
+
        for(config::child_list::const_iterator ui = unit_cfg.begin(); ui != 
unit_cfg.end(); ++ui) {
                std::string save_id = (**ui)["save_id"];
 
@@ -194,6 +196,13 @@
                        save_id=(**ui)["description"];
                }
 
+               //make sure the 'save_id' is unique
+               while(seen_save_ids.count(save_id)) {
+                       save_id += "_";
+               }
+
+               seen_save_ids.insert(save_id);
+
                player_info *player = NULL;
 
                if((**ui)["controller"] == "human" ||
@@ -201,8 +210,8 @@
                   (**ui)["persistent"] == "1") {
                        player = state_of_game.get_player(save_id);
 
-                       if(!player && !save_id.empty()) {
-                               player=&state_of_game.players[save_id];
+                       if(player == NULL && !save_id.empty()) {
+                               player = &state_of_game.players[save_id];
                        }
                }
 
@@ -219,7 +228,7 @@
                std::cerr << "found gold: '" << gold << "'\n";
 
                int ngold = lexical_cast_default<int>(gold);
-               if(player && player->gold >= ngold) {
+               if(player != NULL && player->gold >= ngold) {
                        ngold = player->gold;
                }
 
@@ -227,13 +236,21 @@
 
                teams.push_back(team(**ui,ngold));
 
+               std::cerr << "team " << teams.size() << " can recruit: ";
+               const std::set<std::string>& recruits = teams.back().recruits();
+               for(std::set<std::string>::const_iterator r = recruits.begin(); 
r != recruits.end(); ++r) {
+                       std::cerr << "'" << *r << "', ";
+               }
+
+               std::cerr << "\n";
+
                //if this side tag describes the leader of the side
                if((**ui)["no_leader"] != "yes" && (**ui)["controller"] != 
"null") {
                        unit new_unit(gameinfo, **ui);
 
                        //search the recall list for leader units, and if there 
is
                        //one, use it in place of the config-described unit
-                       if(player) {
+                       if(player != NULL) {
                                for(std::vector<unit>::iterator it = 
player->available_units.begin(); it != player->available_units.end(); ++it) {
                                        if(it->can_recruit()) {
                                                new_unit = *it;
@@ -269,12 +286,12 @@
 
                //if the game state specifies units that can be recruited for 
the player
                //then add them
-               if(player && player->can_recruit.empty() == false) {
+               if(player != NULL && player->can_recruit.empty() == false) {
                        
std::copy(player->can_recruit.begin(),player->can_recruit.end(),
                                        
std::inserter(teams.back().recruits(),teams.back().recruits().end()));
                }
                
-               if(player) {
+               if(player != NULL) {
                        player->can_recruit = teams.back().recruits();
                }
                
Index: wesnoth/src/replay.cpp
diff -u wesnoth/src/replay.cpp:1.81 wesnoth/src/replay.cpp:1.82
--- wesnoth/src/replay.cpp:1.81 Sun Oct  3 19:23:10 2004
+++ wesnoth/src/replay.cpp      Mon Oct 11 23:46:40 2004
@@ -1,4 +1,4 @@
-/* $Id: replay.cpp,v 1.81 2004/10/03 19:23:10 Sirp Exp $ */
+/* $Id: replay.cpp,v 1.82 2004/10/11 23:46:40 Sirp Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -707,6 +707,15 @@
                                throw replay::error();
                        }
 
+                       std::cerr << "recruiting unit from options: ";
+                       for(std::set<std::string>::const_iterator r = 
recruits.begin(); r != recruits.end(); ++r) {
+                               std::cerr << "'" << *r << "', ";
+                       }
+
+                       std::cerr << "\n";
+
+                       std::cerr << "recruiting unit " << val << " '" << *itor 
<< "'\n";
+
                        unit new_unit(&(u_type->second),team_num,true);
                        const std::string& res = 
recruit_unit(map,team_num,units,new_unit,loc);
                        if(!res.empty()) {
Index: wesnoth/src/server/Makefile.am
diff -u wesnoth/src/server/Makefile.am:1.12 wesnoth/src/server/Makefile.am:1.13
--- wesnoth/src/server/Makefile.am:1.12 Fri Sep  3 11:29:56 2004
+++ wesnoth/src/server/Makefile.am      Mon Oct 11 23:46:43 2004
@@ -16,6 +16,8 @@
                   ../gettext.cpp \
                   ../log.cpp \
                   ../network.cpp \
+                  ../network_worker.cpp \
+                  ../thread.cpp \
                   game.hpp \
                   ../game_events.hpp \
                   input_stream.hpp \
@@ -26,7 +28,9 @@
                   ../game_config.hpp \
                   ../gettext.hpp \
                   ../log.hpp \
-                  ../network.hpp
+                  ../network.hpp \
+                  ../network_worker.hpp \
+                  ../thread.hpp
 
 AM_CXXFLAGS = @SDL_CFLAGS@ -DLOCALEDIR=\"$(LOCALEDIR)\"
 AM_LDFLAGS = @SDL_LIBS@ @SDL_NET_LIBS@
Index: wesnoth/src/server/server.cpp
diff -u wesnoth/src/server/server.cpp:1.57 wesnoth/src/server/server.cpp:1.58
--- wesnoth/src/server/server.cpp:1.57  Sun Sep 12 21:55:20 2004
+++ wesnoth/src/server/server.cpp       Mon Oct 11 23:46:44 2004
@@ -95,7 +95,7 @@
        std::vector<std::string> bans_;
 };
 
-server::server(int port, input_stream& input) : net_manager_(), server_(port), 
not_logged_in_(players_), lobby_players_(players_), last_stats_(time(NULL)), 
input_(input)
+server::server(int port, input_stream& input) : net_manager_(5), 
server_(port), not_logged_in_(players_), lobby_players_(players_), 
last_stats_(time(NULL)), input_(input)
 {
        login_response_.add_child("mustlogin");
        login_response_["version"] = game_config::version;
Index: wesnoth/src/show_dialog.cpp
diff -u wesnoth/src/show_dialog.cpp:1.96 wesnoth/src/show_dialog.cpp:1.97
--- wesnoth/src/show_dialog.cpp:1.96    Tue Sep 28 20:09:08 2004
+++ wesnoth/src/show_dialog.cpp Mon Oct 11 23:46:40 2004
@@ -1,4 +1,4 @@
-/* $Id: show_dialog.cpp,v 1.96 2004/09/28 20:09:08 silene Exp $ */
+/* $Id: show_dialog.cpp,v 1.97 2004/10/11 23:46:40 Sirp Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -865,15 +865,15 @@
                        }
                }
 
+               disp.video().flip();
+               SDL_Delay(10);
+
                if(action != NULL) {
                        const int act = action->do_action();
                        if(act != dialog_action::CONTINUE_DIALOG) {
                                return act;
                        }
                }
-
-               disp.video().flip();
-               SDL_Delay(10);
        }
 
        return -1;




reply via email to

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