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

refactor: allow setting source #28

Merged
merged 1 commit into from
Mar 16, 2024
Merged
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
8 changes: 7 additions & 1 deletion stupidArtnet/StupidArtnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class StupidArtnet():
UDP_PORT = 6454

def __init__(self, target_ip='127.0.0.1', universe=0, packet_size=512, fps=30,
even_packet_size=True, broadcast=False, artsync=False):
even_packet_size=True, broadcast=False, source_address=None, artsync=False):
"""Initializes Art-Net Client.

Args:
Expand Down Expand Up @@ -59,6 +59,12 @@ def __init__(self, target_ip='127.0.0.1', universe=0, packet_size=512, fps=30,
self.socket_client.setsockopt(
socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

# Allow speciying the origin interface
if source_address:
self.socket_client.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket_client.bind(source_address)

Comment on lines +62 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handling of the source_address parameter within the constructor is correctly implemented. By checking if source_address is provided and then using socket.SO_REUSEADDR along with socket.bind(), the code ensures that the socket is bound to the specified address if provided. This is a crucial step for enabling the desired functionality of specifying the origin interface for the Art-Net client.

However, it's important to consider the implications of using socket.SO_REUSEADDR. This option allows multiple sockets to bind to the same port number provided they each bind to a different local address. This is generally used for allowing a server to re-bind to a port that is in the TIME_WAIT state after the server has been restarted. While this is useful in many scenarios, it's essential to ensure that this behavior is desired and understood, as it can have security implications in certain contexts.

Additionally, error handling for the bind() call should be considered. Network operations can fail for various reasons, including permissions issues, the specified address not being available on the machine, etc. It would be beneficial to wrap the bind() call in a try-except block to catch potential exceptions and handle them gracefully, possibly logging the error or notifying the user if the binding fails.

Consider adding error handling around the bind() call to gracefully handle potential exceptions. This could improve the robustness of the application and provide clearer feedback to the user in case of issues.

# Timer
self.fps = fps
self.__clock = None
Expand Down
Loading