The Battle for Wesnoth  1.19.5+dev
player_connection.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
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 
26 namespace wesnothd
27 {
28 class game;
29 
31 {
32 public:
33  template<class SocketPtr>
34  player_record(const SocketPtr socket, const player& player)
35  : login_time(std::chrono::steady_clock::now())
36  , socket_(socket)
37  , player_(player)
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  const std::chrono::steady_clock::time_point login_time;
74 
75 private:
77  mutable player player_;
78  std::shared_ptr<game> game_;
79  std::string ip_address;
80 };
81 
82 struct socket_t {};
83 struct name_t {};
84 struct game_t {};
85 
86 namespace bmi = boost::multi_index;
87 
88 using player_connections = bmi::multi_index_container<player_record, bmi::indexed_by<
89  bmi::ordered_unique<bmi::tag<socket_t>,
90  bmi::const_mem_fun<player_record, const any_socket_ptr, &player_record::socket>>,
91  bmi::hashed_unique<bmi::tag<name_t>,
92  bmi::const_mem_fun<player_record, const std::string&, &player_record::name>>,
93  bmi::ordered_non_unique<bmi::tag<game_t>,
94  bmi::const_mem_fun<player_record, int, &player_record::game_id>>
95 >>;
96 
97 typedef player_connections::const_iterator player_iterator;
98 
99 } // namespace wesnothd
std::string client_ip() const
const std::chrono::steady_clock::time_point login_time
const any_socket_ptr socket() const
void set_game(std::shared_ptr< game > new_game)
const std::string & name() const
std::shared_ptr< game > game_
const std::shared_ptr< game > get_game() const
player_record(const SocketPtr socket, const player &player)
const any_socket_ptr socket_
const std::string & name() const
Definition: player.hpp:48
std::string client_address(const any_socket_ptr &sock)
Definition: server.cpp:856
player_connections::const_iterator player_iterator
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
Base class for servers using Wesnoth's WML over TCP protocol.
utils::variant< socket_ptr, tls_socket_ptr > any_socket_ptr
Definition: server_base.hpp:52