2023-08-27 11:07:55 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
tcp: refactor connection close
diff --git a/lib/ludweb/tcp.lua b/lib/ludweb/tcp.lua index 2e542fd..9f56e4a 100644 --- a/lib/ludweb/tcp.lua +++ b/lib/ludweb/tcp.lua @@ -130,11 +130,9 @@ function TCP:run() curfds = curfds + 1 else local size = C.recv(evs[n].data.fd, buffer, buflen, 0) + local can_close = false if size == 0 then - C.epoll_ctl(efd, C.EPOLL_CTL_DEL, evs[n].data.fd, ev) - C.shutdown(evs[n].data.fd, C.SHUT_RDWR) - C.close(evs[n].data.fd) - curfds = curfds - 1 + can_close = true elseif size > 0 then datain[n] = (datain[n] or "") .. ffi.string(buffer, size) if self:request_ready(datain[n]) then @@ -145,14 +143,17 @@ function TCP:run() C.send(evs[n].data.fd, dataout, #dataout, 0) end if not keep_alive then - C.epoll_ctl(efd, C.EPOLL_CTL_DEL, evs[n].data.fd, ev) - C.shutdown(evs[n].data.fd, C.SHUT_RDWR) - C.close(evs[n].data.fd) - curfds = curfds - 1 + can_close = true end datain[n] = "" end end + if can_close then + C.epoll_ctl(efd, C.EPOLL_CTL_DEL, evs[n].data.fd, ev) + C.shutdown(evs[n].data.fd, C.SHUT_RDWR) + C.close(evs[n].data.fd) + curfds = curfds - 1 + end end end end