don't do unnecessary work

This commit is contained in:
hippoz 2023-01-17 03:09:38 +02:00
parent 1ad65fc10e
commit 9a9d001f04
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
2 changed files with 11 additions and 8 deletions

View file

@ -1,11 +1,11 @@
CC?=gcc CC?=gcc
CFLAGS_DEV=-DJB_PRESET_DEBUG -ggdb -fsanitize=address -Og 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+=-pipe -Wall -Wextra -Wshadow -pedantic -std=c99
CFLAGS+=$(CFLAGS_DEV) CFLAGS+=$(CFLAGS_RELEASE)
jitterbug: util.c wire.c match.c server.c main.c jitterbug: util.c wire.c match.c server.c main.c
$(CC) $(CFLAGS) -o $@ $^ $(CC) $(CFLAGS) -o $@ $^

View file

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/fcntl.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
@ -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) 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') { if (!name || *name == ':' || *name == '\0') {
return -1; return -1;
} }
int name_index = TRYST(bus_name_add(s, name, i)); TRYST(bus_name_add(s, name, i));
return 0; 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; uint32_t previous_cursor = ctx->byte_cursor;
if (match_rule_check(s, sender_index, c->matches[j], msg, ctx) >= 0) { 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(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)); TRYST(send(c->fd, reply_ctx->data, reply_ctx->byte_cursor, 0));
// TODO? // 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)); 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)); 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; 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) int bus_turn(bus_t *s)
{ {
static const char agree_unix_fd[] = "ERROR\r\n"; static const char agree_unix_fd[] = "ERROR\r\n";
@ -848,6 +847,8 @@ int bus_turn(bus_t *s)
if (fd == s->sock_fd) { if (fd == s->sock_fd) {
// new connection // new connection
int accepted_fd = TRYST(accept(s->sock_fd, NULL, NULL)); 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) { if (bus_client_add(s, accepted_fd) == -1) {
close(accepted_fd); close(accepted_fd);
@ -857,7 +858,6 @@ int bus_turn(bus_t *s)
// We add padding. See above. // We add padding. See above.
char data[data_buffer_len + data_buffer_padding]; char data[data_buffer_len + data_buffer_padding];
memset(data, 0, sizeof(data));
ssize_t bytes = recv(fd, data, data_buffer_len, 0); ssize_t bytes = recv(fd, data, data_buffer_len, 0);
if (bytes <= 0) { if (bytes <= 0) {
// error during recv() OR client disconnected, disconnect the client // error during recv() OR client disconnected, disconnect the client
@ -926,6 +926,9 @@ int bus_turn(bus_t *s)
_client_die("bad state"); _client_die("bad state");
} break; } break;
} }
done:
memset(data, 0, bytes);
} }
} }