72
72
MQTT_PKT_TYPE_MASK = const (0xF0 )
73
73
74
74
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
+
75
81
CONNACK_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" ,
81
87
}
82
88
83
89
_default_sock = None # pylint: disable=invalid-name
87
93
class MMQTTException (Exception ):
88
94
"""MiniMQTT Exception class."""
89
95
96
+ def __init__ (self , error , code = None ):
97
+ super ().__init__ (error , code )
98
+ self .code = code
99
+
90
100
91
101
class NullLogger :
92
102
"""Fake logger class that does not do anything"""
@@ -428,17 +438,24 @@ def connect(
428
438
self .logger .warning (f"Socket error when connecting: { e } " )
429
439
backoff = False
430
440
except MMQTTException as e :
431
- last_exception = e
441
+ self . _close_socket ()
432
442
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
433
450
backoff = True
434
451
435
452
if self ._reconnect_attempts_max > 1 :
436
453
exc_msg = "Repeated connect failures"
437
454
else :
438
455
exc_msg = "Connect failure"
456
+
439
457
if last_exception :
440
458
raise MMQTTException (exc_msg ) from last_exception
441
-
442
459
raise MMQTTException (exc_msg )
443
460
444
461
# pylint: disable=too-many-branches, too-many-statements, too-many-locals
@@ -535,7 +552,7 @@ def _connect(
535
552
rc = self ._sock_exact_recv (3 )
536
553
assert rc [0 ] == 0x02
537
554
if rc [2 ] != 0x00 :
538
- raise MMQTTException (CONNACK_ERRORS [rc [2 ]])
555
+ raise MMQTTException (CONNACK_ERRORS [rc [2 ]], code = rc [ 2 ] )
539
556
self ._is_connected = True
540
557
result = rc [0 ] & 1
541
558
if self .on_connect is not None :
@@ -549,6 +566,12 @@ def _connect(
549
566
f"No data received from broker for { self ._recv_timeout } seconds."
550
567
)
551
568
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
+
552
575
# pylint: disable=no-self-use
553
576
def _encode_remaining_length (
554
577
self , fixed_header : bytearray , remaining_length : int
@@ -577,8 +600,7 @@ def disconnect(self) -> None:
577
600
self ._sock .send (MQTT_DISCONNECT )
578
601
except RuntimeError as e :
579
602
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 ()
582
604
self ._is_connected = False
583
605
self ._subscribed_topics = []
584
606
self ._last_msg_sent_timestamp = 0
0 commit comments