mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-11-02 10:41:59 +00:00
fix send_msg_with_retry so it gets flood status from the send result
This commit is contained in:
@@ -114,31 +114,24 @@ class MessagingCommands(CommandHandlerBase):
|
|||||||
max_attempts=3, max_flood_attempts=2, flood_after=2, timeout=0, min_timeout=0
|
max_attempts=3, max_flood_attempts=2, flood_after=2, timeout=0, min_timeout=0
|
||||||
) -> Event:
|
) -> Event:
|
||||||
|
|
||||||
|
# try to get a 32 bytes key (for flood reset), fallback to 6
|
||||||
try:
|
try:
|
||||||
dst_bytes = _validate_destination(dst, prefix_length=32)
|
dst_bytes = _validate_destination(dst, prefix_length=32)
|
||||||
# with 32 bytes we can reset to flood
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# but if we can't, we'll assume we're flood
|
|
||||||
dst_bytes = _validate_destination(dst, prefix_length=6)
|
dst_bytes = _validate_destination(dst, prefix_length=6)
|
||||||
contact = self._get_contact_by_prefix(dst_bytes.hex())
|
contact = self._get_contact_by_prefix(dst_bytes.hex())
|
||||||
|
if not contact is None and len(dst_bytes) < 32 :
|
||||||
|
# if we have a contact then we can get full key
|
||||||
|
dst_bytes = _validate_destination(contact, prefix_length=32)
|
||||||
|
|
||||||
attempts = 0
|
attempts = 0
|
||||||
flood_attempts = 0
|
flood_attempts = 0
|
||||||
if not contact is None :
|
flood = False # by default consider we sent direct (will be overriden after send)
|
||||||
flood = contact["out_path_len"] == -1
|
|
||||||
if len(dst_bytes) < 32:
|
|
||||||
# if we have a contact, then we can get a 32 bytes key !
|
|
||||||
dst_bytes = _validate_destination(contact, prefix_length=32)
|
|
||||||
else:
|
|
||||||
# we can't know if we're flood without fetching all contacts
|
|
||||||
# if we have a full key (meaning we can reset path) consider direct
|
|
||||||
# else consider flood
|
|
||||||
flood = len(dst_bytes) < 32
|
|
||||||
logger.info(f"send_msg_with_retry: can't determine if flood, assume {flood}")
|
|
||||||
res = None
|
res = None
|
||||||
while attempts < max_attempts and res is None \
|
while attempts < max_attempts and res is None \
|
||||||
and (not flood or flood_attempts < max_flood_attempts):
|
and (not flood or flood_attempts < max_flood_attempts):
|
||||||
if attempts == flood_after and not flood : # change path to flood
|
if attempts == flood_after and not flood : # change path to flood
|
||||||
|
if len(dst_bytes) == 32: # can only reset with full key
|
||||||
logger.info("Resetting path")
|
logger.info("Resetting path")
|
||||||
rp_res = await self.reset_path(dst_bytes)
|
rp_res = await self.reset_path(dst_bytes)
|
||||||
if rp_res.type == EventType.ERROR:
|
if rp_res.type == EventType.ERROR:
|
||||||
@@ -148,6 +141,8 @@ class MessagingCommands(CommandHandlerBase):
|
|||||||
if not contact is None:
|
if not contact is None:
|
||||||
contact["out_path"] = ""
|
contact["out_path"] = ""
|
||||||
contact["out_path_len"] = -1
|
contact["out_path_len"] = -1
|
||||||
|
else:
|
||||||
|
logger.info("Don't have full key, can't reset path")
|
||||||
|
|
||||||
if attempts > 0:
|
if attempts > 0:
|
||||||
logger.info(f"Retry sending msg: {attempts + 1}")
|
logger.info(f"Retry sending msg: {attempts + 1}")
|
||||||
@@ -160,12 +155,13 @@ class MessagingCommands(CommandHandlerBase):
|
|||||||
flood_attempts += 1
|
flood_attempts += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
flood = result.payload["type"] == 1 # we can sync flood flag from result ...
|
||||||
exp_ack = result.payload["expected_ack"].hex()
|
exp_ack = result.payload["expected_ack"].hex()
|
||||||
timeout = result.payload["suggested_timeout"] / 1000 * 1.2 if timeout==0 else timeout
|
atimeout = result.payload["suggested_timeout"] / 1000 * 1.2 if timeout==0 else timeout
|
||||||
timeout = timeout if timeout > min_timeout else min_timeout
|
atimeout = atimeout if atimeout > min_timeout else min_timeout
|
||||||
res = await self.dispatcher.wait_for_event(EventType.ACK,
|
res = await self.dispatcher.wait_for_event(EventType.ACK,
|
||||||
attribute_filters={"code": exp_ack},
|
attribute_filters={"code": exp_ack},
|
||||||
timeout=timeout)
|
timeout=atimeout)
|
||||||
|
|
||||||
attempts = attempts + 1
|
attempts = attempts + 1
|
||||||
if flood :
|
if flood :
|
||||||
|
|||||||
Reference in New Issue
Block a user