3
|
1 /* $Id: ping.c,v 1.1.1.1 1997/12/06 05:41:29 darius Exp $ */
|
|
2
|
|
3 /*
|
|
4 * ping.c
|
|
5 *
|
|
6 */
|
|
7
|
|
8 #include "copyright2.h"
|
|
9 #include <stdio.h>
|
|
10 #include <sys/types.h>
|
|
11 #include <sys/time.h>
|
|
12 #include <netinet/in.h>
|
|
13 #include <math.h>
|
|
14 #include <errno.h>
|
|
15 #include "Wlib.h"
|
|
16 #include "defs.h"
|
|
17 #include "struct.h"
|
|
18 #include "data.h"
|
|
19 #include "packets.h"
|
|
20 #include "proto.h"
|
|
21
|
|
22 /* These are used only in pingstats.c */
|
|
23
|
|
24 int ping_iloss_sc = 0; /* inc % loss 0--100, server to client */
|
|
25 int ping_iloss_cs = 0; /* inc % loss 0--100, client to server */
|
|
26 int ping_tloss_sc = 0; /* total % loss 0--100, server to client */
|
|
27 int ping_tloss_cs = 0; /* total % loss 0--100, client to server */
|
|
28 int ping_lag = 0; /* delay in ms of last ping */
|
|
29 int ping_av = 0; /* rt time */
|
|
30 int ping_sd = 0; /* std deviation */
|
|
31
|
|
32 static int sum, n;
|
|
33 static int M, var;
|
|
34 static double s2;
|
|
35
|
|
36 void sendServerPingResponse P((int number));
|
|
37 void calc_lag P((void));
|
|
38
|
|
39 void
|
|
40 handlePing(packet) /* SP_PING */
|
|
41 struct ping_spacket *packet;
|
|
42 {
|
|
43 ping = 1; /* we got a ping */
|
|
44
|
|
45 /*
|
|
46 printf("ping received at %d (lag: %d)\n", msetime(), (int)packet->lag);
|
|
47 */
|
|
48 sendServerPingResponse((int) packet->number);
|
|
49 ping_lag = ntohs(packet->lag);
|
|
50 ping_iloss_sc = (int) packet->iloss_sc;
|
|
51 ping_iloss_cs = (int) packet->iloss_cs;
|
|
52 ping_tloss_sc = (int) packet->tloss_sc;
|
|
53 ping_tloss_cs = (int) packet->tloss_cs;
|
|
54
|
|
55 calc_lag();
|
|
56
|
|
57 if (W_IsMapped(pStats)) /* pstat window */
|
|
58 updatePStats();
|
|
59 }
|
|
60
|
|
61 void
|
|
62 startPing()
|
|
63 {
|
|
64 static
|
|
65 struct ping_cpacket packet;
|
|
66 extern int serverDead;
|
|
67
|
|
68 packet.type = CP_PING_RESPONSE;
|
|
69 packet.pingme = 1;
|
|
70
|
|
71 if (gwrite(sock, (char *) &packet, sizeof(struct ping_cpacket)) !=
|
|
72 sizeof(struct ping_cpacket)) {
|
|
73 printf("gwrite failed.\n");
|
|
74 serverDead = 1;
|
|
75 }
|
|
76 }
|
|
77
|
|
78 void
|
|
79 stopPing()
|
|
80 {
|
|
81 static
|
|
82 struct ping_cpacket packet;
|
|
83 extern int serverDead;
|
|
84
|
|
85 ping = 0;
|
|
86 packet.type = CP_PING_RESPONSE;
|
|
87 packet.pingme = 0;
|
|
88
|
|
89 if (gwrite(sock, (char *) &packet, sizeof(struct ping_cpacket)) !=
|
|
90 sizeof(struct ping_cpacket)) {
|
|
91 printf("gwrite failed.\n");
|
|
92 serverDead = 1;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 void
|
|
97 sendServerPingResponse(number) /* CP_PING_RESPONSE */
|
|
98 int number;
|
|
99 {
|
|
100 struct ping_cpacket packet;
|
|
101 int s;
|
|
102 extern int serverDead;
|
|
103
|
|
104 if (udpSock >= 0) {
|
|
105 s = udpSock;
|
|
106 packets_sent++;
|
|
107 } else
|
|
108 s = sock;
|
|
109
|
|
110 packet.type = CP_PING_RESPONSE;
|
|
111 packet.pingme = (char) ping;
|
|
112 packet.number = (unsigned char) number;
|
|
113 /* count this one */
|
|
114 packet.cp_sent = htonl(packets_sent);
|
|
115 packet.cp_recv = htonl(packets_received);
|
|
116
|
|
117 /*
|
|
118 printf("ping response sent at %d\n", msetime());
|
|
119 */
|
|
120
|
|
121 if (gwrite(s, (char *) &packet, sizeof(struct ping_cpacket)) !=
|
|
122 sizeof(struct ping_cpacket)) {
|
|
123 printf("gwrite failed.\n");
|
|
124 serverDead = 1;
|
|
125 }
|
|
126 }
|
|
127
|
|
128 void
|
|
129 calc_lag()
|
|
130 {
|
|
131 /* probably ghostbusted */
|
|
132 /* without this things can get really bad */
|
|
133 if (ping_lag > 2000 || ping_lag == 0)
|
|
134 return;
|
|
135
|
|
136 n++;
|
|
137 sum += ping_lag;
|
|
138 s2 += (ping_lag * ping_lag);
|
|
139 if (n == 1)
|
|
140 return;
|
|
141
|
|
142 M = sum / n;
|
|
143 var = (s2 - M * sum) / (n - 1);
|
|
144
|
|
145 ping_av = M;
|
|
146 ping_sd = (int) sqrt((double) var);
|
|
147 }
|