network.hpp

Go to the documentation of this file.
00001 /* $Id: network.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /** @file */
00017 
00018 #ifndef NETWORK_HPP_INCLUDED
00019 #define NETWORK_HPP_INCLUDED
00020 /**
00021  * Enable bandwidth stats
00022  **/
00023 
00024 class config;
00025 
00026 #include "exceptions.hpp"
00027 #include "SDL_net.h"
00028 
00029 #include <string>
00030 #include <vector>
00031 
00032 #include <boost/shared_ptr.hpp>
00033 
00034 namespace threading
00035 {
00036     class waiter;
00037 }
00038 
00039 // This module wraps the network interface.
00040 
00041 namespace network {
00042 
00043 struct pending_statistics {
00044     int npending_sends;
00045     int nbytes_pending_sends;
00046 };
00047 
00048 pending_statistics get_pending_stats();
00049 
00050 // A network manager must be created before networking can be used.
00051 // It must be destroyed only after all networking activity stops.
00052 
00053 // min_threads is the maximum number we allow to wait,
00054 // if more threads attempt to wait, they will die.
00055 // If min_threads == 0 no thread will ever be destroyed,
00056 // and we will stay at the max number of threads ever needed.
00057 
00058 // max_threads is the overall max number of helper threads.
00059 // If we have that many threads already running, we will never create more.
00060 // If max_threads == 0 we will always create a thread if we need it.
00061 struct manager {
00062     explicit manager(size_t min_threads = 1,size_t max_threads = 0);
00063     ~manager();
00064 
00065 private:
00066     bool free_;
00067 
00068     manager(const manager&);
00069     void operator=(const manager&);
00070 };
00071 
00072 void set_raw_data_only();
00073 
00074 typedef int connection;
00075 connection const null_connection = 0;
00076 
00077 /**
00078  * A server manager causes listening on a given port
00079  * to occur for the duration of its lifetime.
00080  */
00081 struct server_manager {
00082 
00083     /** Parameter to pass to the constructor. */
00084     enum CREATE_SERVER { MUST_CREATE_SERVER,    /**< Will throw exception on failure. */
00085                          TRY_CREATE_SERVER,     /**< Will swallow failure. */
00086                          NO_SERVER };           /**< Won't try to create a server at all. */
00087 
00088     // Throws error.
00089     server_manager(int port, CREATE_SERVER create_server=MUST_CREATE_SERVER);
00090     ~server_manager();
00091 
00092     bool is_running() const;
00093     void stop();
00094 
00095 private:
00096     bool free_;
00097     connection connection_;
00098 };
00099 
00100 /** @name Proxy Settings Methods
00101  *
00102  * Methods to configure the connection of a client through a proxy server.
00103  */
00104 //@{
00105 /**
00106  * Attempt to connect through a proxy (as opposed to directly.)
00107  *
00108  * Use the set_proxy_* methods to configure the connection options.
00109  */
00110 void enable_connection_through_proxy();
00111 
00112 /**
00113  * Set the address of the proxy. Default: "localhost".
00114  *
00115  * @param address: Network address where the proxy server should be running.
00116  */
00117 void set_proxy_address ( const std::string& address  );
00118 
00119 /**
00120  * Set the port of the proxy. Default: "3128".
00121  *
00122  * @param port: Network port where the proxy server should be listening.
00123  */
00124 void set_proxy_port    ( const std::string& port     );
00125 
00126 /**
00127  * Set the user to authenticate with the proxy. Default: "".
00128  *
00129  * @param user: User name to use for authentication purposes.
00130  */
00131 void set_proxy_user    ( const std::string& user     );
00132 
00133 /**
00134  * Set the password to authenticate with the proxy. Default: "".
00135  *
00136  * @param password: Password to use for authentication purposes.
00137  */
00138 void set_proxy_password( const std::string& password );
00139 //@}
00140 
00141 /** The number of peers we are connected to. */
00142 size_t nconnections();
00143 
00144 /** If we are currently accepting connections. */
00145 bool is_server();
00146 
00147 /**
00148  * Function to attempt to connect to a remote host.
00149  *
00150  * @returns                       The new connection on success, or 0 on failure.
00151  * @throw error
00152  */
00153 connection connect(const std::string& host, int port=15000);
00154 
00155 connection connect(const std::string& host, int port, threading::waiter& waiter);
00156 
00157 /**
00158  * Function to accept a connection from a remote host.
00159  *
00160  * If no host is attempting to connect, it will return 0 immediately.
00161  * Otherwise returns the new connection.
00162  *
00163  * @throw error
00164  */
00165 connection accept_connection();
00166 
00167 /**
00168  * Function to disconnect from a certain host,
00169  * or close all connections if connection_num is 0.
00170  * Returns true if the connection was disconnected.
00171  * Returns false on failure to disconnect, since the socket is
00172  * in the middle of sending/receiving data.
00173  * The socket will be closed when it has finished its send/receive.
00174  */
00175 bool disconnect(connection connection_num=0);
00176 
00177 /**
00178  * Function to queue a disconnection.
00179  *
00180  * Next time receive_data is called, it will generate an error
00181  * on the given connection (and presumably then the handling of the error
00182  * will include closing the connection).
00183  */
00184 void queue_disconnect(connection connection_num);
00185 
00186 std::string get_bandwidth_stats();
00187 std::string get_bandwidth_stats_all();
00188 std::string get_bandwidth_stats(int hour);
00189 
00190 void add_bandwidth_out(const std::string& packet_type, size_t len);
00191 void add_bandwidth_in(const std::string& packet_type, size_t len);
00192 struct bandwidth_in {
00193     bandwidth_in(int len) : len_(len), type_("unknown") {}
00194     ~bandwidth_in();
00195 
00196     void set_type(const std::string& type)
00197     {
00198         type_ = type;
00199     }
00200 
00201     private:
00202     int len_;
00203     std::string type_;
00204 };
00205 
00206 typedef boost::shared_ptr<bandwidth_in> bandwidth_in_ptr;
00207 
00208 
00209 /**
00210  * Function to receive data from either a certain connection,
00211  * or all connections if connection_num is 0.
00212  * Will store the data received in cfg.
00213  * Times out after timeout milliseconds.
00214  *
00215  * @returns                       The connection that data was received from,
00216  *                                or 0 if timeout occurred.
00217  *
00218  * @throw error                   If an error occurred.
00219  */
00220 connection receive_data(config& cfg, connection connection_num=0, bandwidth_in_ptr* b = 0);
00221 connection receive_data(config& cfg, connection connection_num, unsigned int timeout, bandwidth_in_ptr* b = 0);
00222 connection receive_data(std::vector<char>& buf, bandwidth_in_ptr* = 0);
00223 
00224 void send_file(const std::string&, connection, const std::string& packet_type = "unknown");
00225 
00226 /**
00227  * Function to send data down a given connection,
00228  * or broadcast to all peers if connection_num is 0.
00229  *
00230  * @throw error
00231  */
00232 size_t send_data(const config& cfg, connection connection_num = 0,
00233         const std::string& packet_type = "unknown");
00234 
00235 void send_raw_data(const char* buf, int len, connection connection_num,
00236         const std::string& packet_type = "unknown");
00237 
00238 /**
00239  * Function to send any data that is in a connection's send_queue,
00240  * up to a maximum of 'max_size' bytes --
00241  * or the entire send queue if 'max_size' bytes is 0.
00242  */
00243 void process_send_queue(connection connection_num=0, size_t max_size=0);
00244 
00245 /** Function to send data to all peers except 'connection_num'. */
00246 void send_data_all_except(const config& cfg, connection connection_num,
00247         const std::string& packet_type = "unknown");
00248 
00249 /** Function to get the remote ip address of a socket. */
00250 std::string ip_address(connection connection_num);
00251 
00252 struct connection_stats
00253 {
00254     connection_stats(int sent, int received, int connected_at);
00255 
00256     int bytes_sent, bytes_received;
00257     int time_connected;
00258 };
00259 
00260 connection_stats get_connection_stats(connection connection_num);
00261 
00262 struct error : public game::error
00263 {
00264     error(const std::string& msg="", connection sock=0);
00265     connection socket;
00266 
00267     void disconnect();
00268 };
00269 
00270 struct statistics
00271 {
00272     statistics() : total(0), current(0), current_max(0) {}
00273     void fresh_current(size_t len)
00274     {
00275         current = 0;
00276         current_max = len;
00277     }
00278     void transfer(size_t size)
00279     {
00280         total += size;
00281         current += size;
00282     }
00283     bool operator==(const statistics& stats) const
00284     {
00285         return total == stats.total && current == stats.current && current_max == stats.current_max;
00286     }
00287     bool operator!=(const statistics& stats) const
00288     {
00289         return !operator==(stats);
00290     }
00291     size_t total        /** The accumulated bytes of the session.                   */ ;
00292     size_t current      /** The current buffer accumulated bytes (sent/received.)   */ ;
00293     size_t current_max  /** The current buffer size (i.e. being received/sent.)     */ ;
00294 };
00295 
00296 /** Function to see the number of bytes being processed on the current socket. */
00297 statistics get_send_stats(connection handle);
00298 statistics get_receive_stats(connection handle);
00299 
00300 /** Amount of seconds after the last server ping when we assume to have timed out. */
00301 extern unsigned int ping_timeout;
00302 /** Minimum interval between pings. */
00303 const int ping_interval = 30;
00304 } // network namespace
00305 
00306 
00307 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:07 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs