The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
network_asio.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 #ifndef NETWORK_ASIO_HPP_INCLUDED
16 #define NETWORK_ASIO_HPP_INCLUDED
17 
18 #ifdef _WIN32
19 # define BOOST_ASIO_DISABLE_IOCP
20 # ifdef INADDR_ANY
21 # undef INADDR_ANY
22 # endif
23 # ifdef INADDR_BROADCAST
24 # undef INADDR_BROADCAST
25 # endif
26 # ifdef INADDR_NONE
27 # undef INADDR_NONE
28 # endif
29 #endif
30 
31 #include <boost/asio.hpp>
32 #include <boost/optional.hpp>
33 #include "exceptions.hpp"
34 
35 class config;
36 
37 namespace network_asio {
38 
39 struct error : public game::error
40 {
41  error(const boost::system::error_code& error) : game::error(error.message()) {}
42 };
43 
44 /** A class that represents a TCP/IP connection. */
46 {
47  boost::asio::io_service io_service_;
48  typedef boost::asio::ip::tcp::resolver resolver;
49  resolver resolver_;
50 
51  typedef boost::asio::ip::tcp::socket socket;
52  socket socket_;
53 
54  bool done_;
55 
56  boost::asio::streambuf write_buf_;
57  boost::asio::streambuf read_buf_;
58 
59  void handle_resolve(
60  const boost::system::error_code& ec,
62  );
63 
65  void handle_connect(
66  const boost::system::error_code& ec,
68  );
69  void handshake();
70  void handle_handshake(
71  const boost::system::error_code& ec
72  );
73  union {
74  char binary[4];
77 
78  std::size_t is_write_complete(
79  const boost::system::error_code& error,
80  std::size_t bytes_transferred
81  );
82  void handle_write(
83  const boost::system::error_code& ec,
84  std::size_t bytes_transferred
85  );
86  std::size_t is_read_complete(
87  const boost::system::error_code& error,
88  std::size_t bytes_transferred
89  );
90  void handle_read(
91  const boost::system::error_code& ec,
92  std::size_t bytes_transferred,
93  config& response
94  );
96  std::size_t bytes_to_write_;
97  std::size_t bytes_written_;
98  std::size_t bytes_to_read_;
99  std::size_t bytes_read_;
100 
101  public:
102  /**
103  * Constructor.
104  *
105  * @param host Name of the host to connect to
106  * @param service Service identifier such as "80" or "http"
107  */
108  connection(const std::string& host, const std::string& service);
109 
110  void transfer(const config& request, config& response);
111 
112  /** Handle all pending asynchonous events and return */
113  std::size_t poll()
114  {
115  try {
116  return io_service_.poll();
117  } catch(const boost::system::system_error& err) {
118  if(err.code() == boost::asio::error::operation_aborted)
119  return 1;
120  throw error(err.code());
121  }
122  }
123  /** Run asio's event loop
124  *
125  * Handle asynchronous events blocking until all asynchronous
126  * operations have finished
127  */
128  void run() { io_service_.run(); }
129 
130  void cancel();
131 
132  /** True if connected and no high-level operation is in progress */
133  bool done() const { return done_; }
134 
135  std::size_t bytes_to_write() const
136  {
137  return bytes_to_write_;
138  }
139  std::size_t bytes_written() const
140  {
141  return bytes_written_;
142  }
143  std::size_t bytes_to_read() const
144  {
145  return bytes_to_read_;
146  }
147  std::size_t bytes_read() const
148  {
149  return bytes_read_;
150  }
151 };
152 
153 }
154 
155 #endif
boost::asio::ip::tcp::socket socket
void handle_read(const boost::system::error_code &ec, std::size_t bytes_transferred, config &response)
boost::asio::streambuf write_buf_
connection(const std::string &host, const std::string &service)
Constructor.
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:29
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
boost::asio::streambuf read_buf_
std::size_t bytes_to_write() const
void handle_connect(const boost::system::error_code &ec, resolver::iterator iterator)
std::size_t is_write_complete(const boost::system::error_code &error, std::size_t bytes_transferred)
std::size_t bytes_read() const
boost::asio::ip::tcp::resolver resolver
union network_asio::connection::@21 handshake_response_
const GLuint GLenum const GLvoid * binary
Definition: glew.h:3027
void handle_write(const boost::system::error_code &ec, std::size_t bytes_transferred)
std::size_t is_read_complete(const boost::system::error_code &error, std::size_t bytes_transferred)
A class that represents a TCP/IP connection.
error(const boost::system::error_code &error)
std::size_t bytes_to_read() const
void connect(resolver::iterator iterator)
void transfer(const config &request, config &response)
boost::uint32_t payload_size_
boost::asio::io_service io_service_
logger & err()
Definition: log.cpp:79
bool done() const
True if connected and no high-level operation is in progress.
void run()
Run asio's event loop.
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
std::size_t poll()
Handle all pending asynchonous events and return.
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
void handle_resolve(const boost::system::error_code &ec, resolver::iterator iterator)
std::size_t bytes_written() const
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void handle_handshake(const boost::system::error_code &ec)