Move more socket options into the flow framework.

This commit is contained in:
Ian Gulliver
2016-03-02 21:51:30 -08:00
parent 91775999d8
commit 10ba451458
8 changed files with 18 additions and 9 deletions

View File

@@ -12,6 +12,12 @@ void flow_socket_ready(int fd, struct flow *flow) {
} }
} }
void flow_socket_connected(int fd, struct flow *flow) {
if (flow->socket_connected) {
flow->socket_connected(fd);
}
}
bool flow_hello(int fd, struct flow *flow, void *passthrough) { bool flow_hello(int fd, struct flow *flow, void *passthrough) {
if (!flow->get_hello) { if (!flow->get_hello) {
return true; return true;

View File

@@ -9,12 +9,14 @@ struct peer;
struct flow { struct flow {
const char *name; const char *name;
void (*socket_ready)(int); void (*socket_ready)(int);
void (*socket_connected)(int);
void (*new)(int, void *, struct peer *); void (*new)(int, void *, struct peer *);
void (*get_hello)(struct buf **, void *); void (*get_hello)(struct buf **, void *);
uint32_t *ref_count; uint32_t *ref_count;
}; };
void flow_socket_ready(int, struct flow *); void flow_socket_ready(int, struct flow *);
void flow_socket_connected(int, struct flow *);
bool flow_hello(int, struct flow *, void *); bool flow_hello(int, struct flow *, void *);
bool flow_new(int, struct flow *, void *); bool flow_new(int, struct flow *, void *);
void flow_ref_inc(struct flow *); void flow_ref_inc(struct flow *);

View File

@@ -64,6 +64,8 @@ static void incoming_handler(struct peer *peer) {
local_hbuf, local_sbuf, local_hbuf, local_sbuf,
peer_hbuf, peer_sbuf); peer_hbuf, peer_sbuf);
flow_socket_connected(fd, incoming->flow);
if (!flow_hello(fd, incoming->flow, incoming->passthrough)) { if (!flow_hello(fd, incoming->flow, incoming->passthrough)) {
fprintf(stderr, "I %s: Error writing greeting\n", incoming->id); fprintf(stderr, "I %s: Error writing greeting\n", incoming->id);
assert(!close(fd)); assert(!close(fd));

View File

@@ -95,11 +95,12 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) {
case 0: case 0:
fprintf(stderr, "O %s: Connected to %s/%s\n", outgoing->id, hbuf, sbuf); fprintf(stderr, "O %s: Connected to %s/%s\n", outgoing->id, hbuf, sbuf);
freeaddrinfo(outgoing->addrs); freeaddrinfo(outgoing->addrs);
flow_socket_ready(outgoing->peer.fd, outgoing->flow);
outgoing->attempt = 0; outgoing->attempt = 0;
int fd = outgoing->peer.fd; int fd = outgoing->peer.fd;
outgoing->peer.fd = -1; outgoing->peer.fd = -1;
outgoing->peer.event_handler = outgoing_disconnect_handler; outgoing->peer.event_handler = outgoing_disconnect_handler;
flow_socket_ready(fd, outgoing->flow);
flow_socket_connected(fd, outgoing->flow);
outgoing->flow->new(fd, outgoing->passthrough, (struct peer *) outgoing); outgoing->flow->new(fd, outgoing->passthrough, (struct peer *) outgoing);
break; break;

View File

@@ -41,6 +41,7 @@ static void receive_new(int, void *, struct peer *);
static struct flow _receive_flow = { static struct flow _receive_flow = {
.name = "receive", .name = "receive",
.socket_connected = socket_connected_receive,
.new = receive_new, .new = receive_new,
.ref_count = &peer_count_in, .ref_count = &peer_count_in,
}; };
@@ -133,8 +134,6 @@ static void receive_read(struct peer *peer) {
static void receive_new(int fd, void __attribute__((unused)) *passthrough, struct peer *on_close) { static void receive_new(int fd, void __attribute__((unused)) *passthrough, struct peer *on_close) {
peer_count_in++; peer_count_in++;
socket_receive(fd);
struct receive *receive = malloc(sizeof(*receive)); struct receive *receive = malloc(sizeof(*receive));
assert(receive); assert(receive);

View File

@@ -39,6 +39,7 @@ static void send_get_hello(struct buf **, void *);
static struct flow _send_flow = { static struct flow _send_flow = {
.name = "send", .name = "send",
.socket_ready = socket_ready_send, .socket_ready = socket_ready_send,
.socket_connected = socket_connected_send,
.new = send_new, .new = send_new,
.get_hello = send_get_hello, .get_hello = send_get_hello,
.ref_count = &peer_count_out, .ref_count = &peer_count_out,
@@ -105,8 +106,6 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) {
peer_count_out++; peer_count_out++;
socket_send(fd);
struct send *send = malloc(sizeof(*send)); struct send *send = malloc(sizeof(*send));
assert(send); assert(send);

View File

@@ -48,13 +48,13 @@ void socket_ready_send(int fd) {
assert(res == 0); assert(res == 0);
} }
void socket_send(int fd) { void socket_connected_send(int fd) {
// Called by data flow code; NOT safe to assume that fd is a socket // Called by data flow code; NOT safe to assume that fd is a socket
int res = shutdown(fd, SHUT_RD); int res = shutdown(fd, SHUT_RD);
assert(res == 0 || (res == -1 && errno == ENOTSOCK)); assert(res == 0 || (res == -1 && errno == ENOTSOCK));
} }
void socket_receive(int fd) { void socket_connected_receive(int fd) {
// Called by data flow code; NOT safe to assume that fd is a socket // Called by data flow code; NOT safe to assume that fd is a socket
int res = shutdown(fd, SHUT_WR); int res = shutdown(fd, SHUT_WR);
assert(res == 0 || (res == -1 && errno == ENOTSOCK)); assert(res == 0 || (res == -1 && errno == ENOTSOCK));

View File

@@ -4,5 +4,5 @@ void socket_pre_bind(int);
void socket_pre_listen(int); void socket_pre_listen(int);
void socket_ready(int); void socket_ready(int);
void socket_ready_send(int); void socket_ready_send(int);
void socket_send(int); void socket_connected_send(int);
void socket_receive(int); void socket_connected_receive(int);