Skip to content

Commit

Permalink
feat(unix): introduce ability to close server
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldziuba03 committed Oct 17, 2024
1 parent 38147cc commit ac79a73
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions include/pandio/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ int pd_tcp_listen(pd_tcp_server_t*, int, pd_on_connection_cb);

void pd_tcp_server_init(pd_io_t*, pd_tcp_server_t*);

int pd_tcp_server_close(pd_tcp_server_t*);

void pd_tcp_init(pd_io_t*, pd_tcp_t*);

/* Creates pd_tcp_t from OS socket handle */
Expand Down
24 changes: 11 additions & 13 deletions samples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,17 @@ void interval_task(pd_timer_t *timer) {
printf("Timer callback\n");
}

void close_server(pd_timer_t *timer) {
printf("Closing server...\n");
pd_tcp_server_t *server = timer->data;
pd_tcp_server_close(server);
free(server);
}

int main() {
pd_io_t *ctx = malloc(sizeof(pd_io_t));
pd_io_init(ctx);

pd_threadpool_init(4);

pd_timer_t timer;
pd_timer_init(ctx, &timer);
pd_timer_repeat(&timer, interval_task, 4000);

for (int i = 0; i < 11; ++i) {
pd_task_t *task = malloc(sizeof(pd_task_t));
task->work = expensive_task;
task->done = task_done;
pd_task_submit(ctx, task);
}

pd_tcp_server_t *server = malloc(sizeof(pd_tcp_server_t));
pd_tcp_server_init(ctx, server);
//pd_threadpool_end();
Expand All @@ -87,5 +80,10 @@ int main() {
printf("Listener failed.\n");
}

pd_timer_t timer;
pd_timer_init(ctx, &timer);
timer.data = server;
pd_timer_start(&timer, close_server, 10 * 1000);

pd_io_run(ctx);
}
1 change: 1 addition & 0 deletions src/sys/event_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void pd_io_init(pd_io_t *ctx) {
}

ctx->now = pd_now();
queue_init(&ctx->pending_closes);
pd_timers_heap_init(ctx);

ctx->task_signal = malloc(sizeof(pd_notifier_t));
Expand Down
1 change: 1 addition & 0 deletions src/sys/event_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void pd_io_init(pd_io_t *ctx) {
abort();

ctx->now = pd_now();
queue_init(&ctx->pending_closes);
pd_timers_heap_init(ctx);

ctx->task_signal = malloc(sizeof(pd_notifier_t));
Expand Down
15 changes: 15 additions & 0 deletions src/sys/tcp_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ void pd_tcp_server_init(pd_io_t *ctx, pd_tcp_server_t *server) {
}


int pd_tcp_server_close(pd_tcp_server_t *server) {
int status;
status = pd__event_del(server->ctx, server->fd);
if (status < 0)
return status;

status = pd__closesocket(server->fd);
if (status < 0)
return status;

server->ctx->refs--;
return 0;
}


/* handler for I/O events from epoll/kqueue */
void pnd__tcp_listener_io(pd_event_t *event, unsigned events) {
assert(events & PD_POLLIN);
Expand Down
6 changes: 6 additions & 0 deletions src/sys/tcp_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ void pd_tcp_server_init(pd_io_t *ctx, pd_tcp_server_t *server) {
}


int pd_tcp_server_close(pd_tcp_server_t *server) {
// not supported yet on Windows
return PD_UNKNOWN;
}


/* Handling callback from IOCP */
void pd__tcp_accept_io(pd_event_t *event) {
pd__accept_op_t *op = event->data;
Expand Down

0 comments on commit ac79a73

Please sign in to comment.