FS2_Open
Open source remastering of the Freespace 2 engine
multi_ping.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 
13 #include "network/multi.h"
14 #include "network/multi_ping.h"
15 #include "network/multimsgs.h"
16 #include "io/timer.h"
17 
18 
19 
20 // ------------------------------------------------------------------------------------
21 // MULTIPLAYER PING DEFINES/VARS
22 //
23 
24 
25 // ------------------------------------------------------------------------------------
26 // MULTIPLAYER PING FUNCTIONS
27 //
28 
29 // initialize all player ping times
31 {
32  int idx;
33 
34  // reset the pings for all players
35  for(idx=0;idx<MAX_PLAYERS;idx++){
37  }
38 }
39 
40 // initialize the given ping struct
42 {
43  // blast the struct clear
44  memset(ps,0,sizeof(ping_struct));
45 
46  ps->ping_add = 0;
47 
48  // set the ping start to be -1
49  ps->ping_start = -1.0f;
50 
51  ps->ping_avg = -1;
52 
53  ps->num_pings = 0;
54 }
55 
56 // evaluate a pong return on the given struct
58 {
59  int idx;
60  float ping_sum;
61 
62  // if the ping technically hasn't started,
63  if(ps->ping_start < 0.0f){
64  nprintf(("Network","Processing pong for ping which hasn't started yet!\n"));
65  return;
66  }
67 
68  // if we still have room to add a ping
69  if(ps->num_pings < MAX_PINGS){
71  ps->num_pings++;
72  }
73  // otherwise if we've wrapped around
74  else {
75  // increment the place to add the ping time
76  if(ps->ping_add >= MAX_PINGS - 1){
77  ps->ping_add = 0;
78  } else {
79  ps->ping_add++;
80  }
81 
83  }
84 
85  // calculate the average ping time
86  ping_sum = 0.0f;
87  for(idx=0;idx<ps->num_pings;idx++){
88  ping_sum += ps->ping_times[idx];
89  }
90  ps->ping_avg = (int)(1000.0f * (ping_sum / (float)ps->num_pings));
91 }
92 
93 // start a ping - call this when sending a ping packet
95 {
97 }
98 
99 // send a ping to a specific player
101 {
103  send_ping(&p->p_info.addr);
104 }
105 
106 // send a ping to the specified address
108 {
109  multi_ping_start(ps);
110  send_ping(addr);
111 }
112 
113 // send a ping to all players
115 {
116  int idx;
117  for(idx = 0;idx < MAX_PLAYERS;idx++){
118  if(MULTI_CONNECTED(Net_players[idx]) && (Net_player != NULL) && (Net_player->player_id != Net_players[idx].player_id)){
119  // start the ping
121 
122  // send the ping packet
124  }
125  }
126 }
void multi_ping_start(ping_struct *ps)
Definition: multi_ping.cpp:94
net_player * Net_player
Definition: multi.cpp:94
void multi_ping_send_all()
Definition: multi_ping.cpp:114
GLclampf f
Definition: Glext.h:7097
void multi_ping_send(net_player *p)
Definition: multi_ping.cpp:100
float ping_times[MAX_PINGS]
Definition: multi_ping.h:28
#define f2fl(fx)
Definition: floating.h:37
void multi_ping_reset(ping_struct *ps)
Definition: multi_ping.cpp:41
void send_ping(net_addr *addr)
Definition: multimsgs.cpp:3527
void multi_ping_reset_players()
Definition: multi_ping.cpp:30
typedef int(SCP_EXT_CALLCONV *SCPDLL_PFVERSION)(SCPDLL_Version *)
net_player_info p_info
Definition: multi.h:473
int ping_avg
Definition: multi_ping.h:32
#define nprintf(args)
Definition: pstypes.h:239
float ping_start
Definition: multi_ping.h:27
net_player_server_info s_info
Definition: multi.h:472
#define MAX_PLAYERS
Definition: pstypes.h:32
short player_id
Definition: multi.h:460
int ping_add
Definition: multi_ping.h:30
int idx
Definition: multiui.cpp:761
net_addr addr
Definition: multi.h:453
int num_pings
Definition: multi_ping.h:29
ping_struct ping
Definition: multi.h:395
#define MULTI_CONNECTED(np)
Definition: multi.h:136
fix timer_get_fixed_seconds()
Definition: timer.cpp:116
GLfloat GLfloat p
Definition: Glext.h:8373
#define MAX_PINGS
Definition: multi.h:106
void multi_ping_eval_pong(ping_struct *ps)
Definition: multi_ping.cpp:57
GLenum const GLvoid * addr
Definition: Glext.h:9092
net_player Net_players[MAX_PLAYERS]
Definition: multi.cpp:93