Minor hello cleanups.

This commit is contained in:
Ian Gulliver
2016-02-27 12:48:01 -08:00
parent 2c5b6419d6
commit 8d9b40e954
2 changed files with 21 additions and 19 deletions

View File

@@ -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,19 +81,11 @@ 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) {
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);
}

View File

@@ -62,7 +62,6 @@ 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);
@@ -70,7 +69,6 @@ static void outgoing_connect_next(struct outgoing *outgoing) {
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) {
struct outgoing *outgoing = (struct outgoing *) peer;