Skip to content

Commit

Permalink
Fix type hints for asyncio protocols and transports.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaine committed Oct 18, 2022
1 parent d9589a5 commit 2aed57f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/aioice/ice.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ async def get_component_candidates(
except OSError as exc:
self.__log_info("Could not bind to %s - %s", address, exc)
continue
protocol = cast(StunProtocol, protocol)
host_protocols.append(protocol)

# add host candidate
Expand Down Expand Up @@ -911,7 +910,6 @@ async def get_component_candidates(
ssl=self.turn_ssl,
transport=self.turn_transport,
)
protocol = cast(StunProtocol, protocol)
self._protocols.append(protocol)

# add relayed candidate
Expand Down
2 changes: 1 addition & 1 deletion src/aioice/mdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ async def create_mdns_protocol() -> MDnsProtocol:
sock=rx_sock,
)

return cast(MDnsProtocol, protocol)
return protocol
21 changes: 18 additions & 3 deletions src/aioice/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
import socket
import struct
import time
from typing import Any, Callable, Dict, List, Optional, Text, Tuple, Union, cast
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Text,
Tuple,
TypeVar,
Union,
cast,
)

from . import stun
from .utils import random_transaction_id
Expand All @@ -17,6 +28,8 @@
UDP_TRANSPORT = 0x11000000
UDP_SOCKET_BUFFER_SIZE = 262144

_ProtocolT = TypeVar("_ProtocolT", bound=asyncio.BaseProtocol)


def is_channel_data(data: bytes) -> bool:
return (data[0] & 0xC0) == 0x40
Expand Down Expand Up @@ -371,19 +384,21 @@ async def _connect(self) -> None:


async def create_turn_endpoint(
protocol_factory: Callable,
protocol_factory: Callable[[], _ProtocolT],
server_addr: Tuple[str, int],
username: Optional[str],
password: Optional[str],
lifetime: int = DEFAULT_ALLOCATION_LIFETIME,
channel_refresh_time: int = DEFAULT_CHANNEL_REFRESH_TIME,
ssl: bool = False,
transport: str = "udp",
) -> Tuple[TurnTransport, asyncio.Protocol]:
) -> Tuple[TurnTransport, _ProtocolT]:
"""
Create datagram connection relayed over TURN.
"""
loop = asyncio.get_event_loop()
inner_protocol: asyncio.BaseProtocol
inner_transport: asyncio.BaseTransport
if transport == "tcp":
inner_transport, inner_protocol = await loop.create_connection(
lambda: TurnClientTcpProtocol(
Expand Down

0 comments on commit 2aed57f

Please sign in to comment.