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.
This commit is contained in:
Joris Vink 2015-12-01 20:55:00 +01:00
parent 428802afc8
commit 961a2e3685
5 changed files with 28 additions and 4 deletions

View File

@ -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));
}
/*

View File

@ -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 *);

View File

@ -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)

View File

@ -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:

View File

@ -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)