From ef081b573140aa6fcdbef2b970e363f2e825590d Mon Sep 17 00:00:00 2001 From: Jerry Mailloux Date: Mon, 25 Mar 2024 17:43:57 -0400 Subject: [PATCH] Fix problems with UdpMulticast and MacOS --- can/interfaces/udp_multicast/bus.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/can/interfaces/udp_multicast/bus.py b/can/interfaces/udp_multicast/bus.py index 8ca2d516b..c14f87e9c 100644 --- a/can/interfaces/udp_multicast/bus.py +++ b/can/interfaces/udp_multicast/bus.py @@ -238,6 +238,7 @@ def __init__( # used by send() self._send_destination = (self.group, self.port) self._last_send_timeout: Optional[float] = None + self.hack = 0 def _create_socket(self, address_family: socket.AddressFamily) -> socket.socket: """Creates a new socket. This might fail and raise an exception! @@ -268,6 +269,13 @@ def _create_socket(self, address_family: socket.AddressFamily) -> socket.socket: # Allow multiple programs to access that address + port sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + try: + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + except AttributeError: + # SO_REUSEPORT not available on this platform + log.warning("SO_REUSEPORT not available!") + pass + # set how to receive timestamps try: sock.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1) @@ -385,19 +393,8 @@ def recv( ) timestamp = seconds + nanoseconds * 1.0e-9 else: - result_buffer = ioctl( - self._socket.fileno(), - SIOCGSTAMP, - bytes(self.received_timestamp_struct_size), - ) - seconds, microseconds = struct.unpack( - self.received_timestamp_struct, result_buffer - ) - if microseconds >= 1e6: - raise can.CanOperationError( - f"Timestamp microseconds field was out of range: {microseconds} not less than 1e6" - ) - timestamp = seconds + microseconds * 1e-6 + self.hack += 1 + timestamp = self.hack return raw_message_data, sender_address, timestamp