moonbridge

changeset 132:293e666a41e0

Revised flushing behavior of write_nb(...)
author jbe
date Thu Apr 16 04:24:21 2015 +0200 (2015-04-16)
parents 89fc89b22e28
children 891cdc4b43eb
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Wed Apr 15 00:59:52 2015 +0200
     1.2 +++ b/moonbridge_io.c	Thu Apr 16 04:24:21 2015 +0200
     1.3 @@ -48,6 +48,7 @@
     1.4    int readbufout;
     1.5    int writeerr;
     1.6    size_t writeleft;
     1.7 +  size_t flushedleft;
     1.8  #if LUA_VERSION_NUM >= 503
     1.9    lua_Integer writeqin;
    1.10    lua_Integer writeqout;
    1.11 @@ -287,12 +288,14 @@
    1.12      lua_rawseti(L, -2, handle->writeqin++);
    1.13      handle->writeleft += strlen;
    1.14    }
    1.15 +  if (flush) handle->flushedleft = handle->writeleft;
    1.16 +  if (handle->flushedleft) moonbr_io_handle_set_nopush(L, handle, 0);
    1.17    while (handle->writeqout != handle->writeqin) {
    1.18      lua_rawgeti(L, -1, handle->writeqout);
    1.19      str = lua_tolstring(L, -1, &strlen);
    1.20      while (handle->writeqoff < strlen) {
    1.21        if (
    1.22 -        strlen - handle->writeqoff <=
    1.23 +        strlen - handle->writeqoff <
    1.24          MOONBR_IO_WRITEBUFLEN - handle->writebufin
    1.25        ) {
    1.26          memcpy(
    1.27 @@ -303,7 +306,6 @@
    1.28          handle->writebufin += strlen - handle->writeqoff;
    1.29          break;
    1.30        } else {
    1.31 -        moonbr_io_handle_set_nopush(L, handle, 1);
    1.32          memcpy(
    1.33            handle->writebuf + handle->writebufin,
    1.34            str + handle->writeqoff,
    1.35 @@ -311,6 +313,7 @@
    1.36          );
    1.37          handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
    1.38          while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
    1.39 +          if (!handle->flushedleft) moonbr_io_handle_set_nopush(L, handle, 1);
    1.40            written = write(
    1.41              handle->fd,
    1.42              handle->writebuf + handle->writebufout,
    1.43 @@ -328,6 +331,8 @@
    1.44            } else {
    1.45              handle->writebufout += written;
    1.46              handle->writeleft -= written;
    1.47 +            if (written >= handle->flushedleft) handle->flushedleft = 0;
    1.48 +            else handle->flushedleft -= written;
    1.49            }
    1.50          }
    1.51          handle->writebufin = 0;
    1.52 @@ -339,28 +344,29 @@
    1.53      lua_pushnil(L);
    1.54      lua_rawseti(L, -2, handle->writeqout++);
    1.55    }
    1.56 -  if (flush) {
    1.57 -    moonbr_io_handle_set_nopush(L, handle, 0);
    1.58 -    while (handle->writebufout < handle->writebufin) {
    1.59 -      written = write(
    1.60 -        handle->fd,
    1.61 -        handle->writebuf + handle->writebufout,
    1.62 -        handle->writebufin - handle->writebufout
    1.63 -      );
    1.64 -      if (written < 0) {
    1.65 -        if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
    1.66 -          goto moonbr_io_write_impl_block;
    1.67 -        } else if (errno != EINTR) {
    1.68 -          moonbr_io_errmsg();
    1.69 -          lua_pushnil(L);
    1.70 -          lua_pushstring(L, errmsg);
    1.71 -          return 2;
    1.72 -        }
    1.73 -      } else {
    1.74 -        handle->writebufout += written;
    1.75 -        handle->writeleft -= written;
    1.76 +  while (handle->flushedleft) {
    1.77 +    written = write(
    1.78 +      handle->fd,
    1.79 +      handle->writebuf + handle->writebufout,
    1.80 +      handle->writebufin - handle->writebufout
    1.81 +    );
    1.82 +    if (written < 0) {
    1.83 +      if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
    1.84 +        goto moonbr_io_write_impl_block;
    1.85 +      } else if (errno != EINTR) {
    1.86 +        moonbr_io_errmsg();
    1.87 +        lua_pushnil(L);
    1.88 +        lua_pushstring(L, errmsg);
    1.89 +        return 2;
    1.90        }
    1.91 +    } else {
    1.92 +      handle->writebufout += written;
    1.93 +      handle->writeleft -= written;
    1.94 +      if (written >= handle->flushedleft) handle->flushedleft = 0;
    1.95 +      else handle->flushedleft -= written;
    1.96      }
    1.97 +  }
    1.98 +  if (handle->writebufout == handle->writebufin) {
    1.99      handle->writebufin = 0;
   1.100      handle->writebufout = 0;
   1.101    }
   1.102 @@ -527,6 +533,7 @@
   1.103    handle->readbufout = 0;
   1.104    handle->writeerr = 0;
   1.105    handle->writeleft = 0;
   1.106 +  handle->flushedleft = 0;
   1.107    handle->writeqin = 0;
   1.108    handle->writeqout = 0;
   1.109    handle->writeqoff = 0;

Impressum / About Us