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

feat: add support for unix sockets to TCPTransport in erpc_python #437

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
33 changes: 22 additions & 11 deletions erpc_python/erpc/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
#
# SPDX-License-Identifier: BSD-3-Clause

import struct
import socket
import struct
import threading
from socket import SHUT_RDWR
import time

from .crc16 import Crc16
from .client import RequestError
import time
from .crc16 import Crc16

try:
import serial
Expand Down Expand Up @@ -138,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 arguments"
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 All @@ -170,7 +181,7 @@ def _serve(self):
def close(self):
if self._isServer:
self._serverSockEventStart.clear()
self._sock.shutdown(SHUT_RDWR)
self._sock.shutdown(socket.SHUT_RDWR)
self._sock.close()
self._sock = None

Expand Down