Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "player_network.hpp"
00018 #include "../log.hpp"
00019 #include "serialization/string_utils.hpp"
00020
00021 static lg::log_domain log_config("config");
00022 #define WRN_CONFIG LOG_STREAM(warn, log_config)
00023
00024 namespace wesnothd {
00025
00026 namespace chat_message {
00027
00028 const size_t max_message_length = 256;
00029
00030 void truncate_message(const simple_wml::string_span& str, simple_wml::node& message)
00031 {
00032
00033
00034 if(str.size() > static_cast<int>(chat_message::max_message_length)) {
00035 std::string tmp(str.begin(), str.end());
00036
00037
00038 utils::truncate_as_wstring(tmp, max_message_length);
00039 message.set_attr_dup("message", tmp.c_str());
00040 }
00041 }
00042
00043 }
00044
00045 bool send_to_one(simple_wml::document& data, const network::connection sock, std::string packet_type)
00046 {
00047 if (packet_type.empty())
00048 packet_type = data.root().first_child().to_string();
00049 try {
00050 simple_wml::string_span s = data.output_compressed();
00051 network::send_raw_data(s.begin(), s.size(), sock, packet_type);
00052 } catch (simple_wml::error& e) {
00053 WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
00054 return false;
00055 }
00056 return true;
00057 }
00058
00059 void send_to_many(simple_wml::document& data, const connection_vector& vec,
00060 const network::connection exclude, std::string packet_type)
00061 {
00062 if (packet_type.empty())
00063 packet_type = data.root().first_child().to_string();
00064 try {
00065 simple_wml::string_span s = data.output_compressed();
00066 for(connection_vector::const_iterator i = vec.begin(); i != vec.end(); ++i) {
00067 if (*i != exclude) {
00068 network::send_raw_data(s.begin(), s.size(), *i, packet_type);
00069 }
00070 }
00071 } catch (simple_wml::error& e) {
00072 WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
00073 }
00074 }
00075
00076 void send_to_many(simple_wml::document& data, const connection_vector& vec,
00077 boost::function<bool (network::connection)> pred,
00078 const network::connection exclude, std::string packet_type)
00079 {
00080 if (packet_type.empty())
00081 packet_type = data.root().first_child().to_string();
00082 try {
00083 simple_wml::string_span s = data.output_compressed();
00084 for(connection_vector::const_iterator i = vec.begin(); i != vec.end(); ++i) {
00085 if ((*i != exclude) && pred(*i)) {
00086 network::send_raw_data(s.begin(), s.size(), *i, packet_type);
00087 }
00088 }
00089 } catch (simple_wml::error& e) {
00090 WRN_CONFIG << __func__ << ": simple_wml error: " << e.message << std::endl;
00091 }
00092
00093 }
00094
00095 }