Skip to content

Commit

Permalink
feat: add support for unix sockets to TCPTransport in erpc_python
Browse files Browse the repository at this point in the history
  • Loading branch information
brussee committed Nov 2, 2024
1 parent 49913b6 commit abd1d03
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions erpc_python/erpc/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,41 @@ 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:
self._serverThread = threading.Thread(target=self._serve)
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:
Expand Down

0 comments on commit abd1d03

Please sign in to comment.