The Battle for Wesnoth  1.15.12+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:
35  : socket_(socket)
36  , player_(player)
37  , game_()
38  , ip_address(client_address(socket))
39  {
40  }
41 
42  const socket_ptr socket() const
43  {
44  return socket_;
45  }
46 
47  std::string client_ip() const
48  {
49  return ip_address;
50  }
51 
52  player& info() const
53  {
54  return player_;
55  }
56 
57  const std::string& name() const
58  {
59  return player_.name();
60  }
61 
62  const std::shared_ptr<game> get_game() const;
63 
64  std::shared_ptr<game>& get_game();
65 
66  int game_id() const;
67 
68  void set_game(std::shared_ptr<game> new_game);
69 
70  void enter_lobby();
71 
72 private:
74  mutable player player_;
75  std::shared_ptr<game> game_;
76  std::string ip_address;
77 };
78 
79 struct socket_t {};
80 struct name_t {};
81 struct game_t {};
82 
83 namespace bmi = boost::multi_index;
84 
85 using player_connections = bmi::multi_index_container<player_record, bmi::indexed_by<
86  bmi::ordered_unique<bmi::tag<socket_t>,
87  bmi::const_mem_fun<player_record, const socket_ptr, &player_record::socket>>,
88  bmi::hashed_unique<bmi::tag<name_t>,
89  bmi::const_mem_fun<player_record, const std::string&, &player_record::name>>,
90  bmi::ordered_non_unique<bmi::tag<game_t>,
91  bmi::const_mem_fun<player_record, int, &player_record::game_id>>
92 >>;
93 
94 typedef player_connections::const_iterator player_iterator;
95 
96 } // namespace wesnothd
const std::string & name() const
Definition: player.hpp:47
std::shared_ptr< game > game_
player_connections::const_iterator player_iterator
const socket_ptr socket() const
const std::string & name() const
player_record(const socket_ptr socket, const player &player)
std::string client_address(const socket_ptr socket)
const std::shared_ptr< game > get_game() const
void set_game(std::shared_ptr< game > new_game)
bmi::multi_index_container< player_record, bmi::indexed_by< bmi::ordered_unique< bmi::tag< socket_t >, bmi::const_mem_fun< player_record, const 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
Definition: ban.cpp:28
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:43
Base class for servers using Wesnoth&#39;s WML over TCP protocol.
std::string client_ip() const