Skip to content

Commit

Permalink
Added support for dynamic link MTU to Channel and Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Jan 12, 2025
1 parent 93330d9 commit bde33e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion RNS/Buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class StreamDataMessage(MessageBase):
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
calculcated based on the value of OVERHEAD
Expand Down Expand Up @@ -215,6 +216,7 @@ def __init__(self, stream_id: int, channel: Channel):
self._stream_id = stream_id
self._channel = channel
self._eof = False
self._mdu = channel.mdu - StreamDataMessage.OVERHEAD

def write(self, __b: bytes) -> int | None:
try:
Expand Down
9 changes: 6 additions & 3 deletions RNS/Channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def send(self, message: MessageBase) -> Envelope:
return envelope

@property
def MDU(self):
def mdu(self):
"""
Maximum Data Unit: the number of bytes available
for a message to consume in a single send. This
Expand All @@ -611,7 +611,10 @@ def MDU(self):
: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):
Expand Down Expand Up @@ -639,7 +642,7 @@ def resend(self, packet: RNS.Packet) -> RNS.Packet:

@property
def mdu(self):
return self.link.MDU
return self.link.mdu

@property
def rtt(self):
Expand Down
4 changes: 2 additions & 2 deletions RNS/Link.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def request(self, path, data = None, response_callback = None, failed_callback =
if timeout == None:
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)
packet_receipt = request_packet.send()

Expand Down Expand Up @@ -775,7 +775,7 @@ def handle_request(self, request_id, unpacked_request):
if response != None:
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()
else:
response_resource = RNS.Resource(packed_response, self, request_id = request_id, is_response = True)
Expand Down

0 comments on commit bde33e7

Please sign in to comment.