Skip to content

Commit

Permalink
Merge pull request #1 from secjoa/patch-1
Browse files Browse the repository at this point in the history
fix receive
  • Loading branch information
HereAdvertise authored Jul 11, 2024
2 parents f566723 + 015e577 commit de6dc04
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
29 changes: 18 additions & 11 deletions pgmoon/redbean.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ do
return nil, err:doc()
end
if self.timeout then
unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_RCVTIMEO, self.timeout)
unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_SNDTIMEO, self.timeout)
unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_RCVTIMEO, self.timeout / 1000)
unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_SNDTIMEO, self.timeout / 1000)
end
return true
end,
Expand All @@ -30,7 +30,7 @@ do
local CANWRITE = unix.POLLOUT | unix.POLLWRNORM
local events = assert(unix.poll({
[self.unix_socket] = unix.POLLOUT
}))
}, self.timeout))
if not (events[self.unix_socket]) then
return nil, "timeout"
end
Expand All @@ -49,7 +49,7 @@ do
if size then
local events = assert(unix.poll({
[self.unix_socket] = unix.POLLIN
}))
}, self.timeout))
if not (events[self.unix_socket]) then
return nil, "timeout"
end
Expand All @@ -59,21 +59,28 @@ do
return unix.recv(self.unix_socket, size)
end
local buf = ""
while not buf:find("\n") do
buf = buf .. assert(unix.recv(self.unix_socket, 4096))
while true do
local events = assert(unix.poll({
[self.unix_socket] = unix.POLLIN
}, self.timeout))
if not (events[self.unix_socket]) then
return nil, "timeout"
end
if events[self.unix_socket] & CANREAD == 0 then
break
else
buf = buf .. assert(unix.recv(self.unix_socket, 4096))
end
end
return buf
end,
close = function(self)
return assert(unix.close(self.unix_socket))
end,
settimeout = function(self, t)
if t then
t = t / 1000
end
if self.unix_socket then
unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_RCVTIMEO, t)
return unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_SNDTIMEO, t)
unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_RCVTIMEO, t / 1000)
return unix.setsockopt(self.unix_socket, SOL_SOCKET, SO_SNDTIMEO, t / 1000)
else
self.timeout = t
end
Expand Down
24 changes: 13 additions & 11 deletions pgmoon/redbean.moon
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class RedbeanSocket
return nil, err\doc!

if @timeout
unix.setsockopt @unix_socket, SOL_SOCKET, SO_RCVTIMEO, @timeout
unix.setsockopt @unix_socket, SOL_SOCKET, SO_SNDTIMEO, @timeout
unix.setsockopt @unix_socket, SOL_SOCKET, SO_RCVTIMEO, @timeout / 1000
unix.setsockopt @unix_socket, SOL_SOCKET, SO_SNDTIMEO, @timeout / 1000

true

Expand All @@ -30,7 +30,7 @@ class RedbeanSocket
data = flatten ...

CANWRITE = unix.POLLOUT | unix.POLLWRNORM
events = assert unix.poll @unix_socket: unix.POLLOUT
events = assert unix.poll @unix_socket: unix.POLLOUT, @timeout
return nil, "timeout" unless events[@unix_socket]
return nil, "close" if events[@unix_socket] & CANWRITE == 0
sent, err = unix.send @unix_socket, data
Expand All @@ -41,26 +41,28 @@ class RedbeanSocket
CANREAD = unix.POLLIN | unix.POLLRDNORM | unix.POLLRDBAND
size = tonumber(pattern)
if size
events = assert unix.poll @unix_socket: unix.POLLIN
events = assert unix.poll @unix_socket: unix.POLLIN, @timeout
return nil, "timeout" unless events[@unix_socket]
return nil, "close" if events[@unix_socket] & CANREAD == 0
return unix.recv @unix_socket, size

buf = ""
while not buf\find "\n"
buf = buf .. assert unix.recv @unix_socket, 4096
while true
events = assert unix.poll @unix_socket: unix.POLLIN, @timeout
return nil, "timeout" unless events[@unix_socket]
if events[@unix_socket] & CANREAD == 0
break
else
buf = buf .. assert unix.recv @unix_socket, 4096
buf

close: =>
assert unix.close @unix_socket

settimeout: (t) =>
if t
t = t/1000

if @unix_socket
unix.setsockopt @unix_socket, SOL_SOCKET, SO_RCVTIMEO, t
unix.setsockopt @unix_socket, SOL_SOCKET, SO_SNDTIMEO, t
unix.setsockopt @unix_socket, SOL_SOCKET, SO_RCVTIMEO, t / 1000
unix.setsockopt @unix_socket, SOL_SOCKET, SO_SNDTIMEO, t / 1000
else
@timeout = t

Expand Down

0 comments on commit de6dc04

Please sign in to comment.