I have a library in Lua that creates and parses data packets for a protocol. When I send a packet out, I'm expecting a reply back from the destination that is then parsed into a table. I'm trying to write a wrapper around this library so that I can make a function call like the following: result = SendUnicast(dest,packetData)
and have the parsed response table returned to result.
My problem is two fold: 1) The incoming message comes in asynchronously and on a different thread than the executing script and 2) the next packet I receive isn't necessarily the response for my request, I have to parse the incoming packet and match a sequence id.
The program flow currently looks something like:
[C# UI Thread]
- Button Click
- Run Lua Script
- Call SendUnicast
- Wait For Response
[C# Data Thread]
- Incoming Message
- Pass message to Lua parser function
- if sequence matches waiting command, store parsed table, resume blocked
[C# UI Thread]
- Lua scipt returns parsed table
I can't seem to find a good method for blocking the currently executing script (in the UIThread). Creating a coroutine to be called when the message is parsed and then while coroutine.status(co) ~= "dead"
seems to kill my lua interpreter.
EDIT
I'm marking BMitch's answer as accepted because it is the correct way to handle this issue. I will warn you, however, that LuaInterface does not support coroutines and I had to add support for them to the C# code myself.