From b644bd12d96b06492c86fa482e6ccd99d52a5d52 Mon Sep 17 00:00:00 2001 From: Florent Date: Fri, 5 Sep 2025 23:36:02 +0200 Subject: [PATCH] use dstType instead of contact --- src/meshcore/commands/base.py | 6 +++++- src/meshcore/commands/messaging.py | 10 ++++++---- src/meshcore/meshcore.py | 1 + 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/meshcore/commands/base.py b/src/meshcore/commands/base.py index 8f416b1..cf4ae16 100644 --- a/src/meshcore/commands/base.py +++ b/src/meshcore/commands/base.py @@ -76,6 +76,10 @@ class CommandHandlerBase: def set_dispatcher(self, dispatcher: EventDispatcher) -> None: self.dispatcher = dispatcher + def set_contact_getter_by_prefix(self, func: Callable[[str], Optional[Dict[str,Any]]] + )-> None: + self._get_contact_by_prefix = func + async def send( self, data: bytes, @@ -166,4 +170,4 @@ class CommandHandlerBase: actual_timeout = timeout if timeout is not None and timeout > 0 else result.payload.get("suggested_timeout", 4000) / 800.0 self._reader.register_binary_request(pubkey_prefix.hex(), exp_tag, request_type, actual_timeout) - return result \ No newline at end of file + return result diff --git a/src/meshcore/commands/messaging.py b/src/meshcore/commands/messaging.py index 0f6b554..31105be 100644 --- a/src/meshcore/commands/messaging.py +++ b/src/meshcore/commands/messaging.py @@ -79,11 +79,14 @@ class MessagingCommands(CommandHandlerBase): ) return await self.send(data, [EventType.MSG_SENT, EventType.ERROR]) - async def send_msg_reliable( - self, contact, msg: str, timestamp: Optional[int] = None, + async def send_msg_with_retry ( + self, dst: DestinationType, msg: str, timestamp: Optional[int] = None, max_attempts=3, flood_after=2, timeout=0 ) -> Event: + dst_bytes = _validate_destination(dst) + contact = self._get_contact_by_prefix(dst_bytes.hex()) + attempts = 0 res = None while attempts < max_attempts and res is None: @@ -101,8 +104,7 @@ class MessagingCommands(CommandHandlerBase): result = await self.send_msg(contact, msg, timestamp, attempt=attempts) if result.type == EventType.ERROR: - print(f"⚠️ Failed to send message: {result.payload}") - return None + logger.error(f"⚠️ Failed to send message: {result.payload}") exp_ack = result.payload["expected_ack"].hex() timeout = result.payload["suggested_timeout"] / 1000 * 1.2 if timeout==0 else timeout diff --git a/src/meshcore/meshcore.py b/src/meshcore/meshcore.py index 84e533f..910f608 100644 --- a/src/meshcore/meshcore.py +++ b/src/meshcore/meshcore.py @@ -37,6 +37,7 @@ class MeshCore: self._reader = MessageReader(self.dispatcher) self.commands = CommandHandler(default_timeout=default_timeout) + self.commands.set_contact_getter_by_prefix(self.get_contact_by_key_prefix) # Set up logger if debug: