@@ -181,7 +181,7 @@ def __init__(
181
181
self ._is_connected = False
182
182
self ._msg_size_lim = MQTT_MSG_SZ_LIM
183
183
self ._pid = 0
184
- self ._last_msg_sent_timestamp : int = 0
184
+ self ._last_msg_sent_timestamp_ns : int = 0
185
185
self .logger = NullLogger ()
186
186
"""An optional logging attribute that can be set with with a Logger
187
187
to enable debug logging."""
@@ -220,7 +220,7 @@ def __init__(
220
220
self .client_id = client_id
221
221
else :
222
222
# assign a unique client_id
223
- time_int = (self .get_monotonic_time () % 10000000000 ) // 10000000
223
+ time_int = (self .get_monotonic_ns_time () % 10000000000 ) // 10000000
224
224
self .client_id = f"cpy{ randint (0 , time_int )} { randint (0 , 99 )} "
225
225
# generated client_id's enforce spec.'s length rules
226
226
if len (self .client_id .encode ("utf-8" )) > 23 or not self .client_id :
@@ -244,7 +244,7 @@ def __init__(
244
244
self .on_subscribe = None
245
245
self .on_unsubscribe = None
246
246
247
- def get_monotonic_time (self ) -> float :
247
+ def get_monotonic_ns_time (self ) -> float :
248
248
"""
249
249
Provide monotonic time in nanoseconds. Based on underlying implementation
250
250
this might result in imprecise time, that will result in the library
@@ -256,12 +256,12 @@ def get_monotonic_time(self) -> float:
256
256
257
257
return int (time .monotonic () * 1000000000 )
258
258
259
- def diff_ns (self , stamp ):
259
+ def diff_ns (self , stamp_ns ):
260
260
"""
261
261
Taking timestamp differences using nanosecond ints before dividing
262
262
should maintain precision.
263
263
"""
264
- return (self .get_monotonic_time () - stamp ) / 1000000000
264
+ return (self .get_monotonic_ns_time () - stamp_ns ) / 1000000000
265
265
266
266
def __enter__ (self ):
267
267
return self
@@ -534,9 +534,9 @@ def _connect(
534
534
if self ._username is not None :
535
535
self ._send_str (self ._username )
536
536
self ._send_str (self ._password )
537
- self ._last_msg_sent_timestamp = self .get_monotonic_time ()
537
+ self ._last_msg_sent_timestamp_ns = self .get_monotonic_ns_time ()
538
538
self .logger .debug ("Receiving CONNACK packet from broker" )
539
- stamp = self .get_monotonic_time ()
539
+ stamp_ns = self .get_monotonic_ns_time ()
540
540
while True :
541
541
op = self ._wait_for_msg ()
542
542
if op == 32 :
@@ -552,7 +552,7 @@ def _connect(
552
552
return result
553
553
554
554
if op is None :
555
- if self .diff_ns (stamp ) > self ._recv_timeout :
555
+ if self .diff_ns (stamp_ns ) > self ._recv_timeout :
556
556
raise MMQTTException (
557
557
f"No data received from broker for { self ._recv_timeout } seconds."
558
558
)
@@ -589,7 +589,7 @@ def disconnect(self) -> None:
589
589
self ._connection_manager .close_socket (self ._sock )
590
590
self ._is_connected = False
591
591
self ._subscribed_topics = []
592
- self ._last_msg_sent_timestamp = 0
592
+ self ._last_msg_sent_timestamp_ns = 0
593
593
if self .on_disconnect is not None :
594
594
self .on_disconnect (self , self .user_data , 0 )
595
595
@@ -602,14 +602,14 @@ def ping(self) -> list[int]:
602
602
self .logger .debug ("Sending PINGREQ" )
603
603
self ._sock .send (MQTT_PINGREQ )
604
604
ping_timeout = self .keep_alive
605
- stamp = self .get_monotonic_time ()
606
- self ._last_msg_sent_timestamp = stamp
605
+ stamp_ns = self .get_monotonic_ns_time ()
606
+ self ._last_msg_sent_timestamp_ns = stamp_ns
607
607
rc , rcs = None , []
608
608
while rc != MQTT_PINGRESP :
609
609
rc = self ._wait_for_msg ()
610
610
if rc :
611
611
rcs .append (rc )
612
- if self .diff_ns (stamp ) > ping_timeout :
612
+ if self .diff_ns (stamp_ns ) > ping_timeout :
613
613
raise MMQTTException ("PINGRESP not returned from broker." )
614
614
return rcs
615
615
@@ -678,11 +678,11 @@ def publish(
678
678
self ._sock .send (pub_hdr_fixed )
679
679
self ._sock .send (pub_hdr_var )
680
680
self ._sock .send (msg )
681
- self ._last_msg_sent_timestamp = self .get_monotonic_time ()
681
+ self ._last_msg_sent_timestamp_ns = self .get_monotonic_ns_time ()
682
682
if qos == 0 and self .on_publish is not None :
683
683
self .on_publish (self , self .user_data , topic , self ._pid )
684
684
if qos == 1 :
685
- stamp = self .get_monotonic_time ()
685
+ stamp_ns = self .get_monotonic_ns_time ()
686
686
while True :
687
687
op = self ._wait_for_msg ()
688
688
if op == 0x40 :
@@ -696,7 +696,7 @@ def publish(
696
696
return
697
697
698
698
if op is None :
699
- if self .diff_ns (stamp ) > self ._recv_timeout :
699
+ if self .diff_ns (stamp_ns ) > self ._recv_timeout :
700
700
raise MMQTTException (
701
701
f"No data received from broker for { self ._recv_timeout } seconds."
702
702
)
@@ -755,12 +755,12 @@ def subscribe(self, topic: Optional[Union[tuple, str, list]], qos: int = 0) -> N
755
755
self .logger .debug (f"SUBSCRIBING to topic { t } with QoS { q } " )
756
756
self .logger .debug (f"payload: { payload } " )
757
757
self ._sock .send (payload )
758
- stamp = self .get_monotonic_time ()
759
- self ._last_msg_sent_timestamp = stamp
758
+ stamp_ns = self .get_monotonic_ns_time ()
759
+ self ._last_msg_sent_timestamp_ns = stamp_ns
760
760
while True :
761
761
op = self ._wait_for_msg ()
762
762
if op is None :
763
- if self .diff_ns (stamp ) > self ._recv_timeout :
763
+ if self .diff_ns (stamp_ns ) > self ._recv_timeout :
764
764
raise MMQTTException (
765
765
f"No data received from broker for { self ._recv_timeout } seconds."
766
766
)
@@ -832,13 +832,13 @@ def unsubscribe(self, topic: Optional[Union[str, list]]) -> None:
832
832
for t in topics :
833
833
self .logger .debug (f"UNSUBSCRIBING from topic { t } " )
834
834
self ._sock .send (payload )
835
- self ._last_msg_sent_timestamp = self .get_monotonic_time ()
835
+ self ._last_msg_sent_timestamp_ns = self .get_monotonic_ns_time ()
836
836
self .logger .debug ("Waiting for UNSUBACK..." )
837
837
while True :
838
- stamp = self .get_monotonic_time ()
838
+ stamp_ns = self .get_monotonic_ns_time ()
839
839
op = self ._wait_for_msg ()
840
840
if op is None :
841
- if self .diff_ns (stamp ) > self ._recv_timeout :
841
+ if self .diff_ns (stamp_ns ) > self ._recv_timeout :
842
842
raise MMQTTException (
843
843
f"No data received from broker for { self ._recv_timeout } seconds."
844
844
)
@@ -938,26 +938,26 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
938
938
self ._connected ()
939
939
self .logger .debug (f"waiting for messages for { timeout } seconds" )
940
940
941
- stamp = self .get_monotonic_time ()
941
+ stamp_ns = self .get_monotonic_ns_time ()
942
942
rcs = []
943
943
944
944
while True :
945
- if self .diff_ns (self ._last_msg_sent_timestamp ) >= self .keep_alive :
945
+ if self .diff_ns (self ._last_msg_sent_timestamp_ns ) >= self .keep_alive :
946
946
# Handle KeepAlive by expecting a PINGREQ/PINGRESP from the server
947
947
self .logger .debug (
948
948
"KeepAlive period elapsed - requesting a PINGRESP from the server..."
949
949
)
950
950
rcs .extend (self .ping ())
951
951
# ping() itself contains a _wait_for_msg() loop which might have taken a while,
952
952
# so check here as well.
953
- if self .diff_ns (stamp ) > timeout :
953
+ if self .diff_ns (stamp_ns ) > timeout :
954
954
self .logger .debug (f"Loop timed out after { timeout } seconds" )
955
955
break
956
956
957
957
rc = self ._wait_for_msg ()
958
958
if rc is not None :
959
959
rcs .append (rc )
960
- if self .diff_ns (stamp ) > timeout :
960
+ if self .diff_ns (stamp_ns ) > timeout :
961
961
self .logger .debug (f"Loop timed out after { timeout } seconds" )
962
962
break
963
963
@@ -1063,7 +1063,7 @@ def _sock_exact_recv(
1063
1063
:param float timeout: timeout, in seconds. Defaults to keep_alive
1064
1064
:return: byte array
1065
1065
"""
1066
- stamp = self .get_monotonic_time ()
1066
+ stamp_ns = self .get_monotonic_ns_time ()
1067
1067
if not self ._backwards_compatible_sock :
1068
1068
# CPython/Socketpool Impl.
1069
1069
rc = bytearray (bufsize )
@@ -1078,7 +1078,7 @@ def _sock_exact_recv(
1078
1078
recv_len = self ._sock .recv_into (mv , to_read )
1079
1079
to_read -= recv_len
1080
1080
mv = mv [recv_len :]
1081
- if self .diff_ns (stamp ) > read_timeout :
1081
+ if self .diff_ns (stamp_ns ) > read_timeout :
1082
1082
raise MMQTTException (
1083
1083
f"Unable to receive { to_read } bytes within { read_timeout } seconds."
1084
1084
)
@@ -1098,7 +1098,7 @@ def _sock_exact_recv(
1098
1098
recv = self ._sock .recv (to_read )
1099
1099
to_read -= len (recv )
1100
1100
rc += recv
1101
- if self .diff_ns (stamp ) > read_timeout :
1101
+ if self .diff_ns (stamp_ns ) > read_timeout :
1102
1102
raise MMQTTException (
1103
1103
f"Unable to receive { to_read } bytes within { read_timeout } seconds."
1104
1104
)
0 commit comments