Skip to content
Open
Changes from 1 commit
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
44 changes: 32 additions & 12 deletions pyps4/ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import re
import socket
import logging

UDP_IP = '0.0.0.0'
UDP_IP = ''
UDP_PORT = 0

DDP_PORT = 987
DDP_VERSION = '00020020'

_LOGGER = logging.getLogger(__name__)

def get_ddp_message(msg_type, data=None):
"""Get DDP message."""
Expand Down Expand Up @@ -66,18 +68,36 @@ def get_ddp_launch_message(credential):

def _send_recv_msg(host, broadcast, msg, receive=True):
"""Send a ddp message and receive the response."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
sock.settimeout(3.0)

if broadcast:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
host = '255.255.255.255'

sock.sendto(msg.encode('utf-8'), (host, DDP_PORT))

if receive:
return sock.recvfrom(1024)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except OSError as error:
_LOGGER.error('failed to create socket, %s', error)
return
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm not sure if it is a good idea to return siliently. The errors would not be catched anymore.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

continuing just seems to break more, and the error is already reported, so I don't know what a better alternative would be.

basicly what you are trying is not working and you are informed why, in my opinion that seems better than breaking with an unhandled exception

try:
sock.bind((UDP_IP, UDP_PORT))
sock.settimeout(3.0)
except OSError as error:
_LOGGER.error('failed to bind socket %s:%s, %s', UDP_IP, UDP_PORT, error)
sock.close()
return

try:
if broadcast:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this should be removed form the try, except block ... maybe in another

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what part are you referring to here?

sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
host = '255.255.255.255'

_LOGGER.debug('send_recv_msg %s [%s]', host, msg)
sock.sendto(msg.encode('utf-8'), (host, DDP_PORT))

if receive:
return sock.recvfrom(1024)

sock.close()
except OSError as error:
_LOGGER.error('failed to send data using socket, %s', error)
sock.close()
return


def _send_msg(host, broadcast, msg):
Expand Down