From cf4624ee8eb56f5d9d3e9a064407415ef578b696 Mon Sep 17 00:00:00 2001 From: Ian Good Date: Sat, 12 Aug 2023 23:13:57 -0400 Subject: [PATCH] Add MTU size to UdpConfig --- swimprotocol/udp/__init__.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/swimprotocol/udp/__init__.py b/swimprotocol/udp/__init__.py index 17b0c61..6df1bc5 100644 --- a/swimprotocol/udp/__init__.py +++ b/swimprotocol/udp/__init__.py @@ -48,6 +48,7 @@ def __init__(self, *, bind_host: Optional[str] = None, default_host: Optional[str] = None, default_port: Optional[int] = None, discovery: bool = False, + mtu_size: int = 1500, **kwargs: Any) -> None: address_parser = AddressParser( default_host=default_host, @@ -58,6 +59,7 @@ def __init__(self, *, bind_host: Optional[str] = None, self.bind_host: Final = bind_host self.bind_port: Final = bind_port self.address_parser: Final = address_parser + self.mtu_size: Final = mtu_size @classmethod def add_arguments(cls, parser: ArgumentParser, *, @@ -213,7 +215,7 @@ async def _run_send(self, thread_pool: ThreadPoolExecutor, packet_data = await loop.run_in_executor( thread_pool, self.udp_pack.pack, packet) address = self.address_parser.parse(member.name) - if len(packet_data) <= 1500: + if len(packet_data) <= self.config.mtu_size: udp_transport.sendto(packet_data, (address.host, address.port)) else: asyncio.create_task(self._tcp_send(packet_data, address)) @@ -225,20 +227,6 @@ async def _tcp_send(self, packet_data: bytes, address: Address) -> None: with closing(tcp_transport): tcp_transport.write(packet_data) - @classmethod - def add_arguments(cls, name: str, parser: ArgumentParser, *, - prefix: str = '--udp') -> None: - group = parser.add_argument_group(f'{name} options') - group.add_argument(f'{prefix}-bind', metavar='INTERFACE', - dest='swim_udp_bind', - help='The bind IP address.') - group.add_argument(f'{prefix}-host', metavar='NAME', - dest='swim_udp_host', - help='The default remote hostname.') - group.add_argument(f'{prefix}-port', metavar='NUM', type=int, - dest='swim_udp_port', - help='The default port number.') - class _BaseProtocol(Subtasks):