login

<     >

2023-08-26 12:46:02 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

tcp: set O_NONBLOCK only when necessary

diff --git a/lib/ludweb/tcp.lua b/lib/ludweb/tcp.lua
index 771f990..c24b29b 100644
--- a/lib/ludweb/tcp.lua
+++ b/lib/ludweb/tcp.lua
@@ -87,7 +87,6 @@ function TCP:init(port)
     while addr ~= nil do
         sockfd = C.socket(addr.ai_family, addr.ai_socktype, addr.ai_protocol)
         if sockfd >= 0 then
-            set_non_blocking(sockfd)
             C.setsockopt(sockfd, C.SOL_SOCKET, C.SO_REUSEADDR, val, ffi.sizeof(val))
             err = C.bind(sockfd, addr.ai_addr, addr.ai_addrlen)
             if err >= 0 then
@@ -100,6 +99,7 @@ function TCP:init(port)
     assert(addr ~= nil, "bind error")
     C.freeaddrinfo(servinfo[0])
     assert(C.listen(sockfd, self.backlog) >= 0)
+    set_non_blocking(sockfd)
     self.sockfd = sockfd
 end
 
@@ -124,7 +124,6 @@ function TCP:run()
             if evs[n].data.fd == self.sockfd then
                 local newfd = C.accept(self.sockfd, nil, nil)
                 if newfd < 0 then break end
-                set_non_blocking(newfd)
                 ev[0].events = bit.bor(C.EPOLLIN, C.EPOLLET)
                 ev[0].data.fd = newfd
                 assert(C.epoll_ctl(efd, C.EPOLL_CTL_ADD, newfd, ev) >= 0, "epoll_ctl error")