Allow interval and timeout to be set on command line.

This commit is contained in:
Matthieu Herrb 2012-07-31 11:24:50 +02:00
parent 421fdadd13
commit 1bc5c2ff5d

29
echoc.c
View file

@ -43,7 +43,7 @@ struct timeval sent;
static void static void
usage(void) usage(void)
{ {
errx(2, "usage: echoc [-v] server"); errx(2, "usage: echoc [-i ms][-t ms][-v] server");
} }
static void static void
@ -70,14 +70,22 @@ main(int argc, char *argv[])
struct tm *tm; struct tm *tm;
struct pollfd pfd[1]; struct pollfd pfd[1];
socklen_t addrlen; socklen_t addrlen;
long interval = 100; /* default interval (ms) */
long timeout = 500; /* default timeout (ms) */
int ch; int ch;
int nfds, received = 0; int nfds, received = 0;
int error, buffer, last = -1; int error, buffer, last = -1;
extern int optind; extern int optind;
setbuf(stdout, NULL); setbuf(stdout, NULL);
while ((ch = getopt(argc, argv, "v")) != -1) { while ((ch = getopt(argc, argv, "i:t:v")) != -1) {
switch (ch) { switch (ch) {
case 'i':
interval = atoi(optarg);
break;
case 't':
timeout = atoi(optarg);
break;
case 'v': case 'v':
verbose++; verbose++;
break; break;
@ -90,6 +98,15 @@ main(int argc, char *argv[])
if (argc != 1) if (argc != 1)
usage(); usage();
if (interval <= 0 || timeout <= 0)
errx(2, "interval and timeout must be > 0");
/* force timeout >= interval */
if (timeout < interval) {
timeout = interval;
warnx("adjusting timeout to %ld ms "
"(must be greater than interval)", timeout);
}
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
@ -113,9 +130,9 @@ main(int argc, char *argv[])
memset(&client, 0, sizeof(client)); memset(&client, 0, sizeof(client));
/* timer values */ /* timer values */
itv.it_interval.tv_usec = 100000; /* 100ms */ itv.it_interval.tv_usec = interval*1000;
itv.it_interval.tv_sec = 0; itv.it_interval.tv_sec = 0;
itv.it_value.tv_usec = 100000; itv.it_value.tv_usec = interval*1000;
itv.it_value.tv_sec = 0; itv.it_value.tv_sec = 0;
if (setitimer(ITIMER_REAL, &itv, NULL) == -1) if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
err(2, "setitimer"); err(2, "setitimer");
@ -132,14 +149,14 @@ main(int argc, char *argv[])
while (1) { while (1) {
pfd[0].fd = sock; pfd[0].fd = sock;
pfd[0].events = POLLIN; pfd[0].events = POLLIN;
nfds = poll(pfd, 1, 200); nfds = poll(pfd, 1, timeout);
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
timersub(&now, &sent, &diff); timersub(&now, &sent, &diff);
if (nfds > 0) if (nfds > 0)
break; break;
if (nfds == -1 && errno != EINTR) if (nfds == -1 && errno != EINTR)
warn("poll error"); warn("poll error");
if (diff.tv_sec > 0 || diff.tv_usec > 500000) { if (diff.tv_sec > 0 || diff.tv_usec > timeout*1000) {
disconnected++; disconnected++;
nfds = 0; nfds = 0;
break; break;