network.hpp

Go to the documentation of this file.
00001 /* $Id: network.hpp 26456 2008-05-08 12:02:24Z suokko $ */
00002 /*
00003    Copyright (C) 2003 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file network.hpp
00016 //!
00017 
00018 #ifndef NETWORK_HPP_INCLUDED
00019 #define NETWORK_HPP_INCLUDED
00020 
00021 class config;
00022 
00023 #include "SDL_net.h"
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 namespace threading
00029 {
00030     class waiter;
00031 }
00032 
00033 // This module wraps the network interface.
00034 
00035 namespace network {
00036 
00037 struct pending_statistics {
00038     int npending_sends;
00039     int nbytes_pending_sends;
00040 };
00041 
00042 pending_statistics get_pending_stats();
00043 
00044 // A network manager must be created before networking can be used.
00045 // It must be destroyed only after all networking activity stops.
00046 
00047 // min_threads is the maximum number we allow to wait,
00048 // if more threads attempt to wait, they will die.
00049 // If min_threads == 0 no thread will ever be destroyed,
00050 // and we will stay at the max number of threads ever needed.
00051 
00052 // max_threads is the overall max number of helper threads.
00053 // If we have that many threads already running, we will never create more.
00054 // If max_threads == 0 we will always create a thread if we need it.
00055 struct manager {
00056     explicit manager(size_t min_threads = 1,size_t max_threads = 0);
00057     ~manager();
00058 
00059 private:
00060     bool free_;
00061 
00062     manager(const manager&);
00063     void operator=(const manager&);
00064 };
00065 
00066 void set_raw_data_only();
00067 
00068 //! A server manager causes listening on a given port
00069 //! to occur for the duration of its lifetime.
00070 struct server_manager {
00071 
00072     //! Parameter to pass to the constructor.
00073     enum CREATE_SERVER { MUST_CREATE_SERVER,    //!< Will throw exception on failure
00074                          TRY_CREATE_SERVER, //!< Will swallow failure
00075                          NO_SERVER };           //!< Won't try to create a server at all
00076 
00077     // Throws error.
00078     server_manager(int port, CREATE_SERVER create_server=MUST_CREATE_SERVER);
00079     ~server_manager();
00080 
00081     bool is_running() const;
00082     void stop();
00083 
00084 private:
00085     bool free_;
00086 };
00087 
00088 typedef int connection;
00089 
00090 connection const null_connection = 0;
00091 
00092 //! The number of peers we are connected to.
00093 size_t nconnections();
00094 
00095 //! If we are currently accepting connections.
00096 bool is_server();
00097 
00098 //! Function to attempt to connect to a remote host.
00099 //! Returns the new connection on success, or 0 on failure.
00100 //! Throws error.
00101 connection connect(const std::string& host, int port=15000);
00102 
00103 connection connect(const std::string& host, int port, threading::waiter& waiter);
00104 
00105 //! Function to accept a connection from a remote host.
00106 //! If no host is attempting to connect, it will return 0 immediately.
00107 //! Otherwise returns the new connection.
00108 //! Throws error.
00109 connection accept_connection();
00110 
00111 //! Function to disconnect from a certain host,
00112 //! or close all connections if connection_num is 0.
00113 //! Returns true if the connection was disconnected.
00114 //! Returns false on failure to disconnect, since the socket is
00115 //! in the middle of sending/receiving data.
00116 //! The socket will be closed when it has finished its send/receive.
00117 bool disconnect(connection connection_num=0, bool force=false);
00118 
00119 //! Function to queue a disconnection.
00120 //! Next time receive_data is called, it will generate an error
00121 //! on the given connection (and presumably then the handling of the error
00122 //! will include closing the connection).
00123 void queue_disconnect(connection connection_num);
00124 
00125 //! Function to receive data from either a certain connection,
00126 //! or all connections if connection_num is 0.
00127 //! Will store the data received in cfg.
00128 //! Times out after timeout milliseconds.
00129 //! Returns the connection that data was received from,
00130 //! or 0 if timeout occurred.
00131 //! Throws error if an error occurred.
00132 connection receive_data(config& cfg, connection connection_num=0);
00133 connection receive_data(config& cfg, connection connection_num, unsigned int timeout);
00134 connection receive_data(std::vector<char>& buf);
00135 
00136 //! Function to send data down a given connection,
00137 //! or broadcast to all peers if connection_num is 0.
00138 //! Throws error.
00139 void send_data(const config& cfg, connection connection_num /*= 0*/, const bool gzipped);
00140 
00141 void send_raw_data(const char* buf, int len, connection connection_num);
00142 
00143 //! Function to send any data that is in a connection's send_queue,
00144 //! up to a maximum of 'max_size' bytes --
00145 //! or the entire send queue if 'max_size' bytes is 0.
00146 void process_send_queue(connection connection_num=0, size_t max_size=0);
00147 
00148 //! Function to send data to all peers except 'connection_num'.
00149 void send_data_all_except(const config& cfg, connection connection_num, const bool gzipped);
00150 
00151 //! Function to get the remote ip address of a socket.
00152 std::string ip_address(connection connection_num);
00153 
00154 struct connection_stats
00155 {
00156     connection_stats(int sent, int received, int connected_at);
00157 
00158     int bytes_sent, bytes_received;
00159     int time_connected;
00160 };
00161 
00162 connection_stats get_connection_stats(connection connection_num);
00163 
00164 struct error
00165 {
00166     error(const std::string& msg="", connection sock=0);
00167     std::string message;
00168     connection socket;
00169 
00170     void disconnect();
00171 };
00172 
00173 struct statistics
00174 {
00175     statistics() : total(0), current(0), current_max(0) {}
00176     void fresh_current(size_t len)
00177     {
00178         current = 0;
00179         current_max = len;
00180     }
00181     void transfer(size_t size)
00182     {
00183         total += size;
00184         current += size;
00185     }
00186     bool operator==(const statistics& stats) const
00187     {
00188         return total == stats.total && current == stats.current && current_max == stats.current_max;
00189     }
00190     bool operator!=(const statistics& stats) const
00191     {
00192         return !operator==(stats);
00193     }
00194     size_t total;
00195     size_t current;
00196     size_t current_max;
00197 };
00198 
00199 //! Function to see the number of bytes being processed on the current socket.
00200 statistics get_send_stats(connection handle);
00201 statistics get_receive_stats(connection handle);
00202 
00203 //! Amount of seconds after the last server ping when we assume to have timed out.
00204 extern unsigned int ping_timeout;
00205 } // network namespace
00206 
00207 
00208 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs