Faster gateway discovery #18

Merged
jackrobison merged 7 commits from fast-discover into master 2019-10-25 22:12:41 +02:00
3 changed files with 8 additions and 9 deletions
Showing only changes of commit 1b25c009ca - Show all commits

View file

@ -184,8 +184,9 @@ class Gateway:
return None
@classmethod
async def _gateway_from_igd_args(cls, lan_address: str, gateway_address: str, timeout: int = 30,
igd_args: Optional[typing.Dict[str, typing.Union[int, str]]] = None,
async def _gateway_from_igd_args(cls, lan_address: str, gateway_address: str,
igd_args: typing.Dict[str, typing.Union[int, str]],
timeout: int = 30,
loop: Optional[asyncio.AbstractEventLoop] = None) -> 'Gateway':
datagram = await m_search(lan_address, gateway_address, igd_args, timeout, loop, set())
gateway = await cls._try_gateway_from_ssdp(datagram, lan_address, gateway_address, loop)
@ -208,7 +209,7 @@ class Gateway:
gateway = await cls._try_gateway_from_ssdp(datagram, lan_address, gateway_address, loop)
if gateway:
return gateway
else:
elif datagram.location:
ignored.add(datagram.location)
finally:
ssdp_proto.disconnect()
@ -219,7 +220,7 @@ class Gateway:
loop: Optional[asyncio.AbstractEventLoop] = None) -> 'Gateway':
loop = loop or asyncio.get_event_loop()
if igd_args:
return await cls._gateway_from_igd_args(lan_address, gateway_address, timeout, igd_args, loop)
return await cls._gateway_from_igd_args(lan_address, gateway_address, igd_args, timeout, loop)
try:
return await asyncio.wait_for(loop.create_task(
cls._discover_gateway(lan_address, gateway_address, timeout, loop)

View file

@ -80,12 +80,10 @@ class SSDPProtocol(MulticastProtocol):
if not fut.done():
fut.set_exception(UPnPError("SSDP transport not connected"))
return
if fut.done():
return
assert packet.st is not None
self._pending_searches.append(
PendingSearch(address, packet.st, fut)
)
self.transport.sendto(packet.encode().encode(), (SSDP_IP_ADDRESS, SSDP_PORT))
# also send unicast

View file

@ -167,7 +167,7 @@ class SSDPDatagram:
packet = cls._from_string(datagram.decode())
if packet is None:
raise UPnPError(
"failed to decode datagram: {}".format(binascii.hexlify(datagram))
f"failed to decode datagram: {binascii.hexlify(datagram).decode()}"
)
for attr_name in packet._required_fields[packet._packet_type]:
if getattr(packet, attr_name, None) is None: