Skip to content

Commit

Permalink
feat: add an api to judge whether the conn was closed
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Sep 22, 2021
1 parent 2f3bdf1 commit cdb6e9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,13 @@ static struct http_parser_settings settings = {
.on_message_complete = on_message_complete_cb
};

static bool conn_closed(struct uh_connection *conn)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;

return __sync_fetch_and_or(&conni->closed, 0);
}

static void conn_incref(struct uh_connection *conn)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
Expand Down Expand Up @@ -736,6 +743,11 @@ void conn_free(struct uh_connection_internal *conn)

conn->flags |= CONN_F_CLOSED;

if (__sync_fetch_and_or(&conn->closed, 1)) {
conn_decref((struct uh_connection *)conn);
return;
}

ev_timer_stop(loop, &conn->timer);
ev_io_stop(loop, &conn->ior);
ev_io_stop(loop, &conn->iow);
Expand Down Expand Up @@ -1061,6 +1073,7 @@ static void conn_init_cb(struct uh_connection *conn)
conn->download_file = download_file;
conn->serve_cgi = serve_cgi;

conn->closed = conn_closed;
conn->close = conn_close;

conn->incref = conn_incref;
Expand Down
1 change: 1 addition & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct uh_connection_internal {
int fd;
uint64_t size;
} file;
uint8_t closed;
size_t refcount;
struct ev_io ior;
struct ev_io iow;
Expand Down
1 change: 1 addition & 0 deletions src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ struct uh_connection {
/* handle cgi */
void (*serve_cgi)(struct uh_connection *conn, int event);

bool (*closed)(struct uh_connection *conn);
void (*close)(struct uh_connection *conn); /* close low level TCP connection */
void (*incref)(struct uh_connection *conn);
void (*decref)(struct uh_connection *conn);
Expand Down

0 comments on commit cdb6e9e

Please sign in to comment.