The Battle for Wesnoth  1.15.1+dev
server_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by Sergey Popov <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License 2
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Base class for servers using Wesnoth's WML over TCP protocol.
18  */
19 
20 #pragma once
21 
22 #include "exceptions.hpp"
23 
24 #include <map>
25 
26 #include <boost/asio.hpp>
27 #include <boost/asio/signal_set.hpp>
28 #include <boost/shared_array.hpp>
29 
30 typedef std::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr;
31 
33 {
34  server_shutdown(const std::string& msg) : game::error(msg) {}
35 };
36 
38 {
39 public:
40  server_base(unsigned short port, bool keep_alive);
41  virtual ~server_base() {}
42  void run();
43 
44 protected:
45  unsigned short port_;
47  boost::asio::io_service io_service_;
48  boost::asio::ip::tcp::acceptor acceptor_v6_;
49  boost::asio::ip::tcp::acceptor acceptor_v4_;
50  void setup_acceptor(boost::asio::ip::tcp::acceptor& acceptor, boost::asio::ip::tcp::endpoint endpoint);
51  void start_server();
52  void serve(boost::asio::ip::tcp::acceptor& acceptor);
53  void accept_connection(boost::asio::ip::tcp::acceptor& acceptor, const boost::system::error_code& error, socket_ptr socket);
54 
55  union {
56  uint32_t connection_num;
57  char buf[4];
58  } handshake_response_;
59  void serverside_handshake(socket_ptr socket);
60  void handle_handshake(const boost::system::error_code& error, socket_ptr socket, boost::shared_array<char> buf);
61 
62  virtual void handle_new_client(socket_ptr socket) = 0;
63 
64  virtual bool accepting_connections() const { return true; }
65  virtual std::string is_ip_banned(const std::string&) { return std::string(); }
66  virtual bool ip_exceeds_connection_limit(const std::string&) const { return false; }
67 
68 #ifndef _WIN32
69  boost::asio::posix::stream_descriptor input_;
70  std::string fifo_path_;
71  void read_from_fifo();
72  virtual void handle_read_from_fifo(const boost::system::error_code& error, std::size_t bytes_transferred) = 0;
73  boost::asio::streambuf admin_cmd_;
74 
75  boost::asio::signal_set sighup_;
76  virtual void handle_sighup(const boost::system::error_code& error, int signal_number) = 0;
77 #endif
78  boost::asio::signal_set sigs_;
79  void handle_termination(const boost::system::error_code& error, int signal_number);
80 };
81 
82 std::string client_address(socket_ptr socket);
83 bool check_error(const boost::system::error_code& error, socket_ptr socket);
84 
85 typedef std::map<std::string, std::string> info_table;
86 
87 void async_send_error(socket_ptr socket, const std::string& msg, const char* error_code = "", const info_table& info = {});
88 void async_send_warning(socket_ptr socket, const std::string& msg, const char* warning_code = "", const info_table& info = {});
89 void async_send_message(socket_ptr socket, const std::string& msg, const info_table& info = {});
boost::asio::ip::tcp::acceptor acceptor_v4_
Definition: server_base.hpp:49
server_shutdown(const std::string &msg)
Definition: server_base.hpp:34
void async_send_warning(socket_ptr socket, const std::string &msg, const char *warning_code="", const info_table &info={})
boost::asio::signal_set sighup_
Definition: server_base.hpp:75
logger & info()
Definition: log.cpp:90
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
unsigned short port_
Definition: server_base.hpp:45
uint32_t connection_num
Definition: server_base.hpp:56
void async_send_message(socket_ptr socket, const std::string &msg, const info_table &info={})
std::map< std::string, std::string > info_table
Definition: server_base.hpp:85
bool keep_alive_
Definition: server_base.hpp:46
boost::asio::streambuf admin_cmd_
Definition: server_base.hpp:73
boost::asio::io_service io_service_
Definition: server_base.hpp:47
boost::asio::ip::tcp::acceptor acceptor_v6_
Definition: server_base.hpp:48
virtual bool accepting_connections() const
Definition: server_base.hpp:64
virtual bool ip_exceeds_connection_limit(const std::string &) const
Definition: server_base.hpp:66
virtual ~server_base()
Definition: server_base.hpp:41
bool check_error(const boost::system::error_code &error, socket_ptr socket)
virtual std::string is_ip_banned(const std::string &)
Definition: server_base.hpp:65
boost::asio::posix::stream_descriptor input_
Definition: server_base.hpp:69
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string fifo_path_
Definition: server_base.hpp:70
std::string client_address(socket_ptr socket)
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:30
void async_send_error(socket_ptr socket, const std::string &msg, const char *error_code="", const info_table &info={})
boost::asio::signal_set sigs_
Definition: server_base.hpp:78