Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef NETWORK_HPP_INCLUDED
00019 #define NETWORK_HPP_INCLUDED
00020
00021
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
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
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
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
00079
00080
00081 struct server_manager {
00082
00083
00084 enum CREATE_SERVER { MUST_CREATE_SERVER,
00085 TRY_CREATE_SERVER,
00086 NO_SERVER };
00087
00088
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
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 void enable_connection_through_proxy();
00111
00112
00113
00114
00115
00116
00117 void set_proxy_address ( const std::string& address );
00118
00119
00120
00121
00122
00123
00124 void set_proxy_port ( const std::string& port );
00125
00126
00127
00128
00129
00130
00131 void set_proxy_user ( const std::string& user );
00132
00133
00134
00135
00136
00137
00138 void set_proxy_password( const std::string& password );
00139
00140
00141
00142 size_t nconnections();
00143
00144
00145 bool is_server();
00146
00147
00148
00149
00150
00151
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
00159
00160
00161
00162
00163
00164
00165 connection accept_connection();
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 bool disconnect(connection connection_num=0);
00176
00177
00178
00179
00180
00181
00182
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
00211
00212
00213
00214
00215
00216
00217
00218
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
00228
00229
00230
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
00240
00241
00242
00243 void process_send_queue(connection connection_num=0, size_t max_size=0);
00244
00245
00246 void send_data_all_except(const config& cfg, connection connection_num,
00247 const std::string& packet_type = "unknown");
00248
00249
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 ;
00292 size_t current ;
00293 size_t current_max ;
00294 };
00295
00296
00297 statistics get_send_stats(connection handle);
00298 statistics get_receive_stats(connection handle);
00299
00300
00301 extern unsigned int ping_timeout;
00302
00303 const int ping_interval = 30;
00304 }
00305
00306
00307 #endif