From f561c4b9e1ea8a225ae4431348529cd5b6b477d9 Mon Sep 17 00:00:00 2001 From: Alec Leamas Date: Tue, 23 Aug 2016 13:16:56 +0200 Subject: [PATCH 08/10] lircd: Retry temporary unavailable write sockets (#221). --- daemons/lircd.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/daemons/lircd.cpp b/daemons/lircd.cpp index de2c341..2dc9029 100644 --- a/daemons/lircd.cpp +++ b/daemons/lircd.cpp @@ -115,6 +115,12 @@ int clock_gettime(int clk_id, struct timespec *t){ static const logchannel_t logchannel = LOG_APP; +/** How long we sleep while waiting for busy write sockets. */ +static const int WRITE_SLEEP_US = 20000; + +/** How many times we retry busy write sockets. */ +static const int WRITE_RETRIES = 50; + struct peer_connection { char* host; unsigned short port; @@ -338,11 +344,22 @@ static int oatoi(const char* s) int write_socket(int fd, const char* buf, int len) { int done, todo = len; + int retries = WRITE_RETRIES; while (todo) { done = write(fd, buf, todo); - if (done <= 0) - return done; + if (done <= 0) { + log_perror_debug("Error in write_socket"); + if (errno == EAGAIN || errno == EWOULDBLOCK) { + retries -= 1; + if (retries <= 0) + return done; + usleep(WRITE_SLEEP_US); + continue; + } else { + return done; + } + } buf += done; todo -= done; } -- 2.5.5