The Battle for Wesnoth  1.17.0-dev
playturn_network_adapter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
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 "config.hpp"
18 #include <list>
19 #include <functional>
20 /*
21  The purpose if this class is to preprocess incoming network data, and provide a steam that always returns just one command/action at a time.
22  Especially we want each replay command in his own [turn].
23 */
25 {
26 public:
27  typedef std::function<bool(config&)> source_type;
28 
29  playturn_network_adapter(source_type source);
31 
32  //returns true on success.
33  //dst has to be empty before the call.
34  //after the call dst contains one child when returned true otherwise it's empty.
35  bool read(config& dst);
36  //returns false if there is still data in the internal buffer.
37  bool is_at_end() const;
38  void set_source(source_type source);
39  //returns a function to be passed to set_source.
40  static source_type get_source_from_config(config& src);
41  void push_front(config&& cfg);
42 private:
43  //reads data from the network stream.
44  void read_from_network();
45  //a function to receive data from the network.
46  source_type network_reader_;
47 
48  // note: all of the following could be replaced by a simple std::list<config> if we would
49  // split incoming tags right after we rechived them from network_reader_ the reason
50  // why we currently don'T do that is for performance.
51 
52  //this always contains one empty config because we want a valid value for next_.
53  std::list<config> data_;
54  //packages that the client could not process at that point.
55  std::list<config> data_front_;
56  //the position of the next to be received element in data_->front().
58  //if we are processing a [turn] with multiple [command] we want to split them.
59  //In this case next_command_num_ is the next to be processed turn into a command otherwise it's 0;
60  unsigned int next_command_num_;
61 };
Definitions for the interface to Wesnoth Markup Language (WML).
void set_source(source_type source)
config::all_children_iterator next_
std::function< bool(config &)> source_type
static source_type get_source_from_config(config &src)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
playturn_network_adapter(source_type source)