moonbridge

changeset 96:fdc1bb710544

Allow full write buffer without forcing TCP PSH
author jbe
date Wed Apr 08 01:22:20 2015 +0200 (2015-04-08)
parents 719f83c7fea4
children 0561abcc68ee
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Wed Apr 08 01:12:03 2015 +0200
     1.2 +++ b/moonbridge_io.c	Wed Apr 08 01:22:20 2015 +0200
     1.3 @@ -55,14 +55,18 @@
     1.4    flags = fcntl(handle->fd, F_GETFL, 0);
     1.5    if (flags == -1) {
     1.6      moonbr_io_errmsg();
     1.7 -    handle->nonblocking = -1;
     1.8 +    close(handle->fd);
     1.9 +    handle->fd = -1;
    1.10 +    handle->closed = 1;
    1.11      luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
    1.12    }
    1.13    if (nonblocking) flags |= O_NONBLOCK;
    1.14    else flags &= ~O_NONBLOCK;
    1.15    if (fcntl(handle->fd, F_SETFL, flags) == -1) {
    1.16      moonbr_io_errmsg();
    1.17 -    handle->nonblocking = -1;
    1.18 +    close(handle->fd);
    1.19 +    handle->fd = -1;
    1.20 +    handle->closed = 1;
    1.21      luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
    1.22    }
    1.23    handle->nonblocking = nonblocking;
    1.24 @@ -77,26 +81,37 @@
    1.25    }
    1.26    if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
    1.27      moonbr_io_errmsg();
    1.28 +    close(handle->fd);
    1.29 +    handle->fd = -1;
    1.30 +    handle->closed = 1;
    1.31      luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
    1.32    }
    1.33  }
    1.34  
    1.35  static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
    1.36 +#if defined(TCP_NOPUSH) || defined(TCP_CORK)
    1.37    if (!handle->isnetwork || handle->nopush == nopush) return;
    1.38 -#if defined(TCP_CORK) && !defined(TCP_NOPUSH)
    1.39 +#if defined(TCP_NOPUSH)
    1.40 +  if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
    1.41 +    moonbr_io_errmsg();
    1.42 +    close(handle->fd);
    1.43 +    handle->fd = -1;
    1.44 +    handle->closed = 1;
    1.45 +    luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
    1.46 +  }
    1.47 +#elif defined(TCP_CORK)
    1.48    if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
    1.49      moonbr_io_errmsg();
    1.50 -    handle->nopush = -1;
    1.51 +    close(handle->fd);
    1.52 +    handle->fd = -1;
    1.53 +    handle->closed = 1;
    1.54      luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
    1.55    }
    1.56 -#else
    1.57 -  if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
    1.58 -    moonbr_io_errmsg();
    1.59 -    handle->nopush = -1;
    1.60 -    luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
    1.61 -  }
    1.62  #endif
    1.63    handle->nopush = nopush;
    1.64 +#else
    1.65 +#warning Neither TCP_NOPUSH nor TCP_CORK is available
    1.66 +#endif
    1.67  }
    1.68  
    1.69  static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
    1.70 @@ -228,15 +243,12 @@
    1.71      lua_rawgeti(L, -1, handle->writeqout);
    1.72      str = lua_tolstring(L, -1, &strlen);
    1.73      while (handle->writeqoff < strlen) {
    1.74 -      if (strlen - handle->writeqoff < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
    1.75 +      if (strlen - handle->writeqoff <= MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
    1.76          memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, strlen - handle->writeqoff);
    1.77          handle->writebufcnt += strlen - handle->writeqoff;
    1.78          break;
    1.79        } else {
    1.80 -        moonbr_io_handle_set_nopush(L, handle,
    1.81 -          (flush || handle->writeleft == MOONBR_IO_WRITEBUFLEN) ?
    1.82 -          0 : 1
    1.83 -        );
    1.84 +        moonbr_io_handle_set_nopush(L, handle, flush ? 0 : 1);
    1.85          written = 0;
    1.86          memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
    1.87          handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;

Impressum / About Us