# HG changeset patch # User jbe # Date 1428372457 -7200 # Node ID 111b2469b7536d9d46e0c336f3275ce2916b01fa # Parent 6b26783f93239478b5c1184396576148769dc058 Always re-fill immediate send buffer in write_nb method diff -r 6b26783f9323 -r 111b2469b753 moonbridge_io.c --- a/moonbridge_io.c Tue Apr 07 03:50:28 2015 +0200 +++ b/moonbridge_io.c Tue Apr 07 04:07:37 2015 +0200 @@ -178,7 +178,7 @@ moonbr_io_handle_t *handle; int i, top; const char *str; - size_t strlen, strpos; + size_t strlen; size_t written; ssize_t result; handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY); @@ -202,16 +202,15 @@ while (handle->writeqout != handle->writeqin) { lua_rawgeti(L, -1, handle->writeqout); str = lua_tolstring(L, -1, &strlen); - strpos = handle->writeqoff; - while (strpos < strlen) { - if (strlen - strpos < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) { - memcpy(handle->writebuf + handle->writebufcnt, str + strpos, strlen - strpos); - handle->writebufcnt += strlen - strpos; + while (handle->writeqoff < strlen) { + if (strlen - handle->writeqoff < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) { + memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, strlen - handle->writeqoff); + handle->writebufcnt += strlen - handle->writeqoff; break; } else { written = 0; - memcpy(handle->writebuf + handle->writebufcnt, str + strpos, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt); - strpos += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt; + memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt); + handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt; while (written < MOONBR_IO_WRITEBUFLEN) { result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written); if (result < 0) { @@ -219,8 +218,8 @@ if (written) { handle->writebufcnt -= written; memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt); + goto moonbr_io_write_impl_continue; } - handle->writeqoff = strpos; goto moonbr_io_write_impl_block; } else if (errno != EINTR) { moonbr_io_errmsg(); @@ -229,13 +228,13 @@ lua_pushstring(L, errmsg); return 2; } - } else { - written += result; - handle->writeleft -= result; } + written += result; + handle->writeleft -= result; } handle->writebufcnt = 0; } + moonbr_io_write_impl_continue:; } handle->writeqoff = 0; lua_pop(L, 1);