The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
network.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[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 /** @file */
16 
17 #ifndef NETWORK_HPP_INCLUDED
18 #define NETWORK_HPP_INCLUDED
19 /**
20  * Enable bandwidth stats
21  **/
22 
23 class config;
24 
25 #include "exceptions.hpp"
26 #include <SDL_net.h>
27 
28 #include <string>
29 #include <vector>
30 
31 #include <boost/shared_ptr.hpp>
32 #include <boost/exception/error_info.hpp>
33 
34 namespace threading
35 {
36  class waiter;
37 }
38 
39 // This module wraps the network interface.
40 
41 namespace network {
42 
46 };
47 
49 
50 // A network manager must be created before networking can be used.
51 // It must be destroyed only after all networking activity stops.
52 
53 // min_threads is the maximum number we allow to wait,
54 // if more threads attempt to wait, they will die.
55 // If min_threads == 0 no thread will ever be destroyed,
56 // and we will stay at the max number of threads ever needed.
57 
58 // max_threads is the overall max number of helper threads.
59 // If we have that many threads already running, we will never create more.
60 // If max_threads == 0 we will always create a thread if we need it.
61 struct manager {
62  explicit manager(size_t min_threads = 1,size_t max_threads = 0);
63  ~manager();
64 
65 private:
66  bool free_;
67 
68  manager(const manager&);
69  void operator=(const manager&);
70 };
71 
72 void set_raw_data_only();
73 
74 typedef int connection;
75 connection const null_connection = 0;
76 
77 /**
78  * A server manager causes listening on a given port
79  * to occur for the duration of its lifetime.
80  */
82 
83  /** Parameter to pass to the constructor. */
84  enum CREATE_SERVER { MUST_CREATE_SERVER, /**< Will throw exception on failure. */
85  TRY_CREATE_SERVER, /**< Will swallow failure. */
86  NO_SERVER }; /**< Won't try to create a server at all. */
87 
88  // Throws error.
89  server_manager(int port, CREATE_SERVER create_server=MUST_CREATE_SERVER);
91 
92  bool is_running() const;
93  void stop();
94 
95 private:
96  bool free_;
97  connection connection_;
98 };
99 
100 /** @name Proxy Settings Methods
101  *
102  * Methods to configure the connection of a client through a proxy server.
103  */
104 //@{
105 /**
106  * Attempt to connect through a proxy (as opposed to directly.)
107  *
108  * Use the set_proxy_* methods to configure the connection options.
109  */
111 
112 /**
113  * Set the address of the proxy. Default: "localhost".
114  *
115  * @param address Network address where the proxy server should be running.
116  */
117 void set_proxy_address ( const std::string& address );
118 
119 /**
120  * Set the port of the proxy. Default: "3128".
121  *
122  * @param port Network port where the proxy server should be listening.
123  */
124 void set_proxy_port ( const std::string& port );
125 
126 /**
127  * Set the user to authenticate with the proxy. Default: "".
128  *
129  * @param user User name to use for authentication purposes.
130  */
131 void set_proxy_user ( const std::string& user );
132 
133 /**
134  * Set the password to authenticate with the proxy. Default: "".
135  *
136  * @param password Password to use for authentication purposes.
137  */
139 //@}
140 
141 /** The number of peers we are connected to. */
142 size_t nconnections();
143 
144 /** If we are currently accepting connections. */
145 bool is_server();
146 
147 /**
148  * Function to attempt to connect to a remote host.
149  *
150  * @returns The new connection on success, or 0 on failure.
151  * @throw error
152  */
153 connection connect(const std::string& host, int port=15000);
154 
155 connection connect(const std::string& host, int port, threading::waiter& waiter);
156 
157 /**
158  * Function to accept a connection from a remote host.
159  *
160  * If no host is attempting to connect, it will return 0 immediately.
161  * Otherwise returns the new connection.
162  *
163  * @throw error
164  */
165 connection accept_connection();
166 
167 /**
168  * Function to disconnect from a certain host,
169  * or close all connections if connection_num is 0.
170  * Returns true if the connection was disconnected.
171  * Returns false on failure to disconnect, since the socket is
172  * in the middle of sending/receiving data.
173  * The socket will be closed when it has finished its send/receive.
174  */
175 bool disconnect(connection connection_num=0);
176 
177 /**
178  * Function to queue a disconnection.
179  *
180  * Next time receive_data is called, it will generate an error
181  * on the given connection (and presumably then the handling of the error
182  * will include closing the connection).
183  */
184 void queue_disconnect(connection connection_num);
185 
189 
190 void add_bandwidth_out(const std::string& packet_type, size_t len);
191 void add_bandwidth_in(const std::string& packet_type, size_t len);
192 struct bandwidth_in {
193  bandwidth_in(int len) : len_(len), type_("unknown") {}
194  ~bandwidth_in();
195 
196  void set_type(const std::string& type)
197  {
198  type_ = type;
199  }
200 
201  private:
202  int len_;
204 };
205 
207 
208 
209 /**
210  * Function to receive data from either a certain connection,
211  * or all connections if connection_num is 0.
212  * Will store the data received in cfg.
213  * Times out after timeout milliseconds.
214  *
215  * @returns The connection that data was received from,
216  * or 0 if timeout occurred.
217  *
218  * @throw error If an error occurred.
219  */
220 connection receive_data(config& cfg, connection connection_num=0, bandwidth_in_ptr* b = 0);
221 connection receive_data(config& cfg, connection connection_num, unsigned int timeout, bandwidth_in_ptr* b = 0);
222 connection receive_data(std::vector<char>& buf, bandwidth_in_ptr* = 0);
223 
224 void send_file(const std::string&, connection, const std::string& packet_type = "unknown");
225 
226 /**
227  * Function to send data down a given connection,
228  * or broadcast to all peers if connection_num is 0.
229  *
230  * @throw error
231  */
232 size_t send_data(const config& cfg, connection connection_num = 0,
233  const std::string& packet_type = "unknown");
234 
235 void send_raw_data(const char* buf, int len, connection connection_num,
236  const std::string& packet_type = "unknown");
237 
238 /**
239  * Function to send any data that is in a connection's send_queue,
240  * up to a maximum of 'max_size' bytes --
241  * or the entire send queue if 'max_size' bytes is 0.
242  */
243 void process_send_queue(connection connection_num=0, size_t max_size=0);
244 
245 /** Function to send data to all peers except 'connection_num'. */
246 void send_data_all_except(const config& cfg, connection connection_num,
247  const std::string& packet_type = "unknown");
248 
249 /** Function to get the remote ip address of a socket. */
250 std::string ip_address(connection connection_num);
251 
253 {
254  connection_stats(int sent, int received, int connected_at);
255 
258 };
259 
260 connection_stats get_connection_stats(connection connection_num);
261 
262 struct error : public game::error
263 {
264  error(const std::string& msg="", connection sock=0);
265  connection socket;
266 
267  void disconnect();
268 };
269 
270 typedef boost::error_info<struct tag_tcpsocket,TCPsocket> tcpsocket_info;
271 typedef boost::error_info<struct tag_connum,connection> connection_info;
272 
274 {
276  void fresh_current(size_t len)
277  {
278  current = 0;
279  current_max = len;
280  }
281  void transfer(size_t size)
282  {
283  total += size;
284  current += size;
285  }
286  bool operator==(const statistics& stats) const
287  {
288  return total == stats.total && current == stats.current && current_max == stats.current_max;
289  }
290  bool operator!=(const statistics& stats) const
291  {
292  return !operator==(stats);
293  }
294  size_t total /** The accumulated bytes of the session. */ ;
295  size_t current /** The current buffer accumulated bytes (sent/received.) */ ;
296  size_t current_max /** The current buffer size (i.e. being received/sent.) */ ;
297 };
298 
299 /** Function to see the number of bytes being processed on the current socket. */
300 statistics get_send_stats(connection handle);
302 
303 /** Amount of seconds after the last server ping when we assume to have timed out. */
304 extern unsigned int ping_timeout;
305 /** Minimum interval between pings. */
306 const int ping_interval = 30;
307 
308 } // network namespace
309 
310 
311 #endif
void set_raw_data_only()
Definition: network.cpp:280
High level network layer for config object transport.
Definition: network.cpp:212
void fresh_current(size_t len)
Definition: network.hpp:276
connection connection_
Definition: network.hpp:97
const int ping_interval
Minimum interval between pings.
Definition: network.hpp:306
void send_data_all_except(const config &cfg, connection connection_num, const std::string &packet_type)
Function to send data to all peers except 'connection_num'.
Definition: network.cpp:1168
void add_bandwidth_in(const std::string &packet_type, size_t len)
Definition: network.cpp:1059
void queue_disconnect(network::connection sock)
Function to queue a disconnection.
Definition: network.cpp:697
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
void set_proxy_user(const std::string &)
Set the user to authenticate with the proxy.
Definition: network.cpp:300
bool operator==(const statistics &stats) const
Definition: network.hpp:286
std::string type_
Definition: network.hpp:203
connection receive_data(config &cfg, connection connection_num, unsigned int timeout, bandwidth_in_ptr *bandwidth_in)
Definition: network.cpp:702
void process_send_queue(connection, size_t)
Function to send any data that is in a connection's send_queue, up to a maximum of 'max_size' bytes �...
Definition: network.cpp:1163
connection socket
Definition: network.hpp:265
void enable_connection_through_proxy()
Attempt to connect through a proxy (as opposed to directly.)
Definition: network.cpp:286
bool is_running() const
Definition: network.cpp:345
void set_proxy_address(const std::string &)
Set the address of the proxy.
Definition: network.cpp:290
void operator=(const manager &)
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
void send_file(const std::string &filename, connection connection_num, const std::string &packet_type)
Definition: network.cpp:1071
size_t current_max
The current buffer size (i.e.
Definition: network.hpp:296
void set_proxy_port(const std::string &)
Set the port of the proxy.
Definition: network.cpp:295
manager(size_t min_threads=1, size_t max_threads=0)
Definition: network.cpp:248
pending_statistics get_pending_stats()
Definition: network.cpp:243
GLenum GLsizei len
Definition: glew.h:5662
void set_type(const std::string &type)
Definition: network.hpp:196
GLenum GLuint GLsizei const char * buf
Definition: glew.h:2498
unsigned int ping_timeout
Amount of seconds after the last server ping when we assume to have timed out.
Definition: network.cpp:219
bool operator!=(const statistics &stats) const
Definition: network.hpp:290
void set_proxy_password(const std::string &)
Set the password to authenticate with the proxy.
Definition: network.cpp:305
GLuint GLuint64EXT address
Definition: glew.h:11276
size_t current
The current buffer accumulated bytes (sent/received.)
Definition: network.hpp:295
connection_stats(int sent, int received, int connected_at)
Definition: network.cpp:221
void send_raw_data(const char *buf, int len, connection connection_num, const std::string &packet_type)
Definition: network.cpp:1133
CREATE_SERVER
Parameter to pass to the constructor.
Definition: network.hpp:84
statistics get_send_stats(connection handle)
Function to see the number of bytes being processed on the current socket.
Definition: network.cpp:1197
size_t send_data(const config &cfg, connection connection_num, const std::string &packet_type)
Function to send data down a given connection, or broadcast to all peers if connection_num is 0...
Definition: network.cpp:1098
GLbitfield GLuint64 timeout
Definition: glew.h:4717
Will throw exception on failure.
Definition: network.hpp:84
A server manager causes listening on a given port to occur for the duration of its lifetime...
Definition: network.hpp:81
boost::shared_ptr< bandwidth_in > bandwidth_in_ptr
Definition: network.hpp:206
server_manager(int port, CREATE_SERVER create_server=MUST_CREATE_SERVER)
Definition: network.cpp:311
connection accept_connection()
Function to accept a connection from a remote host.
Definition: network.cpp:582
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
bool disconnect(connection s)
Function to disconnect from a certain host, or close all connections if connection_num is 0...
Definition: network.cpp:646
GLsizeiptr size
Definition: glew.h:1649
size_t nconnections()
The number of peers we are connected to.
Definition: network.cpp:350
boost::error_info< struct tag_connum, connection > connection_info
Definition: network.hpp:271
connection connect(const std::string &host, int port)
Function to attempt to connect to a remote host.
Definition: network.cpp:498
std::string ip_address(connection connection_num)
Function to get the remote ip address of a socket.
Definition: network.cpp:1179
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
void transfer(size_t size)
Definition: network.hpp:281
size_t total
The accumulated bytes of the session.
Definition: network.hpp:294
int connection
Definition: network.hpp:74
std::string get_bandwidth_stats_all()
Definition: network.cpp:1014
bool is_server()
If we are currently accepting connections.
Definition: network.cpp:355
boost::error_info< struct tag_tcpsocket, TCPsocket > tcpsocket_info
Definition: network.hpp:270
std::string get_bandwidth_stats()
Definition: network.cpp:1024
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
statistics get_receive_stats(connection handle)
Definition: network.cpp:1201
GLsizei const GLcharARB ** string
Definition: glew.h:4503
connection const null_connection
Definition: network.hpp:75
void disconnect()
Definition: network.cpp:238
connection_stats get_connection_stats(connection connection_num)
Definition: network.cpp:225
boost::shared_ptr< halo_record > handle
Definition: halo.hpp:34
void add_bandwidth_out(const std::string &packet_type, size_t len)
Definition: network.cpp:1052