feat: add support for unix sockets to TCPTransport in erpc_python#437
Open
brussee wants to merge 2 commits intoEmbeddedRPC:developfrom
Open
feat: add support for unix sockets to TCPTransport in erpc_python#437brussee wants to merge 2 commits intoEmbeddedRPC:developfrom
brussee wants to merge 2 commits intoEmbeddedRPC:developfrom
Conversation
brussee
commented
Nov 2, 2024
- feat: add support for unix sockets to TCPTransport in erpc_python
- chore: organize imports
abd1d03 to
72663f0
Compare
Contributor
|
Hi, we reviewed your PR and really like the idea of adding a UNIX socket to the transport layers. After discussing with @MichalPrincNXP, we concluded that since UNIX sockets do not use TCP, it would be more appropriate to create a new transport layer. Below is the code that incorporates your changes into a new Note that the code has been slightly modified, as we are also preparing an update to the Python implementation. # Import
from typing import Optional
# Transport
class UnixSocketTransport(FramedTransport):
def __init__(self, unix_socket: str, is_server: bool):
super(UnixSocketTransport, self).__init__()
self._unix_socket = unix_socket
self._is_server = is_server
self._sock: Optional[socket.socket] = None
if self._is_server:
self._server_thread = threading.Thread(target=self._serve)
self._server_thread.daemon = True
self._server_thread.start()
self._server_sock_event_start = threading.Event()
else:
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._sock.connect(self._unix_socket)
def _serve(self):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.setblocking(True)
s.bind(self._unix_socket)
while True:
self._sock, _ = s.accept()
self._server_sock_event_start.set()
def close(self):
if self._is_server:
self._server_sock_event_start.clear()
if self._sock:
self._sock.shutdown(socket.SHUT_RDWR)
self._sock.close()
self._sock = None
def _base_send(self, data: bytes):
if self._is_server:
self._server_sock_event_start.wait()
if self._sock:
self._sock.sendall(data)
def _base_receive(self, count: int) -> bytes:
if self._is_server:
self._server_sock_event_start.wait()
if self._sock:
remaining = count
result = bytes()
while remaining:
data = self._sock.recv(remaining)
if len(data) == 0:
self.close()
raise ConnectionClosed()
result += data
remaining -= len(data)
return result
return bytes() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.