reuse reply context for unicast and broadcast

This commit is contained in:
hippoz 2023-01-11 18:59:16 +02:00
parent 55b1a9b760
commit 6d9f9c365c
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED

View file

@ -286,7 +286,7 @@ ssize_t bus_client_recv(bus_t *s, int i, void *buf, size_t n)
return status; return status;
} }
int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *sender_unique_name) int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *sender_unique_name, wire_context_t *reply_ctx)
{ {
uint32_t body_end = ctx->byte_cursor + msg->body_length; uint32_t body_end = ctx->byte_cursor + msg->body_length;
if (body_end >= ctx->data_len) { if (body_end >= ctx->data_len) {
@ -310,18 +310,13 @@ int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, ch
match_left--; match_left--;
if (match_rule_check(c->matches[j], msg, ctx) >= 0) { if (match_rule_check(c->matches[j], msg, ctx) >= 0) {
uint8_t reply_data[4096];
memset(reply_data, 0, 4096);
wire_context_t reply_ctx = {
.byte_cursor = 0,
.data = reply_data,
.data_len = 4096,
};
uint32_t previous_cursor = ctx->byte_cursor; uint32_t previous_cursor = ctx->byte_cursor;
TRYST(wire_compose_unicast_reply(&reply_ctx, ctx, msg, sender_unique_name)); TRYST(wire_compose_unicast_reply(reply_ctx, ctx, msg, sender_unique_name));
TRYST(send(c->fd, reply_data, reply_ctx.byte_cursor, 0)); TRYST(send(c->fd, reply_ctx->data, reply_ctx->byte_cursor, 0));
ctx->byte_cursor = previous_cursor; ctx->byte_cursor = previous_cursor;
if (match_left) {
memset(reply_ctx->data, 0, reply_ctx->data_len);
}
} }
} }
} }
@ -332,24 +327,11 @@ int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, ch
return 0; return 0;
} }
int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *target_name, char *sender_unique_name) int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *target_name, char *sender_unique_name, wire_context_t *reply_ctx)
{ {
bus_client_t *target = bus_name_find_client(s, target_name); bus_client_t *target = TRYPTR(bus_name_find_client(s, target_name));
if (!target) { TRYST(wire_compose_unicast_reply(reply_ctx, ctx, msg, sender_unique_name));
return -1; TRYST(send(target->fd, reply_ctx->data, reply_ctx->byte_cursor, 0));
}
uint8_t reply_data[8192];
memset(reply_data, 0, 8192);
wire_context_t reply_ctx = {
.byte_cursor = 0,
.data = reply_data,
.data_len = 8192,
};
TRYST(wire_compose_unicast_reply(&reply_ctx, ctx, msg, sender_unique_name));
TRYST(send(target->fd, reply_data, reply_ctx.byte_cursor, 0));
return 0; return 0;
} }
@ -647,12 +629,12 @@ int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx)
wire_message_field_t *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION]; wire_message_field_t *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION];
wire_message_field_t *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER]; wire_message_field_t *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER];
uint8_t reply_data[4096]; uint8_t reply_data[8192];
memset(reply_data, 0, 4096); memset(reply_data, 0, 8192);
wire_context_t reply_ctx = { wire_context_t reply_ctx = {
.byte_cursor = 0, .byte_cursor = 0,
.data = reply_data, .data = reply_data,
.data_len = 4096, .data_len = 8192,
}; };
if (msg.type == DBUS_MESSAGE_METHOD_CALL && destination_field->present && member_field->present && strcmp(destination_field->t.str, "org.freedesktop.DBus") == 0) { if (msg.type == DBUS_MESSAGE_METHOD_CALL && destination_field->present && member_field->present && strcmp(destination_field->t.str, "org.freedesktop.DBus") == 0) {
@ -677,12 +659,12 @@ int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx)
} }
if (destination_field->present) { if (destination_field->present) {
if (bus_unicast_message(s, &msg, ctx, destination_field->t.str, s->names[client->unique_name_index].name) < 0) { if (bus_unicast_message(s, &msg, ctx, destination_field->t.str, s->names[client->unique_name_index].name, &reply_ctx) < 0) {
_process_message_reply_error("xyz.hippoz.jitterbug.UnicastFailed"); _process_message_reply_error("xyz.hippoz.jitterbug.UnicastFailed");
goto end_nonfatal; goto end_nonfatal;
} }
} else { } else {
if (bus_broadcast_message(s, &msg, ctx, s->names[client->unique_name_index].name) < 0) { if (bus_broadcast_message(s, &msg, ctx, s->names[client->unique_name_index].name, &reply_ctx) < 0) {
_process_message_reply_error("xyz.hippoz.jitterbug.BroadcastFailed"); _process_message_reply_error("xyz.hippoz.jitterbug.BroadcastFailed");
goto end_nonfatal; goto end_nonfatal;
} }