From 8d9b40e95447a734b31f81f967ac4e6577728506 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 27 Feb 2016 12:48:01 -0800 Subject: [PATCH] Minor hello cleanups. --- adsbus/incoming.c | 28 ++++++++++++++++------------ adsbus/outgoing.c | 12 +++++------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/adsbus/incoming.c b/adsbus/incoming.c index b6ef250..70e4a89 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -44,6 +44,18 @@ static void incoming_retry(struct incoming *incoming) { wakeup_add((struct peer *) incoming, delay); } +static bool incoming_hello(int fd, struct incoming *incoming) { + if (!incoming->hello) { + return true; + } + struct buf buf = BUF_INIT, *buf_ptr = &buf; + incoming->hello(&buf_ptr, incoming->passthrough); + if (!buf_ptr->length) { + return true; + } + return (write(fd, buf_at(buf_ptr, 0), buf_ptr->length) == (ssize_t) buf_ptr->length); +} + static void incoming_handler(struct peer *peer) { struct incoming *incoming = (struct incoming *) peer; @@ -69,18 +81,10 @@ static void incoming_handler(struct peer *peer) { socket_connected_init(fd); - { - struct buf buf = BUF_INIT, *buf_ptr = &buf; - if (incoming->hello) { - incoming->hello(&buf_ptr, incoming->passthrough); - } - if (buf_ptr->length) { - if (write(fd, buf_at(buf_ptr, 0), buf_ptr->length) != (ssize_t) buf_ptr->length) { - fprintf(stderr, "I %s: Error writing greeting\n", incoming->id); - assert(!close(fd)); - return; - } - } + if (!incoming_hello(fd, incoming)) { + fprintf(stderr, "I %s: Error writing greeting\n", incoming->id); + assert(!close(fd)); + return; } incoming->handler(fd, incoming->passthrough, NULL); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index e5736ec..37feac2 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -62,14 +62,12 @@ static void outgoing_connect_next(struct outgoing *outgoing) { outgoing->peer.fd = socket(outgoing->addr->ai_family, outgoing->addr->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC, outgoing->addr->ai_protocol); assert(outgoing->peer.fd >= 0); - { - struct buf buf = BUF_INIT, *buf_ptr = &buf; - if (outgoing->hello) { - outgoing->hello(&buf_ptr, outgoing->passthrough); - } - int result = (int) sendto(outgoing->peer.fd, buf_at(buf_ptr, 0), buf_ptr->length, MSG_FASTOPEN, outgoing->addr->ai_addr, outgoing->addr->ai_addrlen); - outgoing_connect_result(outgoing, result == 0 ? result : errno); + struct buf buf = BUF_INIT, *buf_ptr = &buf; + if (outgoing->hello) { + outgoing->hello(&buf_ptr, outgoing->passthrough); } + int result = (int) sendto(outgoing->peer.fd, buf_at(buf_ptr, 0), buf_ptr->length, MSG_FASTOPEN, outgoing->addr->ai_addr, outgoing->addr->ai_addrlen); + outgoing_connect_result(outgoing, result == 0 ? result : errno); } static void outgoing_connect_handler(struct peer *peer) {