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 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
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
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
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
00069
00070 struct server_manager {
00071
00072
00073 enum CREATE_SERVER { MUST_CREATE_SERVER,
00074 TRY_CREATE_SERVER,
00075 NO_SERVER };
00076
00077
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
00093 size_t nconnections();
00094
00095
00096 bool is_server();
00097
00098
00099
00100
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
00106
00107
00108
00109 connection accept_connection();
00110
00111
00112
00113
00114
00115
00116
00117 bool disconnect(connection connection_num=0, bool force=false);
00118
00119
00120
00121
00122
00123 void queue_disconnect(connection connection_num);
00124
00125
00126
00127
00128
00129
00130
00131
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
00137
00138
00139 void send_data(const config& cfg, connection connection_num , const bool gzipped);
00140
00141 void send_raw_data(const char* buf, int len, connection connection_num);
00142
00143
00144
00145
00146 void process_send_queue(connection connection_num=0, size_t max_size=0);
00147
00148
00149 void send_data_all_except(const config& cfg, connection connection_num, const bool gzipped);
00150
00151
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
00200 statistics get_send_stats(connection handle);
00201 statistics get_receive_stats(connection handle);
00202
00203
00204 extern unsigned int ping_timeout;
00205 }
00206
00207
00208 #endif