diff options
author | Nikias Bassen | 2018-05-15 00:27:52 +0200 |
---|---|---|
committer | Nikias Bassen | 2018-05-15 00:27:52 +0200 |
commit | 08d9ec01cf59c7bb3febe3c4600e9efeb81901e3 (patch) | |
tree | 8b09520d8b31c5153c9a458d1a596264c21cdf3c /src/device.c | |
parent | ca6f4718deb56367bbae33312e0f341e0595f3eb (diff) | |
download | usbmuxd-08d9ec01cf59c7bb3febe3c4600e9efeb81901e3.tar.gz usbmuxd-08d9ec01cf59c7bb3febe3c4600e9efeb81901e3.tar.bz2 |
device: Flush buffer to client when remote side unexpectedly terminates connection
Diffstat (limited to 'src/device.c')
-rw-r--r-- | src/device.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/device.c b/src/device.c index d5cf9f0..3edaea0 100644 --- a/src/device.c +++ b/src/device.c @@ -32,6 +32,7 @@ #include <stdint.h> #include <inttypes.h> #include <pthread.h> +#include <unistd.h> #include "device.h" #include "client.h" #include "preflight.h" @@ -314,10 +315,21 @@ static void connection_teardown(struct mux_connection *conn) } else { conn->state = CONN_DEAD; if((conn->events & POLLOUT) && conn->ib_size > 0){ + usbmuxd_log(LL_DEBUG, "%s: flushing buffer to client (%u bytes)", __func__, conn->ib_size); + uint64_t tm_last = mstime64(); while(1){ size = client_write(conn->client, conn->ib_buf, conn->ib_size); - if(size <= 0) { + if(size < 0) { + usbmuxd_log(LL_ERROR, "%s: aborting buffer flush to client after error.", __func__); break; + } else if (size == 0) { + uint64_t tm_now = mstime64(); + if (tm_now - tm_last > 1000) { + usbmuxd_log(LL_ERROR, "%s: aborting buffer flush to client after unsuccessfully attempting for %dms.", __func__, (int)(tm_now - tm_last)); + break; + } + usleep(10000); + continue; } if(size == (int)conn->ib_size) { conn->ib_size = 0; @@ -326,6 +338,7 @@ static void connection_teardown(struct mux_connection *conn) conn->ib_size -= size; memmove(conn->ib_buf, conn->ib_buf + size, conn->ib_size); } + tm_last = mstime64(); } } client_close(conn->client); |