The Battle for Wesnoth  1.17.0-dev
player_connection.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by Sergey Popov <loonycyborg@gmail.com>
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 as published by
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 #pragma once
16 
20 
21 #include <boost/multi_index/hashed_index.hpp>
22 #include <boost/multi_index/mem_fun.hpp>
23 #include <boost/multi_index/member.hpp>
24 #include <boost/multi_index/ordered_index.hpp>
25 #include <boost/multi_index_container.hpp>
26 
27 namespace wesnothd
28 {
29 class game;
30 
32 {
33 public:
34  template<class SocketPtr>
35  player_record(const SocketPtr socket, const player& player)
36  : socket_(socket)
37  , player_(player)
38  , game_()
39  , ip_address(client_address(socket))
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 private:
75  mutable player player_;
76  std::shared_ptr<game> game_;
77  std::string ip_address;
78 };
79 
80 struct socket_t {};
81 struct name_t {};
82 struct game_t {};
83 
84 namespace bmi = boost::multi_index;
85 
86 using player_connections = bmi::multi_index_container<player_record, bmi::indexed_by<
87  bmi::ordered_unique<bmi::tag<socket_t>,
88  bmi::const_mem_fun<player_record, const any_socket_ptr, &player_record::socket>>,
89  bmi::hashed_unique<bmi::tag<name_t>,
90  bmi::const_mem_fun<player_record, const std::string&, &player_record::name>>,
91  bmi::ordered_non_unique<bmi::tag<game_t>,
92  bmi::const_mem_fun<player_record, int, &player_record::game_id>>
93 >>;
94 
95 typedef player_connections::const_iterator player_iterator;
96 
97 } // namespace wesnothd
const any_socket_ptr socket_
const any_socket_ptr socket() const
const std::string & name() const
Definition: player.hpp:47
std::shared_ptr< game > game_
utils::variant< socket_ptr, tls_socket_ptr > any_socket_ptr
Definition: server_base.hpp:51
player_connections::const_iterator player_iterator
std::string client_address(const any_socket_ptr &sock)
Definition: server.cpp:829
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::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