Added automatic detection of packet types for server
This commit is contained in:
parent
527a8f0486
commit
8f1f16cc35
3 changed files with 61 additions and 51 deletions
|
@ -188,7 +188,7 @@ void game::start_game(const player_map::const_iterator starter) {
|
|||
// When the host advances tell everyone that the next scenario data is
|
||||
// available.
|
||||
static simple_wml::document notify_next_scenario("[notify_next_scenario]\n[/notify_next_scenario]\n", simple_wml::INIT_COMPRESSED);
|
||||
send_data(notify_next_scenario, starter->first, "game control");
|
||||
send_data(notify_next_scenario, starter->first);
|
||||
}
|
||||
// Send [observer] tags for all observers that are already in the game.
|
||||
send_observerjoins();
|
||||
|
@ -231,7 +231,7 @@ bool game::take_side(const player_map::const_iterator user)
|
|||
// Tell the host which side the new player should take.
|
||||
|
||||
simple_wml::string_span data = cfg.output_compressed();
|
||||
network::send_raw_data(data.begin(), data.size(), owner_, "game");
|
||||
network::send_raw_data(data.begin(), data.size(), owner_, cfg.root().first_child().to_string());
|
||||
DBG_GAME << "take_side: took side " << side_num << " because the name matched\n";
|
||||
DBG_GAME << debug_player_info();
|
||||
return true;
|
||||
|
@ -253,7 +253,7 @@ bool game::take_side(const player_map::const_iterator user)
|
|||
cfg.root().set_attr_dup("side", (**side)["side"]);
|
||||
// Tell the host which side the new player should take.
|
||||
simple_wml::string_span data = cfg.output_compressed();
|
||||
network::send_raw_data(data.begin(), data.size(), owner_, "game");
|
||||
network::send_raw_data(data.begin(), data.size(), owner_, cfg.root().first_child().to_string());
|
||||
DBG_GAME << "take_side: took the first free network side which was " << side_num << "\n";
|
||||
DBG_GAME << debug_player_info();
|
||||
return true;
|
||||
|
@ -430,7 +430,7 @@ void game::transfer_side_control(const network::connection sock, const simple_wm
|
|||
// Update the client side observer list for everyone except old player.
|
||||
simple_wml::document observer_join;
|
||||
observer_join.root().add_child("observer").set_attr_dup("name", old_player_name.c_str());
|
||||
send_data(observer_join, old_player, "game control");
|
||||
send_data(observer_join, old_player);
|
||||
// If the old player was the host of the game, choose another player.
|
||||
/*if (old_player == owner_) {
|
||||
host_leave = true;
|
||||
|
@ -497,7 +497,7 @@ void game::send_change_controller(const size_t side_num,
|
|||
|
||||
// Tell everyone but the new player that this side is network controlled now.
|
||||
change.set_attr("controller", controller.c_str());
|
||||
send_data(response, sock, "game control");
|
||||
send_data(response, sock);
|
||||
|
||||
// Tell the new player that he controls this side now.
|
||||
// Just don't send it when the player left the game. (The host gets the
|
||||
|
@ -505,7 +505,7 @@ void game::send_change_controller(const size_t side_num,
|
|||
if (!player_left) {
|
||||
// make copy so original text aren't going to modified
|
||||
change.set_attr("controller", std::string(controller).replace(0,strlen("network"),"human").c_str());
|
||||
send_to_one(response, sock, "game control");
|
||||
send_to_one(response, sock);
|
||||
}
|
||||
|
||||
// Update the level so observers who join get the new name.
|
||||
|
@ -533,7 +533,7 @@ void game::transfer_ai_sides() {
|
|||
drop.root().set_attr("side_drop", side_drop.c_str());
|
||||
drop.root().set_attr("controller", "ai");
|
||||
const simple_wml::string_span data = drop.output_compressed();
|
||||
network::send_raw_data(data.begin(), data.size(), owner_, "game");
|
||||
network::send_raw_data(data.begin(), data.size(), owner_, drop.root().first_child().to_string());
|
||||
sides_[side] = owner_;
|
||||
}
|
||||
if (ai_transfer) {
|
||||
|
@ -552,7 +552,7 @@ void game::notify_new_host(){
|
|||
cfg_host_transfer.set_attr("name", owner_name.c_str());
|
||||
cfg_host_transfer.set_attr("value", "1");
|
||||
const simple_wml::string_span data = cfg.output_compressed();
|
||||
network::send_raw_data(data.begin(), data.size(), owner_,"game");
|
||||
network::send_raw_data(data.begin(), data.size(), owner_,cfg.root().first_child().to_string());
|
||||
send_and_record_server_message((owner_name
|
||||
+ " has been chosen as the new host.").c_str());
|
||||
}
|
||||
|
@ -676,7 +676,7 @@ network::connection game::kick_member(const simple_wml::node& kick,
|
|||
// Tell the user to leave the game.
|
||||
static simple_wml::document leave_game("[leave_game]\n[/leave_game]\n", simple_wml::INIT_COMPRESSED);
|
||||
static const simple_wml::string_span leave_game_data = leave_game.output_compressed();
|
||||
network::send_raw_data(leave_game_data.begin(), leave_game_data.size(), user->first,"game");
|
||||
network::send_raw_data(leave_game_data.begin(), leave_game_data.size(), user->first,leave_game.root().first_child().to_string());
|
||||
remove_player(user->first);
|
||||
return user->first;
|
||||
}
|
||||
|
@ -717,7 +717,7 @@ network::connection game::ban_user(const simple_wml::node& ban,
|
|||
//tell the user to leave the game.
|
||||
static simple_wml::document leave_game("[leave_game]\n[/leave_game]\n", simple_wml::INIT_COMPRESSED);
|
||||
static const simple_wml::string_span leave_game_data = leave_game.output_compressed();
|
||||
network::send_raw_data(leave_game_data.begin(), leave_game_data.size(), user->first,"game");
|
||||
network::send_raw_data(leave_game_data.begin(), leave_game_data.size(), user->first,leave_game.root().first_child().to_string());
|
||||
remove_player(user->first);
|
||||
return user->first;
|
||||
}
|
||||
|
@ -960,18 +960,18 @@ void game::add_player(const network::connection player, bool observer) {
|
|||
observer_join.root().add_child("observer").set_attr_dup("name", user->second.name().c_str());
|
||||
|
||||
// Send observer join to everyone except the new observer.
|
||||
send_data(observer_join, player, "game control");
|
||||
send_data(observer_join, player);
|
||||
}
|
||||
DBG_GAME << debug_player_info();
|
||||
// Send the user the game data.
|
||||
//std::cerr << "SENDING LEVEL {{{" << level_.output() << "}}}\n";
|
||||
simple_wml::string_span level_data = level_.output_compressed();
|
||||
network::send_raw_data(level_data.begin(), level_data.size(), player,"game");
|
||||
network::send_raw_data(level_data.begin(), level_data.size(), player,"game_level");
|
||||
if(started_) {
|
||||
//tell this player that the game has started
|
||||
static simple_wml::document start_game_doc("[start_game]\n[/start_game]\n", simple_wml::INIT_COMPRESSED);
|
||||
static const simple_wml::string_span start_game = start_game_doc.output_compressed();
|
||||
network::send_raw_data(start_game.begin(), start_game.size(), player,"game");
|
||||
network::send_raw_data(start_game.begin(), start_game.size(), player,start_game_doc.root().first_child().to_string());
|
||||
// Send observer join of all the observers in the game to the new player
|
||||
// only once the game started. The client forgets about it anyway
|
||||
// otherwise.
|
||||
|
@ -1075,7 +1075,7 @@ bool game::remove_player(const network::connection player, const bool disconnect
|
|||
drop.root().set_attr("side_drop", side_drop_buf);
|
||||
drop.root().set_attr("controller", side_controllers_[side_num].c_str());
|
||||
|
||||
send_to_one(drop, owner_, "game control");
|
||||
send_to_one(drop, owner_);
|
||||
}
|
||||
if (host) transfer_ai_sides();
|
||||
DBG_GAME << debug_player_info();
|
||||
|
@ -1100,7 +1100,7 @@ void game::send_user_list(const network::connection exclude) const {
|
|||
cfg.root().add_child("user").set_attr("name", pl->second.name().c_str());
|
||||
}
|
||||
}
|
||||
send_data(cfg, exclude, "user_list");
|
||||
send_data(cfg, exclude);
|
||||
}
|
||||
|
||||
//! A member asks for the next scenario to advance to.
|
||||
|
@ -1109,15 +1109,17 @@ void game::load_next_scenario(const player_map::const_iterator user) const {
|
|||
simple_wml::document cfg_scenario;
|
||||
level_.root().copy_into(cfg_scenario.root().add_child("next_scenario"));
|
||||
simple_wml::string_span data = cfg_scenario.output_compressed();
|
||||
network::send_raw_data(data.begin(), data.size(), user->first, "game");
|
||||
network::send_raw_data(data.begin(), data.size(), user->first, cfg_scenario.root().first_child().to_string());
|
||||
// Send the player the history of the game to-date.
|
||||
send_history(user->first);
|
||||
// Send observer join of all the observers in the game to the user.
|
||||
send_observerjoins(user->first);
|
||||
}
|
||||
|
||||
void game::send_data(simple_wml::document& data, const network::connection exclude, const std::string packet_type) const
|
||||
void game::send_data(simple_wml::document& data, const network::connection exclude, std::string packet_type) const
|
||||
{
|
||||
if (packet_type.empty())
|
||||
packet_type = data.root().first_child().to_string();
|
||||
simple_wml::string_span s = data.output_compressed();
|
||||
const user_vector& users = all_game_users();
|
||||
for(user_vector::const_iterator i = users.begin(); i != users.end(); ++i) {
|
||||
|
@ -1127,8 +1129,10 @@ void game::send_data(simple_wml::document& data, const network::connection exclu
|
|||
}
|
||||
}
|
||||
|
||||
void game::send_to_one(simple_wml::document& data, const network::connection sock, const std::string packet_type) const
|
||||
void game::send_to_one(simple_wml::document& data, const network::connection sock, std::string packet_type) const
|
||||
{
|
||||
if (packet_type.empty())
|
||||
packet_type = data.root().first_child().to_string();
|
||||
simple_wml::string_span s = data.output_compressed();
|
||||
network::send_raw_data(s.begin(), s.size(), sock, packet_type);
|
||||
}
|
||||
|
@ -1136,8 +1140,10 @@ void game::send_to_one(simple_wml::document& data, const network::connection soc
|
|||
void game::send_data_team(simple_wml::document& data,
|
||||
const simple_wml::string_span& team,
|
||||
const network::connection exclude,
|
||||
const std::string packet_type) const
|
||||
std::string packet_type) const
|
||||
{
|
||||
if (packet_type.empty())
|
||||
packet_type = data.root().first_child().to_string();
|
||||
simple_wml::string_span s = data.output_compressed();
|
||||
for(user_vector::const_iterator i = players_.begin(); i != players_.end(); ++i) {
|
||||
if(*i != exclude && is_on_team(team,*i)) {
|
||||
|
@ -1146,7 +1152,9 @@ void game::send_data_team(simple_wml::document& data,
|
|||
}
|
||||
}
|
||||
|
||||
void game::send_data_observers(simple_wml::document& data, const network::connection exclude, const std::string packet_type) const {
|
||||
void game::send_data_observers(simple_wml::document& data, const network::connection exclude, std::string packet_type) const {
|
||||
if (packet_type.empty())
|
||||
packet_type = data.root().first_child().to_string();
|
||||
simple_wml::string_span s = data.output_compressed();
|
||||
for(user_vector::const_iterator i = observers_.begin(); i != observers_.end(); ++i) {
|
||||
if (*i != exclude) {
|
||||
|
@ -1190,11 +1198,11 @@ void game::send_observerjoins(const network::connection sock) const {
|
|||
cfg.root().add_child("observer").set_attr_dup("name", obs->second.name().c_str());
|
||||
if (sock == 0) {
|
||||
// Send to everyone except the observer in question.
|
||||
send_data(cfg, *ob, "game control");
|
||||
send_data(cfg, *ob);
|
||||
} else {
|
||||
// Send to the (new) user.
|
||||
const simple_wml::string_span& data = cfg.output_compressed();
|
||||
network::send_raw_data(data.begin(), data.size(), sock,"game control");
|
||||
network::send_raw_data(data.begin(), data.size(), sock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1208,7 +1216,7 @@ void game::send_observerquit(const player_map::const_iterator observer) const {
|
|||
//don't need to dup the attribute because this document is
|
||||
//short-lived.
|
||||
observer_quit.root().add_child("observer_quit").set_attr("name", observer->second.name().c_str());
|
||||
send_data(observer_quit, observer->first,"game control");
|
||||
send_data(observer_quit, observer->first);
|
||||
}
|
||||
|
||||
void game::send_history(const network::connection sock) const
|
||||
|
|
|
@ -95,8 +95,8 @@ public:
|
|||
//! Send data to all players in this game except 'exclude'.
|
||||
void send_and_record_server_message(const char* message,
|
||||
const network::connection exclude=0);
|
||||
void send_data(simple_wml::document& data, const network::connection exclude=0, const std::string packet_type = "unknown") const;
|
||||
void send_to_one(simple_wml::document& data, const network::connection sock, const std::string packet_type = "unknown") const;
|
||||
void send_data(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const;
|
||||
void send_to_one(simple_wml::document& data, const network::connection sock, std::string packet_type = "") const;
|
||||
|
||||
void record_data(simple_wml::document* data);
|
||||
|
||||
|
@ -142,8 +142,8 @@ private:
|
|||
const bool player_left=true);
|
||||
void transfer_ai_sides();
|
||||
void send_data_team(simple_wml::document& data, const simple_wml::string_span& team,
|
||||
const network::connection exclude=0, const std::string packet_type = "unknown") const;
|
||||
void send_data_observers(simple_wml::document& data, const network::connection exclude=0, const std::string packet_type = "unknown") const;
|
||||
const network::connection exclude=0, std::string packet_type = "") const;
|
||||
void send_data_observers(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const;
|
||||
//! Send [observer] tags of all the observers in the game to the user or
|
||||
//! everyone if none given.
|
||||
void send_observerjoins(const network::connection sock=0) const;
|
||||
|
|
|
@ -126,8 +126,10 @@ namespace {
|
|||
// we take profiling info on every n requests
|
||||
int request_sample_frequency = 1;
|
||||
|
||||
void send_doc(simple_wml::document& doc, network::connection connection, std::string type = "unknown")
|
||||
void send_doc(simple_wml::document& doc, network::connection connection, std::string type = "")
|
||||
{
|
||||
if (type.empty())
|
||||
type = doc.root().first_child().to_string();
|
||||
simple_wml::string_span s = doc.output_compressed();
|
||||
network::send_raw_data(s.begin(), s.size(), connection, type);
|
||||
}
|
||||
|
@ -686,7 +688,7 @@ void server::run() {
|
|||
} else {
|
||||
DBG_SERVER << ip << "\tnew connection accepted. (socket: "
|
||||
<< sock << ")\n";
|
||||
send_doc(version_query_response_, sock,"command");
|
||||
send_doc(version_query_response_, sock);
|
||||
not_logged_in_.add_player(sock, true);
|
||||
}
|
||||
}
|
||||
|
@ -731,7 +733,7 @@ void server::run() {
|
|||
|
||||
process_data(sock, data);
|
||||
|
||||
bandwidth_type->set_type("command");
|
||||
bandwidth_type->set_type(data.root().first_child().to_string());
|
||||
if(sample) {
|
||||
const clock_t after_processing = get_cpu_time(sample);
|
||||
metrics_.record_sample(data.root().first_child(),
|
||||
|
@ -889,7 +891,7 @@ void server::process_login(const network::connection sock,
|
|||
LOG_SERVER << network::ip_address(sock)
|
||||
<< "\tplayer joined using accepted version " << version_str
|
||||
<< ":\ttelling them to log in.\n";
|
||||
send_doc(login_response_, sock, "command");
|
||||
send_doc(login_response_, sock);
|
||||
return;
|
||||
}
|
||||
std::map<std::string,config>::const_iterator config_it;
|
||||
|
@ -1053,7 +1055,7 @@ void server::process_login(const network::connection sock,
|
|||
DBG_SERVER << "selective ping is DISABLED for " << sock << "\n" ;
|
||||
}
|
||||
|
||||
send_doc(join_lobby_response_, sock, "join_lobby");
|
||||
send_doc(join_lobby_response_, sock);
|
||||
|
||||
simple_wml::node& player_cfg = games_and_users_list_.root().add_child("user");
|
||||
const player new_player(username, player_cfg, registered, default_max_messages_,
|
||||
|
@ -1071,7 +1073,7 @@ void server::process_login(const network::connection sock,
|
|||
lobby_.add_player(sock, true);
|
||||
|
||||
// Send the new player the entire list of games and players
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
|
||||
if (motd_ != "") {
|
||||
lobby_.send_server_message(motd_.c_str(), sock);
|
||||
|
@ -1504,7 +1506,7 @@ void server::process_whisper(const network::connection sock,
|
|||
"message=\"Invalid number of arguments\"\n"
|
||||
"sender=\"server\"\n"
|
||||
"[/message]\n", simple_wml::INIT_COMPRESSED);
|
||||
send_doc(data, sock, "error");
|
||||
send_doc(data, sock);
|
||||
return;
|
||||
}
|
||||
const wesnothd::player_map::const_iterator pl = players_.find(sock);
|
||||
|
@ -1534,7 +1536,7 @@ void server::process_whisper(const network::connection sock,
|
|||
|
||||
simple_wml::document cwhisper;
|
||||
whisper.copy_into(cwhisper.root().add_child("whisper"));
|
||||
send_doc(cwhisper, i->first, "command");
|
||||
send_doc(cwhisper, i->first);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1548,7 +1550,7 @@ void server::process_whisper(const network::connection sock,
|
|||
}
|
||||
|
||||
msg.set_attr("sender", "server");
|
||||
send_doc(data, sock, "error");
|
||||
send_doc(data, sock);
|
||||
}
|
||||
|
||||
void server::process_data_lobby(const network::connection sock,
|
||||
|
@ -1565,9 +1567,9 @@ void server::process_data_lobby(const network::connection sock,
|
|||
if (data.root().child("create_game")) {
|
||||
if (graceful_restart) {
|
||||
static simple_wml::document leave_game_doc("[leave_game]\n[/leave_game]\n", simple_wml::INIT_COMPRESSED);
|
||||
send_doc(leave_game_doc, sock,"command");
|
||||
send_doc(leave_game_doc, sock);
|
||||
lobby_.send_server_message("This server is shutting down. You aren't allowed to make new games. Please reconnect to the new server.", sock);
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
return;
|
||||
}
|
||||
const std::string game_name = (*data.root().child("create_game"))["name"].to_string();
|
||||
|
@ -1605,41 +1607,41 @@ void server::process_data_lobby(const network::connection sock,
|
|||
if (g == games_.end()) {
|
||||
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
|
||||
<< "\tattempted to join unknown game:\t" << game_id << ".\n";
|
||||
send_doc(leave_game_doc, sock, "command");
|
||||
send_doc(leave_game_doc, sock);
|
||||
lobby_.send_server_message("Attempt to join unknown game.", sock);
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
return;
|
||||
} else if ((*g)->player_is_banned(sock)) {
|
||||
DBG_SERVER << network::ip_address(sock) << "\tReject banned player: "
|
||||
<< pl->second.name() << "\tfrom game:\t\"" << (*g)->name()
|
||||
<< "\" (" << game_id << ").\n";
|
||||
send_doc(leave_game_doc, sock, "command");
|
||||
send_doc(leave_game_doc, sock);
|
||||
lobby_.send_server_message("You are banned from this game.", sock);
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
return;
|
||||
} else if(!observer && !(*g)->password_matches(password)) {
|
||||
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
|
||||
<< "\tattempted to join game:\t\"" << (*g)->name() << "\" ("
|
||||
<< game_id << ") with bad password\n";
|
||||
send_doc(leave_game_doc, sock, "command");
|
||||
send_doc(leave_game_doc, sock);
|
||||
lobby_.send_server_message("Incorrect password.", sock);
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
return;
|
||||
} else if (observer && !(*g)->allow_observers()) {
|
||||
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
|
||||
<< "\tattempted to observe game:\t\"" << (*g)->name() << "\" ("
|
||||
<< game_id << ") which doesn't allow observers.\n";
|
||||
send_doc(leave_game_doc, sock, "command");
|
||||
send_doc(leave_game_doc, sock);
|
||||
lobby_.send_server_message("Attempt to observe a game that doesn't allow observers.", sock);
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
return;
|
||||
} else if (!(*g)->level_init()) {
|
||||
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
|
||||
<< "\tattempted to join uninitialized game:\t\"" << (*g)->name()
|
||||
<< "\" (" << game_id << ").\n";
|
||||
send_doc(leave_game_doc, sock, "command");
|
||||
send_doc(leave_game_doc, sock);
|
||||
lobby_.send_server_message("Attempt to observe a game that doesn't allow observers.", sock);
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
return;
|
||||
}
|
||||
LOG_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
|
||||
|
@ -1669,7 +1671,7 @@ void server::process_data_lobby(const network::connection sock,
|
|||
// Player requests update of lobby content,
|
||||
// for example when cancelling the create game dialog
|
||||
if (data.child("refresh_lobby")) {
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1930,7 +1932,7 @@ void server::process_data_game(const network::connection sock,
|
|||
}
|
||||
|
||||
// Send the player who has quit the gamelist.
|
||||
send_doc(games_and_users_list_, sock, "lobby_list");
|
||||
send_doc(games_and_users_list_, sock);
|
||||
}
|
||||
return;
|
||||
// If this is data describing side changes by the host.
|
||||
|
@ -1983,7 +1985,7 @@ void server::process_data_game(const network::connection sock,
|
|||
update_game_in_lobby(g, user);
|
||||
}
|
||||
// Send the removed user the lobby game list.
|
||||
send_doc(games_and_users_list_, user, "lobby_list");
|
||||
send_doc(games_and_users_list_, user);
|
||||
// FIXME: should also send a user diff to the lobby
|
||||
// to mark this player as available for others
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue