The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
playturn_network_adapter.cpp
Go to the documentation of this file.
2 #include "config_assign.hpp"
3 #include "log.hpp"
4 
5 #include <boost/assign.hpp>
6 #include "utils/functional.hpp"
7 #include <boost/ref.hpp>
8 #include <iostream>
9 #include <cassert>
10 
11 
12 static lg::log_domain log_network("network");
13 #define LOG_NW LOG_STREAM(info, log_network)
14 #define ERR_NW LOG_STREAM(err, log_network)
15 
17 {
18  assert(!data_.empty());
19 
20  this->data_.push_back(config());
21  config& back = data_.back();
22  bool has_data = false;
23  try
24  {
25  has_data = this->network_reader_(back);
26  }
27  catch(...)
28  {
29  //Readin from network can throw, we want to ignore the possibly corrupt packet in this case.
30  this->data_.pop_back();
31  throw;
32  }
33  //ping is handeled by network.cpp and we can ignore it.
34  back.remove_attribute("ping");
35  if((!has_data) || back.empty())
36  {
37  this->data_.pop_back();
38  return;
39  }
40  assert(!data_.back().empty());
41 
42  if(back.has_attribute("side_drop"))
43  {
44  config child;
45  child["side_num"] = back["side_drop"];
46  child["controller"] = back["controller"];
47  this->data_.push_back(config_of("side_drop", child));
48  back.remove_attribute("side_drop");
49  back.remove_attribute("controller");
50  }
51  else if(back.attribute_range().first != back.attribute_range().second )
52  {
53  ERR_NW << "found unexpected attribute:" <<back.debug() << std::endl;
54  this->data_.pop_back();
55  //ignore those here
56  }
57  assert(!data_.back().empty());
58  //there should be no attributes left.
59 }
60 
62 {
63  assert(!data_.empty());
64  return this->next_ == data_.back().ordered_end();
65 }
66 
68 {
69  assert(dst.empty());
70  if(is_at_end())
71  {
73  }
74  if(is_at_end())
75  {
76  //that means we couldn't read anything from the network.
77  return false;
78  }
79  //skip empty data.
80  while(next_ == data_.begin()->ordered_end())
81  {
82  data_.pop_front();
83  next_ = data_.front().ordered_begin();
84  assert(!is_at_end());
85  }
86  config& child = dst.add_child(next_->key);
87  //TODO: implement a non const version of ordered children
88  config& child_old = const_cast<config&>(next_->cfg);
89  if(next_->key == "turn")
90  {
91  //split [turn] indo different [turn] for each child.
92  assert(next_->cfg.all_children_count() > next_command_num_);
94  std::advance(itor, next_command_num_);
95  //TODO: implement a non const version of ordered children
96  config& childchild_old = const_cast<config&>(itor->cfg);
97  config& childchild = child.add_child(itor->key);
98  childchild.swap(childchild_old);
99 
101  if(next_->cfg.all_children_count() == next_command_num_)
102  {
103  next_command_num_ = 0;
104  ++next_;
105  }
106  return true;
107  }
108  else
109  {
110  child.swap(child_old);
111  ++next_;
112  return true;
113  }
114 }
115 
117  : data_(boost::assign::list_of(config()).convert_to_container<std::list<config> >()),
118  next_(data_.front().ordered_end()),
119  next_command_num_(0),
120  network_reader_(source)
121 {
122 
123 }
124 
125 
127 {
128  try {
129  if(!is_at_end())
130  {
131  LOG_NW << "Destroying playturn_network_adapter with an non empty buffer, this means loss of network data" << std::endl;
132  }
133  } catch (...) {}
134 }
135 
137 {
139 }
140 
141 
142 static bool read_config(config& src, config& dst)
143 {
144  assert(dst.empty());
145  if(!src.empty())
146  {
147  src.swap(dst);
148  return true;
149  }
150  else
151  {
152  return false;
153  }
154 }
155 
157 {
158  return std::bind(read_config, std::ref(cfg), _1);
159 }
void remove_attribute(const std::string &key)
Definition: config.cpp:534
#define ERR_NW
STL namespace.
GLenum src
Definition: glew.h:2392
std::string debug() const
Definition: config.cpp:1438
bool empty() const
Definition: config.cpp:1105
void set_source(source_type source)
#define LOG_NW
void swap(config &cfg)
Definition: config.cpp:1518
GLenum GLenum dst
Definition: glew.h:2392
static lg::log_domain log_network("network")
config & add_child(const std::string &key)
Definition: config.cpp:743
config::all_children_iterator next_
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
std::function< bool(config &)> source_type
const_attr_itors attribute_range() const
Definition: config.cpp:984
static source_type get_source_from_config(config &src)
GLenum GLint ref
Definition: glew.h:1813
bool has_attribute(const std::string &key) const
Definition: config.cpp:514
Standard logging facilities (interface).
static bool read_config(config &src, config &dst)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei GLsizei GLchar * source
Definition: glew.h:1800
all_children_iterator ordered_begin() const
Definition: config.cpp:1117
playturn_network_adapter(source_type source)