Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/wesnoth/wesnoth
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 25, 2014
2 parents f88de7f + 6629fdf commit c319e27
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
22 changes: 15 additions & 7 deletions src/campaign_server/campaign_server.cpp
Expand Up @@ -238,7 +238,7 @@ void server::send_error(const std::string& msg, network::connection sock)
{
config cfg;
cfg.add_child("error")["message"] = msg;
LOG_CS << "ERROR: " << msg << '\n';
LOG_CS << "ERROR [" << network::ip_address(sock) << "]: " << msg << '\n';
network::send_data(cfg, sock);
}

Expand Down Expand Up @@ -278,13 +278,21 @@ void server::run()

while((sock = network::receive_data(data, 0)) != network::null_connection)
{
typedef std::pair<std::string, request_handler> rh_table_entry;
BOOST_FOREACH(const rh_table_entry& rh, handlers_)
{
const config& req_body = data.child(rh.first);
config::all_children_iterator i = data.ordered_begin();

if(req_body) {
rh.second(request(rh.first, req_body, sock));
if(i != data.ordered_end()) {
// We only handle the first child.
const config::any_child& c = *i;

request_handlers_table::const_iterator j
= handlers_.find(c.key);

if(j != handlers_.end()) {
// Call the handler.
j->second(request(c.key, c.cfg, sock));
} else {
send_error("Unrecognized [" + c.key + "] request.",
sock);
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/campaign_server/campaign_server.hpp
Expand Up @@ -84,6 +84,7 @@ class server : private boost::noncopyable
};

typedef boost::function<void (const request& req)> request_handler;
typedef std::map<std::string, request_handler> request_handlers_table;

config cfg_;
const std::string cfg_file_;
Expand All @@ -94,7 +95,7 @@ class server : private boost::noncopyable
boost::scoped_ptr<input_stream> input_; /**< Server control socket. */

std::map<std::string, std::string> hooks_;
std::map<std::string, request_handler> handlers_;
request_handlers_table handlers_;

std::string feedback_url_format_;

Expand Down Expand Up @@ -176,10 +177,22 @@ class server : private boost::noncopyable
// Generic responses.
//

/** Send a client an informational message. */
/**
* Send a client an informational message.
*
* The WML sent consists of a document containing a single @p [message]
* child with a @a message attribute holding the value of @a msg.
*/
void send_message(const std::string& msg, network::connection sock);

/** Send a client an error message. */
/**
* Send a client an error message.
*
* The WML sent consists of a document containing a single @p [error] child
* with a @a message attribute holding the value of @a msg. In addition to
* sending the error to the client, a line with the client IP and message
* is recorded to the server log.
*/
void send_error(const std::string& msg, network::connection sock);
};

Expand Down
8 changes: 6 additions & 2 deletions src/server/server.cpp
Expand Up @@ -333,6 +333,7 @@ server::server(int port, const std::string& config_file, size_t min_threads,
not_logged_in_(),
rooms_(players_),
input_(),
input_path_(),
config_file_(config_file),
cfg_(read_config()),
accepted_versions_(),
Expand Down Expand Up @@ -499,8 +500,11 @@ void server::load_config() {
# endif
#endif
const std::string fifo_path = (cfg_["fifo_path"].empty() ? std::string(FIFODIR) + "/socket" : std::string(cfg_["fifo_path"]));
input_.reset();
input_.reset(new input_stream(fifo_path));
// Reset (replace) the input stream only if the FIFO path changed.
if(fifo_path != input_path_) {
input_.reset(new input_stream(fifo_path));
}
input_path_ = fifo_path;

save_replays_ = cfg_["save_replays"].to_bool();
replay_save_path_ = cfg_["replay_save_path"].str();
Expand Down
1 change: 1 addition & 0 deletions src/server/server.hpp
Expand Up @@ -106,6 +106,7 @@ class server

/** server socket/fifo. */
boost::scoped_ptr<input_stream> input_;
std::string input_path_;

const std::string config_file_;
config cfg_;
Expand Down

0 comments on commit c319e27

Please sign in to comment.