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

Socket timeout #20

Open
wants to merge 4 commits into
base: v3.6-dev
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions src/dsf/connections/base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ def __init__(self, debug: bool = False, timeout: int = 3):
self.id = None
self.input = ""

def connect(self, init_message: client_init_messages.ClientInitMessage, socket_file: str):
def connect(self, init_message: client_init_messages.ClientInitMessage, socket_file: str, timeout: int = 0):
"""Establishes a connection to the given UNIX socket file"""

self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.connect(socket_file)
self.socket.setblocking(True)
if timeout == 0:
self.socket.setblocking(True)
else:
self.socket.setblocking(False)
self.socket.settimeout(timeout)
server_init_msg = server_init_message.ServerInitMessage.from_json(
json.loads(self.socket.recv(50).decode("utf8"))
)
Expand Down Expand Up @@ -75,7 +79,10 @@ def send(self, msg):
def receive(self, cls):
"""Receive a deserialized object from the server"""
json_string = self.receive_json()
return cls.from_json(json.loads(json_string))
try:
return cls.from_json(json.loads(json_string))
except Exception as e:
return None

def receive_response(self):
"""Receive a base response from the server"""
Expand Down Expand Up @@ -108,7 +115,7 @@ def receive_json(self) -> str:
part = self.socket.recv(BUFF_SIZE)
data += part
except socket.timeout:
pass
return None
except Exception as e:
raise e
# either 0 or end of data
Expand Down
4 changes: 2 additions & 2 deletions src/dsf/connections/intercept_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
self.auto_evaluate_expression = auto_evaluate_expression
self.priority_codes = priority_codes

def connect(self, socket_file: str = SOCKET_FILE): # noqa
def connect(self, timeout: int = 0, socket_file: str = SOCKET_FILE): # noqa
"""Establishes a connection to the given UNIX socket file"""
iim = client_init_messages.intercept_init_message(
self.interception_mode,
Expand All @@ -56,7 +56,7 @@ def connect(self, socket_file: str = SOCKET_FILE): # noqa
self.auto_flush,
self.auto_evaluate_expression
)
return super().connect(iim, socket_file)
return super().connect(iim, socket_file, timeout)

def receive_code(self) -> commands.code.Code:
"""Wait for a code to be intercepted and read it"""
Expand Down