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