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

View file

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

View file

@ -167,7 +167,7 @@ class SSDPDatagram:
packet = cls._from_string(datagram.decode()) packet = cls._from_string(datagram.decode())
if packet is None: if packet is None:
raise UPnPError( 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]: for attr_name in packet._required_fields[packet._packet_type]:
if getattr(packet, attr_name, None) is None: if getattr(packet, attr_name, None) is None: