The Battle for Wesnoth  1.17.0-dev
player_connection.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
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 
21 
22 #include <boost/multi_index/hashed_index.hpp>
23 #include <boost/multi_index/mem_fun.hpp>
24 #include <boost/multi_index/member.hpp>
25 #include <boost/multi_index/ordered_index.hpp>
26 #include <boost/multi_index_container.hpp>
27 
28 namespace wesnothd
29 {
30 class game;
31 
33 {
34 public:
35  template<class SocketPtr>
36  player_record(const SocketPtr socket, const player& player)
37  : login_time(std::chrono::steady_clock::now())
38  , socket_(socket)
39  , player_(player)
40  , game_()
41  , ip_address(client_address(socket))
42  {
43  }
44 
45  const any_socket_ptr socket() const
46  {
47  return socket_;
48  }
49 
50  std::string client_ip() const
51  {
52  return ip_address;
53  }
54 
55  player& info() const
56  {
57  return player_;
58  }
59 
60  const std::string& name() const
61  {
62  return player_.name();
63  }
64 
65  const std::shared_ptr<game> get_game() const;
66 
67  std::shared_ptr<game>& get_game();
68 
69  int game_id() const;
70 
71  void set_game(std::shared_ptr<game> new_game);
72 
73  void enter_lobby();
74 
75  const std::chrono::time_point<std::chrono::steady_clock> login_time;
76 
77 private:
79  mutable player player_;
80  std::shared_ptr<game> game_;
81  std::string ip_address;
82 };
83 
84 struct socket_t {};
85 struct name_t {};
86 struct game_t {};
87 
88 namespace bmi = boost::multi_index;
89 
90 using player_connections = bmi::multi_index_container<player_record, bmi::indexed_by<
91  bmi::ordered_unique<bmi::tag<socket_t>,
92  bmi::const_mem_fun<player_record, const any_socket_ptr, &player_record::socket>>,
93  bmi::hashed_unique<bmi::tag<name_t>,
94  bmi::const_mem_fun<player_record, const std::string&, &player_record::name>>,
95  bmi::ordered_non_unique<bmi::tag<game_t>,
96  bmi::const_mem_fun<player_record, int, &player_record::game_id>>
97 >>;
98 
99 typedef player_connections::const_iterator player_iterator;
100 
101 } // namespace wesnothd
const any_socket_ptr socket_
const any_socket_ptr socket() const
const std::string & name() const
Definition: player.hpp:48
STL namespace.
std::shared_ptr< game > game_
utils::variant< socket_ptr, tls_socket_ptr > any_socket_ptr
Definition: server_base.hpp:52
player_connections::const_iterator player_iterator
std::string client_address(const any_socket_ptr &sock)
Definition: server.cpp:830
bmi::multi_index_container< player_record, bmi::indexed_by< bmi::ordered_unique< bmi::tag< socket_t >, bmi::const_mem_fun< player_record, const any_socket_ptr, &player_record::socket > >, bmi::hashed_unique< bmi::tag< name_t >, bmi::const_mem_fun< player_record, const std::string &, &player_record::name > >, bmi::ordered_non_unique< bmi::tag< game_t >, bmi::const_mem_fun< player_record, int, &player_record::game_id > > > > player_connections
const std::chrono::time_point< std::chrono::steady_clock > login_time
const std::string & name() const
const std::shared_ptr< game > get_game() const
void set_game(std::shared_ptr< game > new_game)
player_record(const SocketPtr socket, const player &player)
Base class for servers using Wesnoth&#39;s WML over TCP protocol.
std::string client_ip() const