Added support for dynamic link MTU to Channel and Buffer

This commit is contained in:
Mark Qvist 2025-01-12 23:26:18 +01:00
parent 93330d96a0
commit bde33e7d84
3 changed files with 11 additions and 6 deletions

View File

@ -45,7 +45,8 @@ class StreamDataMessage(MessageBase):
The stream id is limited to 2 bytes - 2 bit The stream id is limited to 2 bytes - 2 bit
""" """
MAX_DATA_LEN = RNS.Link.MDU - 2 - 6 # 2 for stream data message header, 6 for channel envelope OVERHEAD = 2 + 6 # 2 for stream data message header, 6 for channel envelope
MAX_DATA_LEN = RNS.Link.MDU - OVERHEAD
""" """
When the Buffer package is imported, this value is When the Buffer package is imported, this value is
calculcated based on the value of OVERHEAD calculcated based on the value of OVERHEAD
@ -215,6 +216,7 @@ class RawChannelWriter(RawIOBase, AbstractContextManager):
self._stream_id = stream_id self._stream_id = stream_id
self._channel = channel self._channel = channel
self._eof = False self._eof = False
self._mdu = channel.mdu - StreamDataMessage.OVERHEAD
def write(self, __b: bytes) -> int | None: def write(self, __b: bytes) -> int | None:
try: try:

View File

@ -602,7 +602,7 @@ class Channel(contextlib.AbstractContextManager):
return envelope return envelope
@property @property
def MDU(self): def mdu(self):
""" """
Maximum Data Unit: the number of bytes available Maximum Data Unit: the number of bytes available
for a message to consume in a single send. This for a message to consume in a single send. This
@ -611,7 +611,10 @@ class Channel(contextlib.AbstractContextManager):
:return: number of bytes available :return: number of bytes available
""" """
return self._outlet.mdu - 6 # sizeof(msgtype) + sizeof(length) + sizeof(sequence) mdu = self._outlet.mdu - 6 # sizeof(msgtype) + sizeof(length) + sizeof(sequence)
if mdu > 0xFFFF:
mdu = 0xFFFF
return mdu
class LinkChannelOutlet(ChannelOutletBase): class LinkChannelOutlet(ChannelOutletBase):
@ -639,7 +642,7 @@ class LinkChannelOutlet(ChannelOutletBase):
@property @property
def mdu(self): def mdu(self):
return self.link.MDU return self.link.mdu
@property @property
def rtt(self): def rtt(self):

View File

@ -423,7 +423,7 @@ class Link:
if timeout == None: if timeout == None:
timeout = self.rtt * self.traffic_timeout_factor + RNS.Resource.RESPONSE_MAX_GRACE_TIME*1.125 timeout = self.rtt * self.traffic_timeout_factor + RNS.Resource.RESPONSE_MAX_GRACE_TIME*1.125
if len(packed_request) <= Link.MDU: if len(packed_request) <= self.mdu:
request_packet = RNS.Packet(self, packed_request, RNS.Packet.DATA, context = RNS.Packet.REQUEST) request_packet = RNS.Packet(self, packed_request, RNS.Packet.DATA, context = RNS.Packet.REQUEST)
packet_receipt = request_packet.send() packet_receipt = request_packet.send()
@ -775,7 +775,7 @@ class Link:
if response != None: if response != None:
packed_response = umsgpack.packb([request_id, response]) packed_response = umsgpack.packb([request_id, response])
if len(packed_response) <= Link.MDU: if len(packed_response) <= self.mdu:
RNS.Packet(self, packed_response, RNS.Packet.DATA, context = RNS.Packet.RESPONSE).send() RNS.Packet(self, packed_response, RNS.Packet.DATA, context = RNS.Packet.RESPONSE).send()
else: else:
response_resource = RNS.Resource(packed_response, self, request_id = request_id, is_response = True) response_resource = RNS.Resource(packed_response, self, request_id = request_id, is_response = True)