The Battle for Wesnoth  1.15.2+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 {
34 class game;
35 
37 {
38 public:
40  : socket_(socket)
41  , player_(player)
42  , game_()
43  , ip_address(client_address(socket))
44  {
45  }
46 
47  const socket_ptr socket() const
48  {
49  return socket_;
50  }
51 
52  std::string saved_client_ip() const
53  {
54  return ip_address;
55  }
56 
57  player& info() const
58  {
59  return player_;
60  }
61 
62  const std::string& name() const
63  {
64  return player_.name();
65  }
66 
67  const std::shared_ptr<game> get_game() const;
68 
69  std::shared_ptr<game>& get_game();
70 
71  int game_id() const;
72 
73  void set_game(std::shared_ptr<game> new_game);
74 
75  void enter_lobby();
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 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 } // namespace wesnothd
const std::string & name() const
Definition: player.hpp:47
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.