🐛 Fixes event socket binary serialization buffer length
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user