Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix receive #1

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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