7272MQTT_PKT_TYPE_MASK = const (0xF0 )
7373
7474
75+ CONNACK_ERROR_INCORRECT_PROTOCOL = const (0x01 )
76+ CONNACK_ERROR_ID_REJECTED = const (0x02 )
77+ CONNACK_ERROR_SERVER_UNAVAILABLE = const (0x03 )
78+ CONNACK_ERROR_INCORECT_USERNAME_PASSWORD = const (0x04 )
79+ CONNACK_ERROR_UNAUTHORIZED = const (0x05 )
80+
7581CONNACK_ERRORS = {
76- const ( 0x01 ) : "Connection Refused - Incorrect Protocol Version" ,
77- const ( 0x02 ) : "Connection Refused - ID Rejected" ,
78- const ( 0x03 ) : "Connection Refused - Server unavailable" ,
79- const ( 0x04 ) : "Connection Refused - Incorrect username/password" ,
80- const ( 0x05 ) : "Connection Refused - Unauthorized" ,
82+ CONNACK_ERROR_INCORRECT_PROTOCOL : "Connection Refused - Incorrect Protocol Version" ,
83+ CONNACK_ERROR_ID_REJECTED : "Connection Refused - ID Rejected" ,
84+ CONNACK_ERROR_SERVER_UNAVAILABLE : "Connection Refused - Server unavailable" ,
85+ CONNACK_ERROR_INCORECT_USERNAME_PASSWORD : "Connection Refused - Incorrect username/password" ,
86+ CONNACK_ERROR_UNAUTHORIZED : "Connection Refused - Unauthorized" ,
8187}
8288
8389_default_sock = None # pylint: disable=invalid-name
8793class MMQTTException (Exception ):
8894 """MiniMQTT Exception class."""
8995
96+ def __init__ (self , error , code = None ):
97+ super ().__init__ (error , code )
98+ self .code = code
99+
90100
91101class NullLogger :
92102 """Fake logger class that does not do anything"""
@@ -428,17 +438,24 @@ def connect(
428438 self .logger .warning (f"Socket error when connecting: { e } " )
429439 backoff = False
430440 except MMQTTException as e :
431- last_exception = e
441+ self . _close_socket ()
432442 self .logger .info (f"MMQT error: { e } " )
443+ if e .code in [
444+ CONNACK_ERROR_INCORECT_USERNAME_PASSWORD ,
445+ CONNACK_ERROR_UNAUTHORIZED ,
446+ ]:
447+ # No sense trying these again, re-raise
448+ raise
449+ last_exception = e
433450 backoff = True
434451
435452 if self ._reconnect_attempts_max > 1 :
436453 exc_msg = "Repeated connect failures"
437454 else :
438455 exc_msg = "Connect failure"
456+
439457 if last_exception :
440458 raise MMQTTException (exc_msg ) from last_exception
441-
442459 raise MMQTTException (exc_msg )
443460
444461 # pylint: disable=too-many-branches, too-many-statements, too-many-locals
@@ -535,7 +552,7 @@ def _connect(
535552 rc = self ._sock_exact_recv (3 )
536553 assert rc [0 ] == 0x02
537554 if rc [2 ] != 0x00 :
538- raise MMQTTException (CONNACK_ERRORS [rc [2 ]])
555+ raise MMQTTException (CONNACK_ERRORS [rc [2 ]], code = rc [ 2 ] )
539556 self ._is_connected = True
540557 result = rc [0 ] & 1
541558 if self .on_connect is not None :
@@ -549,6 +566,12 @@ def _connect(
549566 f"No data received from broker for { self ._recv_timeout } seconds."
550567 )
551568
569+ def _close_socket (self ):
570+ if self ._sock :
571+ self .logger .debug ("Closing socket" )
572+ self ._connection_manager .close_socket (self ._sock )
573+ self ._sock = None
574+
552575 # pylint: disable=no-self-use
553576 def _encode_remaining_length (
554577 self , fixed_header : bytearray , remaining_length : int
@@ -577,8 +600,7 @@ def disconnect(self) -> None:
577600 self ._sock .send (MQTT_DISCONNECT )
578601 except RuntimeError as e :
579602 self .logger .warning (f"Unable to send DISCONNECT packet: { e } " )
580- self .logger .debug ("Closing socket" )
581- self ._connection_manager .close_socket (self ._sock )
603+ self ._close_socket ()
582604 self ._is_connected = False
583605 self ._subscribed_topics = []
584606 self ._last_msg_sent_timestamp = 0
0 commit comments