Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MTU size to UdpConfig #30

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Changes from all commits
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
18 changes: 3 additions & 15 deletions swimprotocol/udp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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, *,
Expand Down Expand Up @@ -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))
Expand All @@ -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):

Expand Down