Support set a callback for connection closed

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-15 13:07:37 +08:00
parent 6cf45fb0a2
commit afeee7ec2f
5 changed files with 19 additions and 0 deletions

View File

@ -31,6 +31,10 @@
#include "handler.h"
static void conn_closed_cb(struct uh_connection *conn)
{
}
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
if (w->signum == SIGINT) {
@ -113,6 +117,7 @@ int main(int argc, char **argv)
srv->set_docroot(srv, docroot);
srv->set_index_page(srv, index_page);
srv->set_conn_closed_cb(srv, conn_closed_cb);
srv->set_default_handler(srv, default_handler);
srv->add_path_handler(srv, "/echo", echo_handler);
srv->add_path_handler(srv, "/upload", upload_handler);

View File

@ -514,6 +514,9 @@ void conn_free(struct uh_connection_internal *conn)
uh_ssl_free(conn->ssl);
#endif
if (conn->srv->conn_closed_cb)
conn->srv->conn_closed_cb(&conn->com);
if (conn->sock > 0)
close(conn->sock);

View File

@ -226,6 +226,13 @@ static int uh_add_path_handler(struct uh_server *srv, const char *path, uh_path_
return 0;
}
static void uh_set_conn_abort_cb(struct uh_server *srv, uh_con_closed_cb_prototype cb)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;
srvi->conn_closed_cb = cb;
}
static void uh_set_default_handler(struct uh_server *srv, uh_path_handler_prototype handler)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;
@ -365,6 +372,7 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host
srv->load_plugin = uh_load_plugin;
srv->set_conn_closed_cb = uh_set_conn_abort_cb;
srv->set_default_handler = uh_set_default_handler;
srv->add_path_handler = uh_add_path_handler;

View File

@ -81,6 +81,7 @@ struct uh_connection {
void *userdata;
};
typedef void (*uh_con_closed_cb_prototype)(struct uh_connection *conn);
typedef void (*uh_path_handler_prototype)(struct uh_connection *conn, int event);
struct uh_server {
@ -90,6 +91,7 @@ struct uh_server {
int (*ssl_init)(struct uh_server *srv, const char *cert, const char *key);
#endif
int (*load_plugin)(struct uh_server *srv, const char *path);
void (*set_conn_closed_cb)(struct uh_server *srv, uh_con_closed_cb_prototype cb);
void (*set_default_handler)(struct uh_server *srv, uh_path_handler_prototype handler);
int (*add_path_handler)(struct uh_server *srv, const char *path, uh_path_handler_prototype handler);
int (*set_docroot)(struct uh_server *srv, const char *path);

View File

@ -39,6 +39,7 @@ struct uh_server_internal {
struct ev_loop *loop;
struct ev_io ior;
struct uh_connection_internal *conns;
void (*conn_closed_cb)(struct uh_connection *conn);
void (*default_handler)(struct uh_connection *conn, int event);
#if UHTTPD_SSL_SUPPORT
void *ssl_ctx;