server: Make it possible to send a [data] block in server responses
The [data] block in [error]/[warning]/[message] can contain arbitrary data, passed in the form of key/value pairs.
This commit is contained in:
parent
37cca84684
commit
6c14dcf72f
2 changed files with 29 additions and 6 deletions
|
@ -208,32 +208,51 @@ bool check_error(const boost::system::error_code& error, socket_ptr socket)
|
|||
return false;
|
||||
}
|
||||
|
||||
void async_send_error(socket_ptr socket, const std::string& msg, const char* error_code)
|
||||
namespace {
|
||||
|
||||
void info_table_into_simple_wml(simple_wml::document& doc, const std::string& parent_name, const info_table& info)
|
||||
{
|
||||
if(info.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& node = doc.child(parent_name.c_str())->add_child("data");
|
||||
for(const auto& kv : info) {
|
||||
node.set_attr_dup(kv.first.c_str(), kv.second.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void async_send_error(socket_ptr socket, const std::string& msg, const char* error_code, const info_table& info)
|
||||
{
|
||||
simple_wml::document doc;
|
||||
doc.root().add_child("error").set_attr_dup("message", msg.c_str());
|
||||
if(*error_code != '\0') {
|
||||
doc.child("error")->set_attr("error_code", error_code);
|
||||
}
|
||||
info_table_into_simple_wml(doc, "error", info);
|
||||
|
||||
async_send_doc(socket, doc);
|
||||
}
|
||||
|
||||
void async_send_warning(socket_ptr socket, const std::string& msg, const char* warning_code)
|
||||
void async_send_warning(socket_ptr socket, const std::string& msg, const char* warning_code, const info_table& info)
|
||||
{
|
||||
simple_wml::document doc;
|
||||
doc.root().add_child("warning").set_attr_dup("message", msg.c_str());
|
||||
if(*warning_code != '\0') {
|
||||
doc.child("warning")->set_attr("warning_code", warning_code);
|
||||
}
|
||||
info_table_into_simple_wml(doc, "warning", info);
|
||||
|
||||
async_send_doc(socket, doc);
|
||||
}
|
||||
|
||||
void async_send_message(socket_ptr socket, const std::string& msg)
|
||||
void async_send_message(socket_ptr socket, const std::string& msg, const info_table& info)
|
||||
{
|
||||
simple_wml::document doc;
|
||||
doc.root().add_child("message").set_attr_dup("message", msg.c_str());
|
||||
info_table_into_simple_wml(doc, "message", info);
|
||||
|
||||
async_send_doc(socket, doc);
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
#include "exceptions.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
#include <boost/shared_array.hpp>
|
||||
|
@ -80,6 +82,8 @@ protected:
|
|||
std::string client_address(socket_ptr socket);
|
||||
bool check_error(const boost::system::error_code& error, socket_ptr socket);
|
||||
|
||||
void async_send_error(socket_ptr socket, const std::string& msg, const char* error_code = "");
|
||||
void async_send_warning(socket_ptr socket, const std::string& msg, const char* warning_code = "");
|
||||
void async_send_message(socket_ptr socket, const std::string& msg);
|
||||
typedef std::map<std::string, std::string> info_table;
|
||||
|
||||
void async_send_error(socket_ptr socket, const std::string& msg, const char* error_code = "", const info_table& info = {});
|
||||
void async_send_warning(socket_ptr socket, const std::string& msg, const char* warning_code = "", const info_table& info = {});
|
||||
void async_send_message(socket_ptr socket, const std::string& msg, const info_table& info = {});
|
||||
|
|
Loading…
Add table
Reference in a new issue