Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure TCP socket to reduce latency #1683

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Features
* PCAN: Optimize send performance (#1640)
* PCAN: Support version string of older PCAN basic API (#1644)
* Kvaser: add parameter exclusive and `override_exclusive` (#1660)
* socketcand: Add parameter `tcp_tune` to reduce latency (#1683)

### Miscellaneous
* Distinguish Text/Binary-IO for Reader/Writer classes. (#1585)
Expand Down
47 changes: 46 additions & 1 deletion can/interfaces/socketcand/socketcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
http://www.domologic.de
"""
import logging
import os
import select
import socket
import time
Expand Down Expand Up @@ -75,10 +76,42 @@ def connect_to_server(s, host, port):


class SocketCanDaemonBus(can.BusABC):
def __init__(self, channel, host, port, can_filters=None, **kwargs):
def __init__(self, channel, host, port, tcp_tune=False, can_filters=None, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you could add a proper docstring like

"""Creates a new socketcan bus.

The socketcand interface API is not really well documented in doc/interfaces/socketcand.rst

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty, added docstrings to public methods of the Bus object.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks! You can see the docs for your PR here, your changes do not show up yet.

You need to update socketcand.rst.

Add something like

.. autoclass:: can.interfaces.socketcand.SocketCanDaemonBus
   :show-inheritance:
   :member-order: bysource
   :members:

You can build the docs locally with python -m sphinx -Wan --keep-going doc build.

"""Connects to a CAN bus served by socketcand.

It will attempt to connect to the server for up to 10s, after which a
TimeoutError exception will be thrown.

If the handshake with the socketcand server fails, a CanError exception
is thrown.

:param channel:
The can interface name served by socketcand.
An example channel would be 'vcan0' or 'can0'.
:param host:
The host address of the socketcand server.
:param port:
The port of the socketcand server.
:param tcp_tune:
This tunes the TCP socket for low latency (TCP_NODELAY, and
TCP_QUICKACK).
This option is not available under windows.
:param can_filters:
See :meth:`can.BusABC.set_filters`.
"""
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:
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 @@ -120,6 +153,8 @@ def _recv_internal(self, timeout):
ascii_msg = self.__socket.recv(1024).decode(
"ascii"
) # may contain multiple messages
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 @@ -173,16 +208,26 @@ 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 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 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!")

def send(self, msg, timeout=None):
"""Transmit a message to the CAN bus.

:param msg: A message object.
:param timeout: Ignored
"""
ascii_msg = convert_can_message_to_ascii_message(msg)
self._tcp_send(ascii_msg)

def shutdown(self):
"""Stops all active periodic tasks and closes the socket."""
super().shutdown()
self.__socket.close()
8 changes: 8 additions & 0 deletions doc/interfaces/socketcand.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ The output may look like this::
Timestamp: 1637791111.609763 ID: 0000031d X Rx DLC: 8 16 27 d8 3d fe d8 31 24
Timestamp: 1637791111.634630 ID: 00000587 X Rx DLC: 8 4e 06 85 23 6f 81 2b 65

Bus
---

.. autoclass:: can.interfaces.socketcand.SocketCanDaemonBus
:show-inheritance:
:member-order: bysource
:members:

Socketcand Quickstart
---------------------

Expand Down