Skip to content

Commit

Permalink
If we get a connection reset, do the unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
starseeker committed Jul 18, 2023
1 parent 88d3c7a commit d6a8b92
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/libpkg/example_uv/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void echo_write(uv_write_t *req, int status) {
free_write_req(req);
}

void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
void after_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {

if (nread > 0) {
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
Expand All @@ -119,15 +119,18 @@ void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
}

if (nread < 0) {
if (nread != UV_EOF)
if (nread != UV_EOF && nread != UV_ECONNRESET)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, NULL);
uv_fs_t req;
uv_fs_unlink(loop, &req, "/tmp/pkg_uv_pipe", NULL);
exit(0);
}

free(buf->base);
}

void on_new_connection(uv_stream_t *server, int status) {
void on_listen(uv_stream_t *server, int status) {
if (status == -1) {
// error!
return;
Expand All @@ -136,19 +139,13 @@ void on_new_connection(uv_stream_t *server, int status) {
uv_pipe_t *client = (uv_pipe_t*) malloc(sizeof(uv_pipe_t));
uv_pipe_init(loop, client, 0);
if (uv_accept(server, (uv_stream_t*) client) == 0) {
uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
uv_read_start((uv_stream_t*) client, alloc_buffer, after_read);
}
else {
uv_close((uv_handle_t*) client, NULL);
}
}

void remove_sock(int UNUSED(sig)) {
uv_fs_t req;
uv_fs_unlink(loop, &req, PIPENAME, NULL);
exit(0);
}

int
main(void) {

Expand All @@ -165,7 +162,7 @@ main(void) {
fprintf(stderr, "Bind error %s\n", uv_err_name(r));
return 1;
}
if ((r = uv_listen((uv_stream_t*) server, 128, on_new_connection))) {
if ((r = uv_listen((uv_stream_t*) server, 128, on_listen))) {
fprintf(stderr, "Listen error %s\n", uv_err_name(r));
return 2;
}
Expand Down

0 comments on commit d6a8b92

Please sign in to comment.