From 9a9d001f040115a371b2362536c6f99559127f66 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 17 Jan 2023 03:09:38 +0200 Subject: [PATCH] don't do unnecessary work --- Makefile | 4 ++-- server.c | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index cbf9dc6..00a1291 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ CC?=gcc CFLAGS_DEV=-DJB_PRESET_DEBUG -ggdb -fsanitize=address -Og -CFLAGS_RELEASE=-O2 -ggdb +CFLAGS_RELEASE=-O2 -fgraphite-identity -funroll-loops -ftree-vectorize -flto CFLAGS+=-pipe -Wall -Wextra -Wshadow -pedantic -std=c99 -CFLAGS+=$(CFLAGS_DEV) +CFLAGS+=$(CFLAGS_RELEASE) jitterbug: util.c wire.c match.c server.c main.c $(CC) $(CFLAGS) -o $@ $^ diff --git a/server.c b/server.c index 4c70778..0cc5e28 100644 --- a/server.c +++ b/server.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -227,12 +228,11 @@ int bus_client_assign_unique_name(bus_t *s, int i) int bus_client_assign_own_name(bus_t *s, int i, char *name) { - bus_client_t *c = &s->clients[i]; if (!name || *name == ':' || *name == '\0') { return -1; } - int name_index = TRYST(bus_name_add(s, name, i)); + TRYST(bus_name_add(s, name, i)); return 0; } @@ -316,7 +316,6 @@ int bus_broadcast_message(bus_t *s, int sender_index, wire_message_t *msg, wire_ uint32_t previous_cursor = ctx->byte_cursor; if (match_rule_check(s, sender_index, c->matches[j], msg, ctx) >= 0) { - printf("---------> sending to %d\n", c->fd); TRYST(wire_compose_unicast_reply(reply_ctx, ctx, msg, s->names[sender_client->unique_name_index].name)); TRYST(send(c->fd, reply_ctx->data, reply_ctx->byte_cursor, 0)); // TODO? @@ -442,7 +441,7 @@ int handle_request_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ct } char *name = TRYPTR(wire_get_name_string(ctx)); - uint32_t flags = *(uint32_t*)TRYPTR(wire_get_u32(ctx)); // unused flags + TRYPTR(wire_get_u32(ctx)); // unused flags char *name_str = TRYPTR(string_dup(name)); @@ -799,7 +798,7 @@ int bus_client_drain_messages(bus_t *s, int ci, uint8_t *data, uint32_t data_len return 0; } -#define _client_die(m) do { bus_client_error(s,i,m); continue; } while(0) +#define _client_die(m) do { bus_client_error(s,i,m); goto done; } while(0) int bus_turn(bus_t *s) { static const char agree_unix_fd[] = "ERROR\r\n"; @@ -848,6 +847,8 @@ int bus_turn(bus_t *s) if (fd == s->sock_fd) { // new connection int accepted_fd = TRYST(accept(s->sock_fd, NULL, NULL)); + int flags = TRYST(fcntl(accepted_fd, F_GETFL, 0)); + TRYST(fcntl(accepted_fd, F_SETFL, flags | O_NONBLOCK)); if (bus_client_add(s, accepted_fd) == -1) { close(accepted_fd); @@ -857,7 +858,6 @@ int bus_turn(bus_t *s) // We add padding. See above. char data[data_buffer_len + data_buffer_padding]; - memset(data, 0, sizeof(data)); ssize_t bytes = recv(fd, data, data_buffer_len, 0); if (bytes <= 0) { // error during recv() OR client disconnected, disconnect the client @@ -926,6 +926,9 @@ int bus_turn(bus_t *s) _client_die("bad state"); } break; } + +done: + memset(data, 0, bytes); } }