2016-03-02 22:25:56 -08:00
|
|
|
#include <assert.h>
|
2016-02-29 20:49:36 -08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "buf.h"
|
2016-02-29 22:17:37 -08:00
|
|
|
#include "socket.h"
|
2016-02-29 20:49:36 -08:00
|
|
|
|
|
|
|
|
#include "flow.h"
|
|
|
|
|
|
2016-03-02 22:25:56 -08:00
|
|
|
static bool flow_send_hello(int fd, struct flow *flow, void *passthrough) {
|
|
|
|
|
struct buf buf = BUF_INIT, *buf_ptr = &buf;
|
|
|
|
|
flow_get_hello(flow, &buf_ptr, passthrough);
|
|
|
|
|
if (!buf_ptr->length) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return (write(fd, buf_at(buf_ptr, 0), buf_ptr->length) == (ssize_t) buf_ptr->length);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 19:28:02 -08:00
|
|
|
void flow_socket_ready(int fd, struct flow *flow) {
|
|
|
|
|
socket_ready(fd);
|
|
|
|
|
if (flow->socket_ready) {
|
|
|
|
|
flow->socket_ready(fd);
|
2016-02-29 22:17:37 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 21:51:30 -08:00
|
|
|
void flow_socket_connected(int fd, struct flow *flow) {
|
|
|
|
|
if (flow->socket_connected) {
|
|
|
|
|
flow->socket_connected(fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 22:25:56 -08:00
|
|
|
void flow_new(int fd, struct flow *flow, void *passthrough, struct peer *on_close) {
|
|
|
|
|
flow->new(fd, passthrough, on_close);
|
2016-02-29 20:49:36 -08:00
|
|
|
}
|
2016-03-02 19:16:23 -08:00
|
|
|
|
2016-03-02 22:25:56 -08:00
|
|
|
bool flow_new_send_hello(int fd, struct flow *flow, void *passthrough, struct peer *on_close) {
|
|
|
|
|
if (!flow_send_hello(fd, flow, passthrough)) {
|
|
|
|
|
assert(!close(fd));
|
2016-03-02 19:16:23 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-03-02 22:25:56 -08:00
|
|
|
flow_new(fd, flow, passthrough, on_close);
|
2016-03-02 19:16:23 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
2016-03-02 19:20:25 -08:00
|
|
|
|
2016-03-02 22:25:56 -08:00
|
|
|
void flow_get_hello(struct flow *flow, struct buf **buf_ptr, void *passthrough) {
|
|
|
|
|
if (!flow->get_hello) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
flow->get_hello(buf_ptr, passthrough);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 19:20:25 -08:00
|
|
|
void flow_ref_inc(struct flow *flow) {
|
|
|
|
|
(*flow->ref_count)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flow_ref_dec(struct flow *flow) {
|
|
|
|
|
(*flow->ref_count)--;
|
|
|
|
|
}
|