I have a custom Lua interpreter executable with some basic socket functionality built in. It's not Luasocket, and as much as I would like to be using Luasocket here, I can't (so please don't suggest it as an answer).
The socket API that I'm working with relies on asynchronous closures to signal that a network operation is complete. So, in the code below, socketConnect() returns immediately and then onConnect() is called later on when the connection is complete.
local function onConnect(cookie, err, sock)
print("Connected!")
end
local function connect(host, port)
local success, err = socketConnect(host, port, onConnect)
print("Connecting...")
end
So, here's the question. I want to make the connect() function block until the onConnect() closure is called. I'm pretty new at Lua, but I'm hoping that coroutines might be helpful here?
EDIT: Here's my attempt at making the function block by using a coroutine:
local connected = false
local function onConnect(cookie, err, sock)
print("Connected!")
connected = true
end
local coroConnect = coroutine.create(
function()
local success, err = socketConnect(m_sHost, m_nPort, onConnect);
while not connected do
coroutine.yield()
end
end
)
local function connect(sHost, nPort)
m_sHost = sHost
m_nPort = nPort
while not coroutine.status(coroConnect) ~= "dead" do
coroutine.resume(coroConnect)
print("Connecting...")
end
end
socketConnectcalls the callback before it returns or the code uses some multithreading extension, in which case it would be helpful to know which one. - Colonel Thirty Two