From c75f08ec81829f3bb978e8cdca8e11a6aa67d028 Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Thu, 28 Mar 2019 08:13:23 +0100 Subject: [PATCH 1/2] Don't send MIDI flow control messages too often. --- sndiod/midi.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sndiod/midi.c b/sndiod/midi.c index b879f7d..b5cc4e8 100644 --- a/sndiod/midi.c +++ b/sndiod/midi.c @@ -239,6 +239,16 @@ midi_tickets(struct midi *iep) int i, tickets, avail, maxavail; struct midi *oep; + /* + * don't request iep->ops->fill() too often as it generates + * useless network traffic: wait until we reach half of the + * max tickets count. As in the worst case (see comment below) + * one ticket may consume two bytes, the max ticket count is + * BUFSZ / 2 and halt of it is simply BUFSZ / 4. + */ + if (iep->tickets >= MIDI_BUFSZ / 4) + return; + maxavail = MIDI_BUFSZ; for (i = 0; i < MIDI_NEP ; i++) { if ((iep->txmask & (1 << i)) == 0) From 6ac10f167bfd355393b6ea9a74ca9e6baec461b9 Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Thu, 28 Mar 2019 08:13:59 +0100 Subject: [PATCH 2/2] Wait until the server disconnects before closing the socket. When the socket is closed, the last bytes written may be lost. --- libsndio/aucat.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/libsndio/aucat.c b/libsndio/aucat.c index 48a8abb..2edc129 100644 --- a/libsndio/aucat.c +++ b/libsndio/aucat.c @@ -574,7 +574,8 @@ _aucat_open(struct aucat *hdl, const char *str, unsigned int mode) void _aucat_close(struct aucat *hdl, int eof) { - char dummy[1]; + char dummy[sizeof(struct amsg)]; + ssize_t n; if (!eof) { AMSG_INIT(&hdl->wmsg); @@ -582,8 +583,20 @@ _aucat_close(struct aucat *hdl, int eof) hdl->wtodo = sizeof(struct amsg); if (!_aucat_wmsg(hdl, &eof)) goto bad_close; - while (read(hdl->fd, dummy, 1) < 0 && errno == EINTR) - ; /* nothing */ + + /* + * block until the peer disconnects + */ + while (1) { + n = read(hdl->fd, dummy, sizeof(dummy)); + if (n < 0) { + if (errno == EINTR) + continue; + break; + } + if (n == 0) + break; + } } bad_close: while (close(hdl->fd) < 0 && errno == EINTR)