From abd1d03cb825995928d70974a449ae9a9aa66164 Mon Sep 17 00:00:00 2001
From: Paul Brussee
Date: Sat, 2 Nov 2024 00:35:59 +0100
Subject: [PATCH] feat: add support for unix sockets to TCPTransport in
erpc_python
---
erpc_python/erpc/transport.py | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/erpc_python/erpc/transport.py b/erpc_python/erpc/transport.py
index bf117cf6..13757b1f 100644
--- a/erpc_python/erpc/transport.py
+++ b/erpc_python/erpc/transport.py
@@ -137,11 +137,15 @@ class ConnectionClosed(Exception):
class TCPTransport(FramedTransport):
- def __init__(self, host, port, isServer):
+ def __init__(self, host, port=None, isServer=False, isUnixSocket=False):
super(TCPTransport, self).__init__()
+ assert (port is not None and isUnixSocket is False) or (
+ port is None and isUnixSocket is True
+ ), "port and isUnixSocket are mutually exclusive"
self._host = host
self._port = port
self._isServer = isServer
+ self._isUnixSocket = isUnixSocket
self._sock = None
if self._isServer:
@@ -149,17 +153,25 @@ def __init__(self, host, port, isServer):
self._serverThread.daemon = True
self._serverThread.start()
self._serverSockEventStart = threading.Event()
+ elif self._isUnixSocket:
+ self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ self._sock.connect(self._host)
else:
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
self._sock.connect((self._host, self._port))
def _serve(self):
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.setblocking(1)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
- s.bind((self._host, self._port))
+ if self._isUnixSocket:
+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ s.setblocking(1)
+ s.bind(self._host)
+ else:
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.setblocking(1)
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
+ s.bind((self._host, self._port))
s.listen(5)
while True: