I am seeing a very simple piece of Lua code timing out when sending over TCP. I can do nearly the same thing in python and it works fine. Both are running locally on my network, not on the hub. How would you port this Python code to a Lua equivalent?
If I donât set a timeout in Lua , I eventually get a response, but after a very long period of time (~120 sec). I know it isnât the device since the Python is very fast.
I have tried the receive with â*aâ, â*lâ and 1024. I have to have the timeout or I just sit there forever. I do get data in the partial, but with a timeout status. Iâm guessing that the device isnât closing the connection which is why receive isnât returning right away. Just not sure why python doesnât see the same thing.
Pythonâs recv() call returns any and all bytes UP TO the max passed in (BUFFER_SIZE in your example). Blocks on no data ready, returns with 1-BUFFER_SIZE bytes in the read queue.
Luaâs receive() works on patterns of either:
all-until-socket closure (*a)
lines (CR/LF delimited) (*l)
total bytes (but not fewer, until socket closure). (<number>)
Its based on the file i/o patterns, which do the same operations (with EOF being equivalent to socket closure).
This is my understanding as well. Iâm not sure how to mimic Pythonâs behavior. It appears that the device doesnât close the channel, nor does it provide CR/LF characters. Since the bytes are never 1024, it just sits there.
Oddly enough, I moved the code on to the hub and ran it with cosock instead of LuaSocket. The code ran as desired, but Iâd still like it to make sense.
This has returned and I now see the stalling behavior on all platforms with Lua. Seems like I will need to figure this out after all. I am getting the data I want and the code works, but every request is âtiming outâ and the data is returned as a partial, although it has everything in there.
The protocol has some markers in the headers indicating the packet sizes, so I am just going to manually read the bytes by count instead of using the formatting specifiers. The device doesnât appear to use the control characters needed by the formatting specifiers. I donât have an explanation for why Python is more forgiving, but I suppose that is for another day.
Yes. You canât use *a since that will read until the socket closes. Yours will not close with event stream. Use *l to read line by line. You need to read line by line anyway for SSE. SSE will provide you chunk sizes, then you read by buffer length. So it is receive(*l) --> check the chunk length --> receive(length) --> repeat