2 * $Logfile: /Freespace2/code/Network/multi_rate.cpp $
8 * Revision 1.1 2002/05/03 03:28:10 root
12 * 6 7/15/99 9:20a Andsager
13 * FS2_DEMO initial checkin
15 * 5 4/25/99 3:02p Dave
16 * Build defines for the E3 build.
18 * 4 4/09/99 2:21p Dave
19 * Multiplayer beta stuff. CD checking.
21 * 3 3/20/99 5:09p Dave
22 * Fixed release build fred warnings and unhandled exception.
24 * 2 3/09/99 6:24p Dave
25 * More work on object update revamping. Identified several sources of
26 * unnecessary bandwidth.
34 #include "alphacolors.h"
35 #include "multi_rate.h"
37 // -----------------------------------------------------------------------------------------------------------------------
38 // MULTI RATE DEFINES/VARS
41 // keep this defined to compile in rate checking
42 #if !defined(NDEBUG) || defined(MULTIPLAYER_BETA_BUILD) || defined(FS2_DEMO)
46 // how many records in the past we'll keep track of
47 #define NUM_UPDATE_RECORDS 5
49 // rate monitoring info
50 typedef struct mr_info {
52 char type[MAX_RATE_TYPE_LEN+1];
55 int total_bytes; // total bytes alltime
58 int stamp_second; // stamp for one second
59 int bytes_second; // how many bytes we've sent in the last second
60 int records_second[NUM_UPDATE_RECORDS]; // records
61 int records_second_count; // how many records we have
62 float avg_second; // avg bytes/sec
65 int bytes_frame; // how many bytes we've sent this frame
66 int records_frame[NUM_UPDATE_RECORDS]; // records
67 int records_frame_count; // how many records we have
68 float avg_frame; // avg bytes/frame
73 mr_info Multi_rate[MAX_RATE_PLAYERS][MAX_RATE_TYPES];
77 // -----------------------------------------------------------------------------------------------------------------------
78 // MULTI RATE FUNCTIONS
81 // notify of a player join
82 void multi_rate_reset(int np_index)
91 if((np_index < 0) || (np_index >= MAX_RATE_PLAYERS)){
95 // blast the index clear
96 for(idx=0; idx<MAX_RATE_TYPES; idx++){
97 memset(&Multi_rate[np_index][idx], 0, sizeof(mr_info));
98 Multi_rate[np_index][idx].stamp_second = -1;
103 // add data of the specified type to datarate processing, returns 0 on fail (if we ran out of types, etc, etc)
104 int multi_rate_add(int np_index, char *type, int size)
113 if((np_index < 0) || (np_index >= MAX_RATE_PLAYERS)){
116 if((type == NULL) || (strlen(type) <= 0)){
120 // see if the type already exists
121 for(idx=0; idx<MAX_RATE_TYPES; idx++){
123 if(strlen(Multi_rate[np_index][idx].type) <= 0){
127 else if(!stricmp(Multi_rate[np_index][idx].type, type)){
132 // if we couldn't find a slot
133 if(idx >= MAX_RATE_TYPES){
137 // otherwise add the data
138 m = &Multi_rate[np_index][idx];
141 strcpy(m->type, type);
144 m->total_bytes += size;
147 m->bytes_second += size;
150 m->bytes_frame += size;
158 #define R_AVG(ct, ar, avg) do {int av_idx; float av_sum = 0.0f; if(ct == 0){ avg = 0;} else { for(av_idx=0; av_idx<ct; av_idx++){ av_sum += (float)ar[av_idx]; } avg = av_sum / (float)ct; } }while(0)
159 void multi_rate_process()
169 // process all active players
170 for(idx=0; idx<MAX_RATE_PLAYERS; idx++){
171 for(s_idx=0; s_idx<MAX_RATE_TYPES; s_idx++){
172 m = &Multi_rate[idx][s_idx];
175 if(strlen(m->type) <= 0){
180 if(m->stamp_second == -1){
181 m->stamp_second = timestamp(1000);
182 } else if(timestamp_elapsed(m->stamp_second)){
183 // if we've reached max records
184 if(m->records_second_count >= NUM_UPDATE_RECORDS){
185 memmove(m->records_second, m->records_second+1, sizeof(int) * (NUM_UPDATE_RECORDS - 1));
186 m->records_second[NUM_UPDATE_RECORDS-1] = m->bytes_second;
188 // haven't reached max records
190 m->records_second[m->records_second_count++] = m->bytes_second;
193 // recalculate the average
194 R_AVG(m->records_second_count, m->records_second, m->avg_second);
196 // reset bytes/second and timestamp
198 m->stamp_second = timestamp(1000);
202 // if we've reached max records
203 if(m->records_frame_count >= NUM_UPDATE_RECORDS){
204 memmove(m->records_frame, m->records_frame+1, sizeof(int) * (NUM_UPDATE_RECORDS - 1));
205 m->records_frame[NUM_UPDATE_RECORDS-1] = m->bytes_frame;
207 // haven't reached max records
209 m->records_frame[m->records_frame_count++] = m->bytes_frame;
212 // recalculate the average
213 R_AVG(m->records_frame_count, m->records_frame, m->avg_frame);
223 void multi_rate_display(int np_index, int x, int y)
234 if((np_index < 0) || (np_index >= MAX_RATE_PLAYERS)){
239 for(idx=0; idx<MAX_RATE_TYPES; idx++){
240 m = &Multi_rate[np_index][idx];
242 // if we have a 0 length string, we're done
243 if(strlen(m->type) <= 0){
248 gr_set_color_fast(&Color_red);
249 gr_printf(x, y, "%s %d (%d/s) (%f/f)", m->type, m->total_bytes, (int)m->avg_second, m->avg_frame);