diff --git a/src/measure.c b/src/measure.c index 864e687..060f58d 100644 --- a/src/measure.c +++ b/src/measure.c @@ -19,7 +19,7 @@ void measure_parse(int size, struct measure_conf* mc) { micro_sec = elapsed_micros (&head->emit_time, &curr); printf("[%s] Packet %llu latency %luµs\n", ctime_no_newline, (unsigned long long)head->counter, micro_sec); - if (mc->max_measure > 0 && head->counter >= mc->max_measure) { + if (!mc->inhibit_exit && head->counter >= mc->max_measure) { printf("Measurement done\n"); exit(EXIT_SUCCESS); } diff --git a/src/measure.h b/src/measure.h index 1f97f77..51ff873 100644 --- a/src/measure.h +++ b/src/measure.h @@ -11,6 +11,7 @@ struct measure_conf { uint64_t interval; char* payload; uint64_t counter; + uint8_t inhibit_exit; }; struct measure_packet { diff --git a/src/udp_echo.c b/src/udp_echo.c index df887cc..9294f41 100644 --- a/src/udp_echo.c +++ b/src/udp_echo.c @@ -4,22 +4,30 @@ #include #include "evt_core.h" #include "net_tools.h" +#include "measure.h" + +struct udpecho_ctx { + struct measure_conf mc; + uint8_t is_measlat; +}; int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { - char buffer[1500]; ssize_t nread, nwritten; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); + struct udpecho_ctx *uctx = fdinfo->cat->app_ctx; - nread = recvfrom(fdinfo->fd, buffer, sizeof(buffer), MSG_TRUNC, (struct sockaddr*)&addr, &addrlen); + nread = recvfrom(fdinfo->fd, uctx->mc.payload, uctx->mc.payload_size, MSG_TRUNC, (struct sockaddr*)&addr, &addrlen); if (nread == -1 && errno == EAGAIN) return 1; // Read done - if (nread <= 0 || nread > sizeof(buffer)) { - fprintf(stderr, "Message is either truncated or an error occured. nread=%ld\n", nread); + if (nread <= 0 || nread > uctx->mc.payload_size) { + fprintf(stderr, "Message is either truncated or an error occured. nread=%ld, expected=%ld\n", nread, uctx->mc.payload_size); perror("read errno"); exit(EXIT_FAILURE); } - nwritten = sendto(fdinfo->fd, buffer, nread, 0, (struct sockaddr*)&addr, addrlen); + if (uctx->is_measlat) measure_parse (nread, &uctx->mc); + + nwritten = sendto(fdinfo->fd, uctx->mc.payload, nread, 0, (struct sockaddr*)&addr, addrlen); // @FIXME don't support EAGAIN on write. Could be intended, you don't think so? if (nwritten != nread) { fprintf(stderr, "Didn't write the same number of bytes as read. nread=%ld, nwritten=%ld\n", nread, nwritten); @@ -33,12 +41,15 @@ int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ udpecho ~\n"); + struct udpecho_ctx uctx = {0}; int opt, udp_sock, verbose = 0; char *port = NULL, *bindhost = NULL; struct evt_core_ctx evts = {0}; + uctx.mc.inhibit_exit = 1; + uctx.mc.payload_size = 1500; // 1. Parse parameters - while ((opt = getopt(argc, argv, "b:p:v")) != -1) { + while ((opt = getopt(argc, argv, "b:p:vms:")) != -1) { switch(opt) { case 'v': verbose++; @@ -49,15 +60,22 @@ int main(int argc, char** argv) { case 'b': bindhost = optarg; break; + case 'm': + uctx.is_measlat = 1; + break; + case 's': + uctx.mc.payload_size = atoi(optarg); + break; default: goto usage; } } if (bindhost == NULL) bindhost = "127.0.0.1"; + measure_prepare (&uctx.mc); // 2. Register category struct evt_core_cat udp_read = { - .app_ctx = NULL, + .app_ctx = &uctx, .free_app_ctx = NULL, .cb = on_udp, .err_cb = NULL,