The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wesnothd_connection.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Sergey Popov <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifdef _WIN32
18 # define BOOST_ASIO_DISABLE_IOCP
19 # ifdef INADDR_ANY
20 # undef INADDR_ANY
21 # endif
22 # ifdef INADDR_BROADCAST
23 # undef INADDR_BROADCAST
24 # endif
25 # ifdef INADDR_NONE
26 # undef INADDR_NONE
27 # endif
28 #endif
29 
30 #include <boost/asio.hpp>
31 #include <boost/optional.hpp>
32 #include <deque>
33 #include <list>
34 #include "exceptions.hpp"
36 #include "configr_assign.hpp"
37 class config;
38 
39 /** A class that represents a TCP/IP connection to the wesnothd server. */
40 class twesnothd_connection : boost::noncopyable
41 {
42 public:
44 
45  /**
46  * Constructor.
47  *
48  * @param host Name of the host to connect to
49  * @param service Service identifier such as "80" or "http"
50  */
51  twesnothd_connection(const std::string& host, const std::string& service);
52 
53  void send_data(const configr_of& request);
54 
55  bool receive_data(config& result);
56 
57  /** Handle all pending asynchonous events and return */
58  std::size_t poll();
59  /** Run asio's event loop
60  *
61  * Handle asynchronous events blocking until all asynchronous
62  * operations have finished
63  */
64 
65  void cancel();
66 
67  /** True if connected and no high-level operation is in progress */
68  bool handshake_finished() const { return handshake_finished_; }
69 
70  std::size_t bytes_to_write() const
71  {
72  return bytes_to_write_;
73  }
74  std::size_t bytes_written() const
75  {
76  return bytes_written_;
77  }
78  std::size_t bytes_to_read() const
79  {
80  return bytes_to_read_;
81  }
82  std::size_t bytes_read() const
83  {
84  return bytes_read_;
85  }
86  bool has_data_received() const
87  {
88  return !recv_queue_.empty();
89  }
90  bool is_sending_data() const
91  {
92  return !send_queue_.empty();
93  }
94 private:
95  boost::asio::io_service io_service_;
96  typedef boost::asio::ip::tcp::resolver resolver;
97  resolver resolver_;
98 
99  typedef boost::asio::ip::tcp::socket socket;
100  socket socket_;
101 
103 
104  boost::asio::streambuf read_buf_;
105 
106  void handle_resolve(
107  const boost::system::error_code& ec,
109  );
110 
112  void handle_connect(
113  const boost::system::error_code& ec,
115  );
116  void handshake();
117  void handle_handshake(
118  const boost::system::error_code& ec
119  );
120  union {
121  char binary[4];
124 
125  std::size_t is_write_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
126  void handle_write(const boost::system::error_code& ec, std::size_t bytes_transferred);
127  std::size_t is_read_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
128  void handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred);
129 
130  void send();
131  void recv();
132 
133  std::list<boost::asio::streambuf> send_queue_;
134  std::list<config> recv_queue_;
135 
137  std::size_t bytes_to_write_;
138  std::size_t bytes_written_;
139  std::size_t bytes_to_read_;
140  std::size_t bytes_read_;
141 
142 };
an error occured inside the underlying network comminication code (boost asio) TODO: find a short nam...
std::size_t bytes_read() const
void connect(resolver::iterator iterator)
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
void handle_handshake(const boost::system::error_code &ec)
std::size_t bytes_to_read() const
void handle_resolve(const boost::system::error_code &ec, resolver::iterator iterator)
bool receive_data(config &result)
void cancel()
Run asio's event loop.
std::size_t is_read_complete(const boost::system::error_code &error, std::size_t bytes_transferred)
void send_data(const configr_of &request)
void handle_read(const boost::system::error_code &ec, std::size_t bytes_transferred)
GLuint64EXT * result
Definition: glew.h:10727
std::size_t poll()
Handle all pending asynchonous events and return.
A class that represents a TCP/IP connection to the wesnothd server.
const GLuint GLenum const GLvoid * binary
Definition: glew.h:3027
std::list< config > recv_queue_
boost::asio::io_service io_service_
std::size_t bytes_to_write() const
bool handshake_finished() const
True if connected and no high-level operation is in progress.
void handle_write(const boost::system::error_code &ec, std::size_t bytes_transferred)
boost::asio::streambuf read_buf_
twesnothd_connection(const std::string &host, const std::string &service)
Constructor.
std::list< boost::asio::streambuf > send_queue_
wesnothd_connection_error error
void handle_connect(const boost::system::error_code &ec, resolver::iterator iterator)
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
union twesnothd_connection::@26 handshake_response_
std::size_t is_write_complete(const boost::system::error_code &error, std::size_t bytes_transferred)
boost::asio::ip::tcp::socket socket
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::size_t bytes_written() const
boost::asio::ip::tcp::resolver resolver