From cb7bdb310fe009ddbd54ac8ad14961f2208774d5 Mon Sep 17 00:00:00 2001 From: Ben Batt Date: Mon, 20 Jul 2026 21:30:52 +1000 Subject: [PATCH] Refactor common logic from pushPostToClient() and pushTopicToClient() into pushPostInternal() --- examples/simple_room_server/MyMesh.cpp | 64 ++++++++------------------ examples/simple_room_server/MyMesh.h | 1 + 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 8e7d39bdf..51e107b5e 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -68,68 +68,40 @@ void MyMesh::storePost(const mesh::Identity &author, const char *postData) { void MyMesh::pushPostToClient(ClientInfo *client, PostInfo &post) { MESH_DEBUG_PRINTLN("room.post: pushPostToClient text=%s", post.text); - int len = 0; - memcpy(&reply_data[len], &post.post_timestamp, 4); - len += 4; // this is a PAST timestamp... but should be accepted by client - - uint8_t attempt; - getRNG()->random(&attempt, 1); // need this for re-tries, so packet hash (and ACK) will be different - reply_data[len++] = (TXT_TYPE_SIGNED_PLAIN << 2) | (attempt & 3); // 'signed' plain text - - // encode prefix of post.author.pub_key - memcpy(&reply_data[len], post.author.pub_key, 4); - len += 4; // just first 4 bytes - - int text_len = strlen(post.text); - memcpy(&reply_data[len], post.text, text_len); - len += text_len; - - // calc expected ACK reply - mesh::Utils::sha256((uint8_t *)&client->extra.room.pending_ack, 4, reply_data, len, client->id.pub_key, PUB_KEY_SIZE); - client->extra.room.push_post_timestamp = post.post_timestamp; - - auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, client->shared_secret, reply_data, len); - if (reply) { - if (client->out_path_len == OUT_PATH_UNKNOWN) { - unsigned long delay_millis = 0; - sendFloodScoped(default_scope, reply, delay_millis, _prefs.path_hash_mode + 1); // REVISIT - client->extra.room.ack_timeout = futureMillis(PUSH_ACK_TIMEOUT_FLOOD); - } else { - sendDirect(reply, client->out_path, client->out_path_len); - - uint8_t path_hash_count = client->out_path_len & 63; - client->extra.room.ack_timeout = futureMillis(PUSH_TIMEOUT_BASE + PUSH_ACK_TIMEOUT_FACTOR * (path_hash_count + 1)); - } - _num_post_pushes++; // stats - } else { - client->extra.room.pending_ack = 0; - MESH_DEBUG_PRINTLN("Unable to push post to client"); - } + pushPostInternal(client, post.post_timestamp, post.author, nullptr, post.text); } void MyMesh::pushTopicToClient(ClientInfo *client) { + pushPostInternal(client, topic_timestamp, self_id, "Topic: ", topic); +} + +void MyMesh::pushPostInternal(ClientInfo* client, uint32_t timestamp, const mesh::Identity& author, + const char* prefix, const char* text) { int len = 0; - memcpy(&reply_data[len], &topic_timestamp, 4); + memcpy(&reply_data[len], ×tamp, 4); len += 4; // this is a PAST timestamp... but should be accepted by client uint8_t attempt; getRNG()->random(&attempt, 1); // need this for re-tries, so packet hash (and ACK) will be different reply_data[len++] = (TXT_TYPE_SIGNED_PLAIN << 2) | (attempt & 3); // 'signed' plain text - // encode prefix of self_id.pub_key - memcpy(&reply_data[len], self_id.pub_key, 4); + // encode prefix of author.pub_key + memcpy(&reply_data[len], author.pub_key, 4); len += 4; // just first 4 bytes - memcpy(&reply_data[len], "Topic: ", 7); - len += 7; + if (prefix) { + int prefix_len = strlen(prefix); + memcpy(&reply_data[len], prefix, prefix_len); + len += prefix_len; + } - int text_len = strlen(topic); - memcpy(&reply_data[len], topic, text_len); + int text_len = strlen(text); + memcpy(&reply_data[len], text, text_len); len += text_len; // calc expected ACK reply mesh::Utils::sha256((uint8_t *)&client->extra.room.pending_ack, 4, reply_data, len, client->id.pub_key, PUB_KEY_SIZE); - client->extra.room.push_post_timestamp = topic_timestamp; + client->extra.room.push_post_timestamp = timestamp; auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, client->shared_secret, reply_data, len); if (reply) { @@ -146,7 +118,7 @@ void MyMesh::pushTopicToClient(ClientInfo *client) { _num_post_pushes++; // stats } else { client->extra.room.pending_ack = 0; - MESH_DEBUG_PRINTLN("Unable to push topic to client"); + MESH_DEBUG_PRINTLN("Unable to push post to client"); } } diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index c396ede02..73fe5ab23 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -127,6 +127,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { void storePost(const mesh::Identity& author, const char* postData); void pushPostToClient(ClientInfo* client, PostInfo& post); void pushTopicToClient(ClientInfo* client); + void pushPostInternal(ClientInfo* client, uint32_t timestamp, const mesh::Identity& author, const char* prefix, const char* text); uint8_t getUnsyncedCount(ClientInfo* client); bool processAck(const uint8_t *data); mesh::Packet* createSelfAdvert();