server/player_network.cpp

Go to the documentation of this file.
00001 /* $Id: player_network.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Copyright (C) 2009 - 2012 by Tomasz Sniatowski <kailoran@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
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     // testing for msg.size() is not sufficient but we're not getting false negatives
00033     // and it's cheaper than always converting to wstring.
00034     if(str.size() > static_cast<int>(chat_message::max_message_length)) {
00035         std::string tmp(str.begin(), str.end());
00036         // The string can contain utf-8 characters so truncate as wide_string otherwise
00037         // a corrupted utf-8 string can be returned.
00038         utils::truncate_as_wstring(tmp, max_message_length);
00039         message.set_attr_dup("message", tmp.c_str());
00040     }
00041 }
00042 
00043 } // end chat_message namespace
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 } //end namespace wesnothd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:09 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs