kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 961a2e36858fda1b51a32bb54f702e06b71c2e23
parent 428802afc8d27a04dd322354b0bbc12d215bbea4
Author: Joris Vink <joris@coders.se>
Date:   Tue,  1 Dec 2015 20:55:00 +0100

Allow apps to override connection handling.

Setting the handle callback allows your application
to take care of network events for the connection.

Look at the connection state and flags to determine
if read/write is possible and go from there.

See kore_connection_handle() for more details.

Diffstat:
examples/nohttp/src/nohttp.c | 22++++++++++++++++++++++
includes/kore.h | 1+
src/bsd.c | 2+-
src/connection.c | 5+++--
src/linux.c | 2+-
5 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/examples/nohttp/src/nohttp.c b/examples/nohttp/src/nohttp.c @@ -27,6 +27,7 @@ #include <kore/kore.h> void connection_setup(struct connection *); +int connection_handle(struct connection *); int connection_recv_data(struct netbuf *); void @@ -43,6 +44,27 @@ connection_setup(struct connection *c) /* We are responsible for setting the connection state. */ c->state = CONN_STATE_ESTABLISHED; + + /* Override the handle function, called when new events occur. */ + c->handle = connection_handle; +} + +/* + * This function is called everytime a new event is triggered on the + * connection. In this demo we just use it as a stub for the normal + * callback kore_connection_handle(). + * + * In this callback you would generally look at the state of the connection + * in c->state and perform the required actions like writing / reading using + * net_send_flush() or net_recv_flush() if CONN_SEND_POSSIBLE or + * CONN_READ_POSSIBLE are set respectively. Returning KORE_RESULT_ERROR from + * this callback will disconnect the connection alltogether. + */ +int +connection_handle(struct connection *c) +{ + kore_log(LOG_NOTICE, "connection_handle: %p", c); + return (kore_connection_handle(c)); } /* diff --git a/includes/kore.h b/includes/kore.h @@ -166,6 +166,7 @@ struct connection { X509 *cert; int tls_reneg; + int (*handle)(struct connection *); void (*disconnect)(struct connection *); int (*read)(struct connection *, int *); int (*write)(struct connection *, int, int *); diff --git a/src/bsd.c b/src/bsd.c @@ -176,7 +176,7 @@ kore_platform_event_wait(u_int64_t timer) !(c->flags & CONN_WRITE_BLOCK)) c->flags |= CONN_WRITE_POSSIBLE; - if (!kore_connection_handle(c)) + if (c->handle != NULL && !c->handle(c)) kore_connection_disconnect(c); break; #if defined(KORE_USE_PGSQL) diff --git a/src/connection.c b/src/connection.c @@ -52,6 +52,7 @@ kore_connection_new(void *owner) c->cert = NULL; c->owner = owner; c->tls_reneg = 0; + c->handle = NULL; c->disconnect = NULL; c->hdlr_extra = NULL; c->proto = CONN_PROTO_UNKNOWN; @@ -102,6 +103,7 @@ kore_connection_accept(struct listener *listener, struct connection **out) return (KORE_RESULT_ERROR); } + c->handle = kore_connection_handle; TAILQ_INSERT_TAIL(&connections, c, list); #if !defined(KORE_NO_TLS) @@ -250,7 +252,7 @@ kore_connection_handle(struct connection *c) listener = (struct listener *)c->owner; if (listener->connect != NULL) { listener->connect(c); - goto tls_established; + return (KORE_RESULT_OK); } } @@ -266,7 +268,6 @@ kore_connection_handle(struct connection *c) #endif c->state = CONN_STATE_ESTABLISHED; -tls_established: /* FALLTHROUGH */ #endif /* !KORE_NO_TLS */ case CONN_STATE_ESTABLISHED: diff --git a/src/linux.c b/src/linux.c @@ -154,7 +154,7 @@ kore_platform_event_wait(u_int64_t timer) !(c->flags & CONN_WRITE_BLOCK)) c->flags |= CONN_WRITE_POSSIBLE; - if (!kore_connection_handle(c)) + if (!c->handle(c)) kore_connection_disconnect(c); break; #if defined(KORE_USE_PGSQL)