2 * $Logfile: /Freespace2/code/Network/multi_rate.cpp $
8 * Revision 1.2 2002/05/07 03:16:47 theoddone33
9 * The Great Newline Fix
11 * Revision 1.1.1.1 2002/05/03 03:28:10 root
15 * 6 7/15/99 9:20a Andsager
16 * FS2_DEMO initial checkin
18 * 5 4/25/99 3:02p Dave
19 * Build defines for the E3 build.
21 * 4 4/09/99 2:21p Dave
22 * Multiplayer beta stuff. CD checking.
24 * 3 3/20/99 5:09p Dave
25 * Fixed release build fred warnings and unhandled exception.
27 * 2 3/09/99 6:24p Dave
28 * More work on object update revamping. Identified several sources of
29 * unnecessary bandwidth.
37 #include "alphacolors.h"
38 #include "multi_rate.h"
40 // -----------------------------------------------------------------------------------------------------------------------
41 // MULTI RATE DEFINES/VARS
44 // keep this defined to compile in rate checking
45 #if !defined(NDEBUG) || defined(MULTIPLAYER_BETA_BUILD) || defined(FS2_DEMO)
49 // how many records in the past we'll keep track of
50 #define NUM_UPDATE_RECORDS 5
52 // rate monitoring info
53 typedef struct mr_info {
55 char type[MAX_RATE_TYPE_LEN+1];
58 int total_bytes; // total bytes alltime
61 int stamp_second; // stamp for one second
62 int bytes_second; // how many bytes we've sent in the last second
63 int records_second[NUM_UPDATE_RECORDS]; // records
64 int records_second_count; // how many records we have
65 float avg_second; // avg bytes/sec
68 int bytes_frame; // how many bytes we've sent this frame
69 int records_frame[NUM_UPDATE_RECORDS]; // records
70 int records_frame_count; // how many records we have
71 float avg_frame; // avg bytes/frame
76 mr_info Multi_rate[MAX_RATE_PLAYERS][MAX_RATE_TYPES];
80 // -----------------------------------------------------------------------------------------------------------------------
81 // MULTI RATE FUNCTIONS
84 // notify of a player join
85 void multi_rate_reset(int np_index)
94 if((np_index < 0) || (np_index >= MAX_RATE_PLAYERS)){
98 // blast the index clear
99 for(idx=0; idx<MAX_RATE_TYPES; idx++){
100 memset(&Multi_rate[np_index][idx], 0, sizeof(mr_info));
101 Multi_rate[np_index][idx].stamp_second = -1;
106 // add data of the specified type to datarate processing, returns 0 on fail (if we ran out of types, etc, etc)
107 int multi_rate_add(int np_index, char *type, int size)
116 if((np_index < 0) || (np_index >= MAX_RATE_PLAYERS)){
119 if((type == NULL) || (strlen(type) <= 0)){
123 // see if the type already exists
124 for(idx=0; idx<MAX_RATE_TYPES; idx++){
126 if(strlen(Multi_rate[np_index][idx].type) <= 0){
130 else if(!stricmp(Multi_rate[np_index][idx].type, type)){
135 // if we couldn't find a slot
136 if(idx >= MAX_RATE_TYPES){
140 // otherwise add the data
141 m = &Multi_rate[np_index][idx];
144 strcpy(m->type, type);
147 m->total_bytes += size;
150 m->bytes_second += size;
153 m->bytes_frame += size;
161 #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)
162 void multi_rate_process()
172 // process all active players
173 for(idx=0; idx<MAX_RATE_PLAYERS; idx++){
174 for(s_idx=0; s_idx<MAX_RATE_TYPES; s_idx++){
175 m = &Multi_rate[idx][s_idx];
178 if(strlen(m->type) <= 0){
183 if(m->stamp_second == -1){
184 m->stamp_second = timestamp(1000);
185 } else if(timestamp_elapsed(m->stamp_second)){
186 // if we've reached max records
187 if(m->records_second_count >= NUM_UPDATE_RECORDS){
188 memmove(m->records_second, m->records_second+1, sizeof(int) * (NUM_UPDATE_RECORDS - 1));
189 m->records_second[NUM_UPDATE_RECORDS-1] = m->bytes_second;
191 // haven't reached max records
193 m->records_second[m->records_second_count++] = m->bytes_second;
196 // recalculate the average
197 R_AVG(m->records_second_count, m->records_second, m->avg_second);
199 // reset bytes/second and timestamp
201 m->stamp_second = timestamp(1000);
205 // if we've reached max records
206 if(m->records_frame_count >= NUM_UPDATE_RECORDS){
207 memmove(m->records_frame, m->records_frame+1, sizeof(int) * (NUM_UPDATE_RECORDS - 1));
208 m->records_frame[NUM_UPDATE_RECORDS-1] = m->bytes_frame;
210 // haven't reached max records
212 m->records_frame[m->records_frame_count++] = m->bytes_frame;
215 // recalculate the average
216 R_AVG(m->records_frame_count, m->records_frame, m->avg_frame);
226 void multi_rate_display(int np_index, int x, int y)
237 if((np_index < 0) || (np_index >= MAX_RATE_PLAYERS)){
242 for(idx=0; idx<MAX_RATE_TYPES; idx++){
243 m = &Multi_rate[np_index][idx];
245 // if we have a 0 length string, we're done
246 if(strlen(m->type) <= 0){
251 gr_set_color_fast(&Color_red);
252 gr_printf(x, y, "%s %d (%d/s) (%f/f)", m->type, m->total_bytes, (int)m->avg_second, m->avg_frame);