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

Fix poll/select for Windows #690

Merged
merged 5 commits into from
Mar 15, 2023
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
22 changes: 14 additions & 8 deletions pychromecast/socket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
CONNECTION_STATUS_FAILED_RESOLVE = "FAILED_RESOLVE"
# The socket connection was lost and needs to be retried
CONNECTION_STATUS_LOST = "LOST"
# Check for select poll method
SELECT_HAS_POLL = hasattr(select,"poll")

HB_PING_TIME = 10
HB_PONG_TIME = 10
Expand Down Expand Up @@ -565,15 +567,19 @@ def run_once(self, timeout=POLL_TIME_NON_BLOCKING):

# poll the socket, as well as the socketpair to allow us to be interrupted
rlist = [self.socket, self.socketpair[0]]
# Map file descriptors to socket objects because select.select does not support fd > 1024
# https://stackoverflow.com/questions/14250751/how-to-increase-filedescriptors-range-in-python-select
fd_to_socket = {rlist_item.fileno(): rlist_item for rlist_item in rlist}
try:
poll_obj = select.poll()
for poll_fd in rlist:
poll_obj.register(poll_fd, select.POLLIN)
poll_result = poll_obj.poll(timeout * 1000) # timeout in milliseconds
can_read = [fd_to_socket[fd] for fd, _status in poll_result]
if SELECT_HAS_POLL == True:
# Map file descriptors to socket objects because select.select does not support fd > 1024
# https://stackoverflow.com/questions/14250751/how-to-increase-filedescriptors-range-in-python-select
fd_to_socket = {rlist_item.fileno(): rlist_item for rlist_item in rlist}

poll_obj = select.poll()
for poll_fd in rlist:
poll_obj.register(poll_fd, select.POLLIN)
poll_result = poll_obj.poll(timeout * 1000) # timeout in milliseconds
can_read = [fd_to_socket[fd] for fd, _status in poll_result]
else:
can_read, _, _ = select.select(rlist, [], [], timeout)
except (ValueError, OSError) as exc:
self.logger.error(
"[%s(%s):%s] Error in select call: %s",
Expand Down