🐛 Fixes event socket binary serialization buffer length

This commit is contained in:
Rune Harlyk
2025-07-10 20:44:04 +02:00
parent ec4c3fd98e
commit c8ee64d7f4
+27 -23
View File
@@ -105,35 +105,39 @@ void EventSocket::emit(const char *event, JsonVariant &payload, const char *orig
a.add(event); a.add(event);
a.add(payload); a.add(payload);
String out;
#if USE_MSGPACK #if USE_MSGPACK
serializeMsgPack(doc, out); static char out[512];
size_t len = serializeMsgPack(doc, out, sizeof(out));
if (len == 0 || len >= sizeof(out)) {
xSemaphoreGive(clientSubscriptionsMutex);
ESP_LOGE("EventSocket", "Message payload bigger than buffer (%d <= %d)", sizeof(out), len);
return;
}
const char *data = out;
#else #else
serializeJson(doc, out); static char out[1024];
size_t len = serializeJson(doc, out, sizeof(out));
if (len == 0 || len >= sizeof(out)) {
xSemaphoreGive(clientSubscriptionsMutex);
ESP_LOGE("EventSocket", "Message payload bigger than buffer (%d <= %d)", sizeof(out), len);
return;
}
const char *data = out;
#endif #endif
const char *msg = out.c_str(); auto sendTo = [&](int id) {
if (auto *c = _socket.getClient(id)) {
// if onlyToSameOrigin == true, send the message back to the origin send(c, data, len);
if (onlyToSameOrigin && originSubscriptionId > 0) { } else {
auto *client = _socket.getClient(originSubscriptionId); subscriptions.remove(id);
if (client) {
ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event,
client->remoteIP().toString().c_str(), msg);
send(client, msg, strlen(msg));
} }
} else { // else send the message to all other clients };
for (int subscription : client_subscriptions[event]) { if (onlyToSameOrigin && originSubscriptionId > 0) {
if (subscription == originSubscriptionId) continue; sendTo(originSubscriptionId);
auto *client = _socket.getClient(subscription); } else {
if (!client) { for (int id : subscriptions) {
subscriptions.remove(subscription); if (id != originSubscriptionId) sendTo(id);
continue;
}
ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event,
client->remoteIP().toString().c_str(), msg);
send(client, msg, strlen(msg));
} }
} }
xSemaphoreGive(clientSubscriptionsMutex); xSemaphoreGive(clientSubscriptionsMutex);