server/server.hpp

Go to the documentation of this file.
00001 #ifndef SERVER_HPP_INCLUDED
00002 #define SERVER_HPP_INCLUDED
00003 
00004 #include "config.hpp"
00005 #include "user_handler.hpp"
00006 #include "input_stream.hpp"
00007 #include "metrics.hpp"
00008 #include "../network.hpp"
00009 #include "ban.hpp"
00010 #include "player.hpp"
00011 #include "room_manager.hpp"
00012 #include "simple_wml.hpp"
00013 
00014 #include <boost/function.hpp>
00015 #include <boost/scoped_ptr.hpp>
00016 
00017 class server
00018 {
00019 public:
00020     server(int port, const std::string& config_file, size_t min_threads,size_t max_threads);
00021     void run();
00022 private:
00023     void send_error(network::connection sock, const char* msg, const char* error_code ="") const;
00024     void send_error(network::connection sock, const std::string &msg, const char* error_code = "") const
00025     {
00026         send_error(sock, msg.c_str(), error_code);
00027     }
00028 
00029     void send_warning(network::connection sock, const char* msg, const char* warning_code ="") const;
00030     void send_warning(network::connection sock, const std::string &msg, const char* warning_code = "") const
00031     {
00032         send_warning(sock, msg.c_str(), warning_code);
00033     }
00034 
00035     // The same as send_error(), we just add an extra child to the response
00036     // telling the client the chosen username requires a password.
00037     void send_password_request(network::connection sock, const std::string& msg,
00038             const std::string& user, const char* error_code ="",
00039             bool force_confirmation = false);
00040 
00041     const network::manager net_manager_;
00042     network::server_manager server_;
00043     wesnothd::ban_manager ban_manager_;
00044 
00045     struct connection_log {
00046         connection_log(std::string _nick, std::string _ip, time_t _log_off) :
00047         nick(_nick), ip(_ip), log_off(_log_off) {}
00048         std::string nick, ip;
00049         time_t log_off;
00050 
00051         bool operator==(const connection_log& c) const
00052         {
00053             // log off time does not matter to find ip-nick pairs
00054             return c.nick == nick && c.ip == ip;
00055         }
00056     };
00057 
00058     std::deque<connection_log> ip_log_;
00059 
00060     struct login_log {
00061         login_log(std::string _ip, int _attempts, time_t _first_attempt) :
00062         ip(_ip), attempts(_attempts), first_attempt(_first_attempt) {}
00063         std::string ip;
00064         int attempts;
00065         time_t first_attempt;
00066 
00067         bool operator==(const login_log& l) const
00068         {
00069             // only the IP matters
00070             return l.ip == ip;
00071         }
00072     };
00073 
00074     std::deque<login_log> failed_logins_;
00075 
00076     boost::scoped_ptr<user_handler> user_handler_;
00077     std::map<network::connection,std::string> seeds_;
00078 
00079     /** std::map<network::connection,player>. */
00080     wesnothd::player_map players_;
00081     std::set<network::connection> ghost_players_;
00082 
00083     std::vector<wesnothd::game*> games_;
00084     std::set<network::connection> not_logged_in_;
00085 
00086     wesnothd::room_manager rooms_;
00087 
00088     /** server socket/fifo. */
00089     boost::scoped_ptr<input_stream> input_;
00090 
00091     const std::string config_file_;
00092     config cfg_;
00093 
00094     /** Read the server config from file 'config_file_'. */
00095     config read_config() const;
00096 
00097     // settings from the server config
00098     std::vector<std::string> accepted_versions_;
00099     std::map<std::string,config> redirected_versions_;
00100     std::map<std::string,config> proxy_versions_;
00101     std::vector<std::string> disallowed_names_;
00102     std::string admin_passwd_;
00103     std::set<network::connection> admins_;
00104     std::string motd_;
00105     size_t default_max_messages_;
00106     size_t default_time_period_;
00107     size_t concurrent_connections_;
00108     bool graceful_restart;
00109     time_t lan_server_;
00110     time_t last_user_seen_time_;
00111     std::string restart_command;
00112     size_t max_ip_log_size_;
00113     std::string uh_name_;
00114     bool deny_unregistered_login_;
00115     bool save_replays_;
00116     std::string replay_save_path_;
00117     bool allow_remote_shutdown_;
00118     std::vector<std::string> tor_ip_list_;
00119     int failed_login_limit_;
00120     time_t failed_login_ban_;
00121     std::deque<login_log>::size_type failed_login_buffer_size_;
00122 
00123     /** Parse the server config into local variables. */
00124     void load_config();
00125 
00126     bool ip_exceeds_connection_limit(const std::string& ip) const;
00127     std::string is_ip_banned(const std::string& ip) const;
00128 
00129     simple_wml::document version_query_response_;
00130     simple_wml::document login_response_;
00131     simple_wml::document join_lobby_response_;
00132     simple_wml::document games_and_users_list_;
00133 
00134     metrics metrics_;
00135 
00136     time_t last_ping_;
00137     time_t last_stats_;
00138     void dump_stats(const time_t& now);
00139 
00140     time_t last_uh_clean_;
00141     void clean_user_handler(const time_t& now);
00142 
00143     void process_data(const network::connection sock,
00144                       simple_wml::document& data);
00145     void process_login(const network::connection sock,
00146                        simple_wml::document& data);
00147 
00148     /** Handle queries from clients. */
00149     void process_query(const network::connection sock,
00150                        simple_wml::node& query);
00151 
00152     /** Process commands from admins and users. */
00153     std::string process_command(std::string cmd, std::string issuer_name);
00154 
00155     /** Handle private messages between players. */
00156     void process_whisper(const network::connection sock,
00157                          simple_wml::node& whisper) const;
00158 
00159     /** Handle nickname registration related requests from clients. */
00160     void process_nickserv(const network::connection sock, simple_wml::node& data);
00161     void process_data_lobby(const network::connection sock,
00162                             simple_wml::document& data);
00163     void process_data_game(const network::connection sock,
00164                            simple_wml::document& data);
00165     void delete_game(std::vector<wesnothd::game*>::iterator game_it);
00166 
00167     void update_game_in_lobby(const wesnothd::game* g, network::connection exclude=0);
00168 
00169     void start_new_server();
00170 
00171     void setup_handlers();
00172 
00173     typedef boost::function5<void, server*, const std::string&, const std::string&, std::string&, std::ostringstream *> cmd_handler;
00174     std::map<std::string, cmd_handler> cmd_handlers_;
00175 
00176     void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00177     void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00178     void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00179     void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00180     void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00181     void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00182     void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00183     void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00184     void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00185     void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00186     void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00187     void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00188     void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00189     void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00190     void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00191     void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00192     void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00193     void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00194     void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00195     void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00196     void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00197     void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00198     void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00199     void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00200     void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00201     void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
00202 };
00203 
00204 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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