Skip to content

Commit

Permalink
Move os check into __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
faisal-shah committed Oct 27, 2023
1 parent 3aa9047 commit 9bd4b8c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions can/interfaces/socketcand/socketcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,17 @@ class SocketCanDaemonBus(can.BusABC):
def __init__(self, channel, host, port, tcp_tune=False, can_filters=None, **kwargs):
self.__host = host
self.__port = port

self.__tcp_tune = tcp_tune
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if os.name == "nt":
self.__tcp_tune = False
log.warning("'tcp_tune' not available in Windows. Setting to False")
else:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

self.__message_buffer = deque()
self.__receive_buffer = "" # i know string is not the most efficient here
self.channel = channel
Expand Down Expand Up @@ -124,9 +131,8 @@ def _recv_internal(self, timeout):
ascii_msg = self.__socket.recv(1024).decode(
"ascii"
) # may contain multiple messages
if os.name != "nt":
if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)
if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)
self.__receive_buffer += ascii_msg
log.debug(f"Received Ascii Message: {ascii_msg}")
buffer_view = self.__receive_buffer
Expand Down Expand Up @@ -180,15 +186,13 @@ def _recv_internal(self, timeout):
def _tcp_send(self, msg: str):
log.debug(f"Sending TCP Message: '{msg}'")
self.__socket.sendall(msg.encode("ascii"))
if os.name != "nt":
if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)
if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)

def _expect_msg(self, msg):
ascii_msg = self.__socket.recv(256).decode("ascii")
if os.name != "nt":
if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)
if self.__tcp_tune:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)
if not ascii_msg == msg:
raise can.CanError(f"{msg} message expected!")

Expand Down

0 comments on commit 9bd4b8c

Please sign in to comment.