-
Notifications
You must be signed in to change notification settings - Fork 42
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
Running luamqtt through the Qt eventloop #43
Comments
Hi @syyyr, Yes, luamqtt can be used in a dedicated event loop. You have to write a lua module with several functions - to establish and close a TCP connection, and to send/read packets into it. Then you use that module as a I suppose those functions have to deal with coroutines somehow to be paused and continued when your Qt's event loop performs that connect/close/send/read operations. I would appreciate it if you will share your solution as an example so I can add it to this repo. |
Here is a brief workflow that should work. So in your Qt application, you create a Lua state ( Then you start that coroutine with When you got an event about opened connection, you can resume the Lua coroutine with the same Documentation is here - https://www.lua.org/manual/5.3/manual.html#lua_yield The only thing tricky here is sending PINGREQ MQTT packets periodically to maintain the MQTT connection open. I think you can call the Lua method |
Thanks for the quick reply. In the end, I decided that it would be easier for me to just use I'll leave the issue open until I can create the example, but for now, my problem is solved, so feel free to close this, if you want. :) |
I think this can be done, but it's quite hard. The library is very unsafe for concurrent use and will not handle partial data received or sent (and you cannot assume the data received/sent to be the full packet all at once, despite that it will mostly work). I've made a fast number of changes to handle those situations (see #31 for my branch).
|
@syyyr ,
Of course, it will be the best solution if you have such an option available. @Tieske ,
Actually, connectors have to ensure they have sent/received the same amount of data as passed to their ... and that was my thoughts before I saw this (my) code: Lines 1152 to 1158 in e3fa62c
So connector usage has to be changed to a single By the way, I'm thinking about splitting library to two parts - separate mqtt protocol implementation from the transport layer (client logic, connectors, ioloop), maybe by several github repositories. |
that send logic is correct. LuaSocket will not guarantee that the whole packet was sent at once. I don't think there's anything you can do about it, except what I already did in my PR #31 . It has all those changes and has been running for months now, without issues. That PR also separates the client from the runloop. So maybe best to continue with that PR first, and then separate the io-loop stuff out in a separate repo. |
I mean that the code with a |
Hi,
I am embedding Lua in my Qt application. However, it seems to me that running luamqtt would require me to run it in a separate thread, because it uses its own eventloop. Is it possible to run luamqtt via a custom eventloop? The workflow would be:
Is it possible to implement something like this? I can see that there is an
iteration
function, that would maybe work for this usecase? I'm not how many iterations would one operation take and also, I don't want the Lua code to block if the connection is disrupted (when an operation gets a timeout). Or maybe I can use a custom connector? From what I can read, the connector doesn't allow me to use a custom eventloop.Thanks
The text was updated successfully, but these errors were encountered: