The Battle for Wesnoth  1.15.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 
17 #include "server/player.hpp"
18 #include "server/server_base.hpp"
19 #include "server/simple_wml.hpp"
20 
21 #ifndef _WIN32
22 #define BOOST_ASIO_DISABLE_THREADS
23 #endif
24 #include <boost/asio.hpp>
25 
26 #include <boost/multi_index/hashed_index.hpp>
27 #include <boost/multi_index/mem_fun.hpp>
28 #include <boost/multi_index/member.hpp>
29 #include <boost/multi_index/ordered_index.hpp>
30 #include <boost/multi_index_container.hpp>
31 
32 namespace wesnothd
33 {
35 {
36 public:
38  : socket_(socket)
39  , player_(player)
40  , game_()
41  , ip_address(client_address(socket))
42  {
43  }
44 
45  const socket_ptr socket() const
46  {
47  return socket_;
48  }
49 
50  std::string saved_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 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 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 } // namespace wesnothd
const std::string & name() const
Definition: player.hpp:49
std::shared_ptr< game > game_
const socket_ptr socket() const
std::string saved_client_ip() 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:30
Base class for servers using Wesnoth&#39;s WML over TCP protocol.