From 437d511703324db90abe177671f6e97b4f9bf617 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Sun, 13 Jul 2025 20:47:52 -0400 Subject: [PATCH] Fix messages combining by sending size first --- components/rpc/TCPServer.cpp | 15 ++++++++++++--- components/rpc/include/TCPServer.h | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/components/rpc/TCPServer.cpp b/components/rpc/TCPServer.cpp index 012f370..b20ad9d 100644 --- a/components/rpc/TCPServer.cpp +++ b/components/rpc/TCPServer.cpp @@ -32,7 +32,7 @@ TCPServer::TCPServer(const int port, const std::shared_ptrm_rx_queue = rx_queue; this->m_server_sock = 0; - xTaskCreate(tcp_server_task, "tcp_accept_server", 2048, this, 5, &this->m_task); + xTaskCreate(tcp_server_task, "tcp_accept_server", 3072, this, 5, &this->m_task); xTaskCreate(socket_monitor_thread, "tcp_rx", 4096, this, 5, &this->m_rx_task); } @@ -148,7 +148,15 @@ TCPServer::~TCPServer() { auto buffer = std::make_unique>(); buffer->resize(MAX_RX_BUFFER_SIZE); - int len = recv(sock, buffer->data(), MAX_RX_BUFFER_SIZE, 0); // temp: for the null terminator + uint32_t msg_size = 0; + recv(sock, &msg_size, 4, MSG_WAITALL); + if (msg_size < 1 || msg_size > 512) { + continue; + } + + printf("Message size: %ld\n", msg_size); + + int len = recv(sock, buffer->data(), msg_size, MSG_WAITALL); if (len < 0) { printf("Error occurred during receiving: errno %d\n", errno); to_remove.emplace_back(sock); @@ -203,7 +211,7 @@ bool TCPServer::authenticate_client(int sock) { return 0; } -int TCPServer::send_msg(char *buffer, size_t length) const { +int TCPServer::send_msg(char *buffer, uint32_t length) const { // todo: should we assign a unique rank to each pc? if (!is_network_connected()) { @@ -212,6 +220,7 @@ int TCPServer::send_msg(char *buffer, size_t length) const { for (const auto client_sock : m_clients) { std::cout << "sending tcp" << std::endl; + send(client_sock, &length, 4, 0); send(client_sock, buffer, length, 0); } diff --git a/components/rpc/include/TCPServer.h b/components/rpc/include/TCPServer.h index ee70333..7a9cd03 100644 --- a/components/rpc/include/TCPServer.h +++ b/components/rpc/include/TCPServer.h @@ -19,7 +19,7 @@ class TCPServer : IRPCServer { public: TCPServer(int port, const std::shared_ptr>>& rx_queue); ~TCPServer(); - int send_msg(char* buffer, size_t length) const; + int send_msg(char* buffer, uint32_t length) const; private: bool authenticate_client(int client_sock);