72 lines
2.0 KiB
C
72 lines
2.0 KiB
C
#include <stdio.h>
|
|
|
|
#include "ping_pong.h"
|
|
#include "timespec.h"
|
|
#include "packet.h"
|
|
|
|
int handle_ping_pong(int sockfd,
|
|
struct sockaddr_in6 * dest_addr,
|
|
void * buf, ssize_t length,
|
|
struct link_state * link_state
|
|
)
|
|
{
|
|
struct event * evt = (struct event *)buf;
|
|
switch (evt->type) {
|
|
case EVENT_TYPE__PING:
|
|
{
|
|
if (length != (sizeof (struct packet_ping_pong))) {
|
|
printf("handle_ping_pong: ping: invalid length\n");
|
|
return 0; // recoverable
|
|
}
|
|
|
|
printf("recv ping; send pong\n");
|
|
struct packet_ping_pong * ping_pkt = (struct packet_ping_pong *)buf;
|
|
int ret = packet_send_pong(sockfd, dest_addr, &ping_pkt->time);
|
|
if (ret == -1) {
|
|
return -1;
|
|
}
|
|
}
|
|
break;
|
|
case EVENT_TYPE__PONG:
|
|
{
|
|
if (length != (sizeof (struct packet_ping_pong))) {
|
|
printf("handle_ping_pong: pong: invalid length\n");
|
|
return 0; // recoverable
|
|
}
|
|
|
|
printf("recv pong\n");
|
|
struct packet_ping_pong * ping_pkt = (struct packet_ping_pong *)buf;
|
|
struct timespec now;
|
|
int ret = clock_gettime(CLOCK_MONOTONIC_RAW, &now);
|
|
assert(ret != -1);
|
|
link_state->last_pong = now;
|
|
|
|
struct timespec * rtt = &link_state->ping_pong_rtt[link_state->rtt_ix];
|
|
link_state->rtt_ix = (link_state->rtt_ix + 1) % RTT_AVERAGE_SAMPLES;
|
|
*rtt = diff_timespec(&now, &ping_pkt->time);
|
|
printf("rtt: %ld.%09ld\n", rtt->tv_sec, rtt->tv_nsec);
|
|
struct timespec average_rtt = average_timespec(link_state->ping_pong_rtt, RTT_AVERAGE_SAMPLES);
|
|
|
|
printf("send_average_rtt\n");
|
|
ret = packet_send_average_rtt(sockfd, dest_addr, &average_rtt);
|
|
if (ret == -1) {
|
|
return -1;
|
|
}
|
|
}
|
|
break;
|
|
case EVENT_TYPE__AVERAGE_RTT:
|
|
{
|
|
if (length != (sizeof (struct packet_ping_pong))) {
|
|
printf("handle_ping_pong: average_rtt: invalid length\n");
|
|
return 0; // recoverable
|
|
}
|
|
|
|
printf("recv average_rtt\n");
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|