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