uMQTT_Simple suggestions: #1: Return ping result instead of None & #2: Make socket write under 'disconnect' & 'ping' non-blocking
Suggestion (#1) for a small change in line 205 ('section' #PINGRESP in function wait_msg):
return res[0] instead of return None
This allows you to check in your own consumer code that called check_msg() or wait_msg(), that the ping response came by and was valid, e.g.: if(oReply == 0xD0): # valid ping response
And suggestion (#2) for another small change in line 122 (section 'ping') and line 118 (section 'disconnect'):
add line self.sock.setblocking(False) in front of self.sock.write(b"...")
This prevents lóóóng (default 30 secs?) lock-ups when the connection is lost (or not ready yet).
I haven't experienced a down-side yet of this non-blocking write (with and without a working connection).
P.s. I guess the problem is only valid when using SSL, as the (plain) socket is otherwise already initiated with specified timeout at 'connect' and may not stall that long. Once wrapped in an SSL socket though, the original timeout seems either lost or ineffective. Attempting to use settimeout(t) instead of setblocking(False) here before writing to the socket, is currently not possible with SSL sockets in micropython (see this request)
Thanks!
umqtt.simple: Return handled message type from wait_msg().
Hello everyone, thanks for the nice work on micropython.
Problem
I am trying to solve two problems with this request:
A) I execute the method check_msg() in umqtt.simple.MQTTClient regularly from a main loop. For scheduling reasons I would like to know if the call has handled an incoming message or not.
B) Currently there seems no way to react to an incoming PINGRESP.
Solution
The proposed solution is: Return the message header of a handled message instead of None at the end of the function. Return the message header (0xd0) of a handled PINGRESP instead of None. Move the statement op = res[0] above the ping handling to be consistent. By this change the calling module can extract all the required information.
Impact
One line of code is added so the code size remains small. Since the method was always returning None I do not see any impact on depending code.