mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-28 18:13:52 +00:00
fixed problem where restarting a network game...
...after already playing one doesn't work properly
This commit is contained in:
parent
608d14311b
commit
3a7d0ad85f
@ -57,6 +57,9 @@ connection_acceptor::connection_acceptor(config& players)
|
||||
positions_[*i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//if we have any connected players when we are created, send them the data
|
||||
network::send_data(players_);
|
||||
}
|
||||
|
||||
int connection_acceptor::do_action()
|
||||
@ -157,8 +160,7 @@ int connection_acceptor::do_action()
|
||||
<< "\n";
|
||||
}
|
||||
} else {
|
||||
std::cerr << "tried to take unknown side: " << side_taken
|
||||
<< "\n";
|
||||
std::cerr << "tried to take unknown side: " << side_taken << "\n";
|
||||
}
|
||||
|
||||
return CONNECTIONS_PART_FILLED;
|
||||
@ -211,9 +213,11 @@ bool accept_network_connections(display& disp, config& players)
|
||||
|
||||
}
|
||||
|
||||
void play_multiplayer(display& disp, game_data& units_data, config& cfg,
|
||||
game_state& state)
|
||||
void play_multiplayer(display& disp, game_data& units_data, config cfg,
|
||||
game_state& state, bool server)
|
||||
{
|
||||
log_scope("play multiplayer");
|
||||
|
||||
std::vector<std::string> options;
|
||||
std::vector<config*>& levels = cfg.children["multiplayer"];
|
||||
for(std::vector<config*>::iterator i = levels.begin(); i!=levels.end();++i){
|
||||
@ -325,7 +329,7 @@ void play_multiplayer(display& disp, game_data& units_data, config& cfg,
|
||||
}
|
||||
|
||||
const network::manager net_manager;
|
||||
const network::server_manager server_man;
|
||||
const network::server_manager server_man(15000,server);
|
||||
|
||||
const bool network_state = accept_network_connections(disp,level);
|
||||
if(network_state == false)
|
||||
|
@ -23,6 +23,6 @@ void play_multiplayer_client(display& disp, game_data& units_data,
|
||||
config& cfg, game_state& state);
|
||||
|
||||
void play_multiplayer(display& disp, game_data& units_data,
|
||||
config& cfg, game_state& state);
|
||||
config cfg, game_state& state, bool server=true);
|
||||
|
||||
#endif
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "language.hpp"
|
||||
#include "log.hpp"
|
||||
#include "multiplayer.hpp"
|
||||
#include "multiplayer_client.hpp"
|
||||
#include "playlevel.hpp"
|
||||
@ -51,7 +52,19 @@ GAME_LIST_RESULT manage_game_list(display& disp, const config* gamelist)
|
||||
if(res == gamelist_manager::UPDATED_GAMELIST) {
|
||||
gamelist = manager.get_gamelist();
|
||||
} else if(res == 0) {
|
||||
return CREATE_GAME;
|
||||
std::string name;
|
||||
const int res = gui::show_dialog(disp,NULL,"","Name your game:",
|
||||
gui::OK_CANCEL,NULL,NULL,"Name:",&name);
|
||||
if(res == 0) {
|
||||
config response;
|
||||
config create_game;
|
||||
create_game["name"] = name;
|
||||
response.children["create_game"].push_back(
|
||||
new config(create_game));
|
||||
network::send_data(response);
|
||||
|
||||
return CREATE_GAME;
|
||||
}
|
||||
} else if(size_t(res) == options.size()-1) {
|
||||
return QUIT_GAME;
|
||||
} else if(res > 0 && size_t(res) < options.size()) {
|
||||
@ -77,6 +90,8 @@ GAME_LIST_RESULT manage_game_list(display& disp, const config* gamelist)
|
||||
void play_multiplayer_client(display& disp, game_data& units_data, config& cfg,
|
||||
game_state& state)
|
||||
{
|
||||
log_scope("playing multiplayer client");
|
||||
|
||||
const network::manager net_manager;
|
||||
|
||||
std::string host;
|
||||
@ -103,13 +118,17 @@ void play_multiplayer_client(display& disp, game_data& units_data, config& cfg,
|
||||
if(gamelist != NULL) {
|
||||
const GAME_LIST_RESULT res = manage_game_list(disp,gamelist);
|
||||
switch(res) {
|
||||
case QUIT_GAME:
|
||||
case QUIT_GAME: {
|
||||
return;
|
||||
case CREATE_GAME:
|
||||
play_multiplayer(disp,units_data,cfg,state);
|
||||
}
|
||||
case CREATE_GAME: {
|
||||
std::cerr << "playing multiplayer...\n";
|
||||
play_multiplayer(disp,units_data,cfg,state,false);
|
||||
return;
|
||||
case JOIN_GAME:
|
||||
}
|
||||
case JOIN_GAME: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
|
@ -21,8 +21,14 @@ TCPsocket server_socket;
|
||||
|
||||
namespace network {
|
||||
|
||||
manager::manager()
|
||||
manager::manager() : free_(true)
|
||||
{
|
||||
//if the network is already being managed
|
||||
if(socket_set) {
|
||||
free_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(SDLNet_Init() == -1) {
|
||||
throw error(SDL_GetError());
|
||||
}
|
||||
@ -34,20 +40,23 @@ manager::~manager()
|
||||
{
|
||||
disconnect();
|
||||
SDLNet_FreeSocketSet(socket_set);
|
||||
socket_set = 0;
|
||||
SDLNet_Quit();
|
||||
}
|
||||
|
||||
server_manager::server_manager(int port, bool create_server)
|
||||
: free_(false)
|
||||
{
|
||||
if(create_server) {
|
||||
if(create_server && !server_socket) {
|
||||
server_socket = connect("",port);
|
||||
std::cerr << "server socket initialized: " << server_socket << "\n";
|
||||
free_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
server_manager::~server_manager()
|
||||
{
|
||||
if(server_socket) {
|
||||
if(free_) {
|
||||
SDLNet_TCP_Close(server_socket);
|
||||
server_socket = 0;
|
||||
}
|
||||
@ -89,7 +98,9 @@ connection connect(const std::string& host, int port)
|
||||
|
||||
connection accept_connection()
|
||||
{
|
||||
assert(server_socket);
|
||||
if(!server_socket)
|
||||
return 0;
|
||||
|
||||
const connection sock = SDLNet_TCP_Accept(server_socket);
|
||||
if(sock) {
|
||||
const int res = SDLNet_TCP_AddSocket(socket_set,sock);
|
||||
@ -124,6 +135,11 @@ void disconnect(connection s)
|
||||
sockets.erase(i);
|
||||
SDLNet_TCP_DelSocket(socket_set,s);
|
||||
SDLNet_TCP_Close(s);
|
||||
} else {
|
||||
std::cerr << "Could not find socket to close: " << (int)s << "\n";
|
||||
if(sockets.size() == 1) {
|
||||
std::cerr << "valid socket: " << (int)*sockets.begin() << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,6 +180,7 @@ connection receive_data(config& cfg, connection connection_num, int timeout)
|
||||
}
|
||||
|
||||
if(buffer == "") {
|
||||
std::cerr << "error receiving data: " << (int)*i << "\n";
|
||||
throw error("error receiving data",*i);
|
||||
}
|
||||
|
||||
@ -195,7 +212,7 @@ connection receive_data(config& cfg, connection connection_num, int timeout)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void send_data(config& cfg, connection connection_num)
|
||||
void send_data(const config& cfg, connection connection_num)
|
||||
{
|
||||
log_scope("sending data");
|
||||
if(!connection_num) {
|
||||
|
@ -12,11 +12,17 @@ namespace network {
|
||||
struct manager {
|
||||
manager();
|
||||
~manager();
|
||||
|
||||
private:
|
||||
bool free_;
|
||||
};
|
||||
|
||||
struct server_manager {
|
||||
server_manager(int port=15000, bool create_server=true);
|
||||
~server_manager();
|
||||
|
||||
private:
|
||||
bool free_;
|
||||
};
|
||||
|
||||
typedef TCPsocket connection;
|
||||
@ -29,11 +35,12 @@ void disconnect(connection connection_num=0);
|
||||
|
||||
connection receive_data(config& cfg, connection connection_num=0, int tout=0);
|
||||
|
||||
void send_data(config& cfg, connection connection_num=0);
|
||||
void send_data(const config& cfg, connection connection_num=0);
|
||||
|
||||
struct error
|
||||
{
|
||||
error(const std::string& msg, connection sock=0) : message(msg) {}
|
||||
error(const std::string& msg, connection sock=0)
|
||||
: message(msg), socket(sock) {}
|
||||
std::string message;
|
||||
connection socket;
|
||||
|
||||
|
@ -344,7 +344,6 @@ void show_preferences_dialog(display& disp)
|
||||
bool redraw_all = true;
|
||||
|
||||
for(;;) {
|
||||
log_scope("looping");
|
||||
int mousex, mousey;
|
||||
const int mouse_flags = SDL_GetMouseState(&mousex,&mousey);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user