The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
player_connection.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 - 2025
3 by Sergey Popov <loonycyborg@gmail.com>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#pragma once
17
20
21#include <boost/multi_index/hashed_index.hpp>
22#include <boost/multi_index/mem_fun.hpp>
23#include <boost/multi_index/ordered_index.hpp>
24#include <boost/multi_index_container.hpp>
25
26namespace wesnothd
27{
28class game;
29
31{
32public:
33 template<typename SocketPtr, typename... Args>
34 player_record(const SocketPtr socket, Args&&... args)
35 : login_time_(std::chrono::steady_clock::now())
37 , player_(std::forward<Args>(args)...)
38 , game_()
40 {
41 }
42
43 const any_socket_ptr socket() const
44 {
45 return socket_;
46 }
47
48 std::string client_ip() const
49 {
50 return ip_address;
51 }
52
53 player& info() const
54 {
55 return player_;
56 }
57
58 const std::string& name() const
59 {
60 return player_.name();
61 }
62
63 const std::shared_ptr<game> get_game() const;
64
65 std::shared_ptr<game>& get_game();
66
67 int game_id() const;
68
69 void set_game(std::shared_ptr<game> new_game);
70
71 void enter_lobby();
72
73 std::chrono::steady_clock::duration time_logged_on() const
74 {
75 return std::chrono::steady_clock::now() - login_time_;
76 }
77
78private:
79 std::chrono::steady_clock::time_point login_time_;
81 mutable player player_;
82 std::shared_ptr<game> game_;
83 std::string ip_address;
84};
85
86struct socket_t {};
87struct name_t {};
88struct game_t {};
89
90namespace bmi = boost::multi_index;
91
92// Key extraction declarations for use by key-based indices to obtain the indexing keys from the elements of the multi_index_container.
93// const_mem_fun is a Key Extractor returning as key the result of invoking a given constant member function of a class.
94using extract_by_socket = bmi::const_mem_fun<player_record, const any_socket_ptr, &player_record::socket>;
95using extract_by_name = bmi::const_mem_fun<player_record, const std::string&, &player_record::name>;
96using extract_by_game = bmi::const_mem_fun<player_record, int, &player_record::game_id>;
97
98// Index declarations with assigned tags for easier access.
99using index_by_socket = bmi::ordered_unique<bmi::tag<socket_t>, extract_by_socket>;
100using index_by_name = bmi::hashed_unique<bmi::tag<name_t>, extract_by_name>;
101using index_by_game = bmi::ordered_non_unique<bmi::tag<game_t>, extract_by_game>;
102
103using player_connections = boost::multi_index_container<player_record, bmi::indexed_by<index_by_socket, index_by_name, index_by_game>>;
104
105typedef player_connections::index<socket_t>::type player_by_socket;
106typedef player_connections::index<name_t> ::type player_by_name;
107typedef player_connections::index<game_t> ::type player_by_game;
108
109typedef player_connections::const_iterator player_iterator;
110
111} // namespace wesnothd
std::string client_ip() const
const std::string & name() const
const any_socket_ptr socket() const
void set_game(std::shared_ptr< game > new_game)
std::chrono::steady_clock::time_point login_time_
std::chrono::steady_clock::duration time_logged_on() const
std::shared_ptr< game > game_
const std::shared_ptr< game > get_game() const
player_record(const SocketPtr socket, Args &&... args)
const any_socket_ptr socket_
const std::string & name() const
Definition player.hpp:55
STL namespace.
boost::multi_index_container< player_record, bmi::indexed_by< index_by_socket, index_by_name, index_by_game > > player_connections
player_connections::index< socket_t >::type player_by_socket
bmi::const_mem_fun< player_record, const std::string &, &player_record::name > extract_by_name
bmi::hashed_unique< bmi::tag< name_t >, extract_by_name > index_by_name
bmi::const_mem_fun< player_record, const any_socket_ptr, &player_record::socket > extract_by_socket
player_connections::index< game_t >::type player_by_game
player_connections::const_iterator player_iterator
bmi::ordered_unique< bmi::tag< socket_t >, extract_by_socket > index_by_socket
bmi::const_mem_fun< player_record, int, &player_record::game_id > extract_by_game
bmi::ordered_non_unique< bmi::tag< game_t >, extract_by_game > index_by_game
player_connections::index< name_t >::type player_by_name
std::string client_address(SocketPtr socket)
Base class for servers using Wesnoth's WML over TCP protocol.
utils::variant< socket_ptr, tls_socket_ptr > any_socket_ptr