Posted 10 May 2018 - 04:20 PM
So I have a table called socket (don't ask where it came from) and an API to wrap it. However, strangely, there is a function that, when I call it outside the wrapper, it works just fine, but when called from inside the wrapper, it just freezes up for a few seconds.
The wrapper looks like this:
The code that I am using to test it looks like this:
Line 25 of socket.lua is this one:
I've also removed the second call to socket.isClosed() from the test program, just to see if it has anything to do with "function memory", but it still lags and errors
Anybody know why? Am I calling a function within itself without noticing or something?
The wrapper looks like this:
local nativeSocket = socket
lookup = nativeSocket.lookup
checkHost = nativeSocket.checkHost
local function wrapSocket(sock)
local nativeSock = sock
sock.write = function(sText)
local ok, isClosed = nativeSock.isClosed()
if isClosed then
return false, "Attempt to interact with closed socket"
end
return nativeSock.write(sText)
end
sock.read = function(nAmount)
local ok, isClosed = nativeSock.isClosed()
if isClosed then
return false, "Attempt to interact with closed socket"
end
if (type(nAmount) ~= "number") or ((nAmount%1)~=0) or nAmount<1 then
return false, "expected positive integer"
end
return nativeSock.read(nAmount)
end
sock.close = function()
local ok, isClosed = nativeSock.isClosed()
if isClosed then
return false, "Attempt to interact with closed socket"
end
return nativeSock.close()
end
return sock
end
open = function(host, port)
if not socket.checkHost("http://"..host) then
return false, "Bad or blacklisted URL"
end
local ok, sock = nativeSocket.open(host, port)
if not ok then return ok, sock end
return true, wrapSocket(sock)
end
The code that I am using to test it looks like this:
os.loadAPI("socket.lua")
local ok, host, port = socket.lookup("https://google.com")
local ok, sock = socket.open(host, port)
if ok then
--First call, no lag
print(sock.isClosed())
--Second call, just to be sure
print(sock.isClosed())
--The next function calls the above functions
print(sock.close())
else
print(sock)
end
The log looks something like this:
true false
true false
socket.lua: 25: too long without yielding
There is a long delay between the second time it prints "true false" and the too long without yielding errorLine 25 of socket.lua is this one:
local ok, isClosed = nativeSock.isClosed()
I've also removed the second call to socket.isClosed() from the test program, just to see if it has anything to do with "function memory", but it still lags and errors
Anybody know why? Am I calling a function within itself without noticing or something?
Edited on 10 May 2018 - 02:34 PM