diff --git a/src/ipc.h b/src/ipc.h index f487948..876b843 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -247,10 +247,10 @@ struct ipc_messages { struct ipc_switching { int orig; int dest; - int (*orig_in) (int origin_fd, struct ipc_message *m); - int (*orig_out) (int origin_fd, struct ipc_message *m); - int (*dest_in) (int origin_fd, struct ipc_message *m); - int (*dest_out) (int origin_fd, struct ipc_message *m); + enum ipccb (*orig_in) (int origin_fd, struct ipc_message *m); + enum ipccb (*orig_out) (int origin_fd, struct ipc_message *m); + enum ipccb (*dest_in) (int origin_fd, struct ipc_message *m); + enum ipccb (*dest_out) (int origin_fd, struct ipc_message *m); }; struct ipc_switchings { @@ -392,6 +392,11 @@ void ipc_switching_add (struct ipc_switchings *is, int orig, int dest); int ipc_switching_del (struct ipc_switchings *is, int fd); int ipc_switching_get (struct ipc_switchings *is, int fd); void ipc_switching_free (struct ipc_switchings *is); +void ipc_switching_callbacks ( + struct ipc_ctx *ctx + , int fd + , enum ipccb (*cb_in )(int fd, struct ipc_message *m) + , enum ipccb (*cb_out)(int fd, struct ipc_message *m)); int ipc_ctx_fd_type (struct ipc_ctx *ctx, int fd, enum ipc_connection_type type); diff --git a/src/network.c b/src/network.c index 910f323..f07ad8d 100644 --- a/src/network.c +++ b/src/network.c @@ -145,14 +145,14 @@ int ipc_switching_del (struct ipc_switchings *is, int fd) */ int ipc_switching_get_ (const struct ipc_switchings *is , int fd - , struct ipc_switching *s) + , struct ipc_switching **s) { for (size_t i = 0; i < is->size; i++) { if (is->collection[i].orig == fd) { - *s = is->collection[i]; + *s = &is->collection[i]; return 0; } else if (is->collection[i].dest == fd) { - *s = is->collection[i]; + *s = &is->collection[i]; return 1; } } @@ -239,6 +239,28 @@ default_cb_out(int fd, struct ipc_message *m) return IPC_CB_NO_ERROR; } +void ipc_switching_callbacks ( + struct ipc_ctx *ctx + , int fd + , enum ipccb (*cb_in )(int fd, struct ipc_message *m) + , enum ipccb (*cb_out)(int fd, struct ipc_message *m)) +{ + struct ipc_switching *sw = NULL; + int is_valid = ipc_switching_get_ (&ctx->switchdb, fd, &sw); + if (is_valid == -1) { + return; + } + + if (sw->orig == fd) { + sw->orig_in = cb_in; + sw->orig_out = cb_out; + } + else { + sw->dest_in = cb_in; + sw->dest_out = cb_out; + } +} + /** * fd_switching_read allows to read a message from a switched fd. */ @@ -252,7 +274,7 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx int talkingfd = ctx->pollfd[index].fd; int dest_fd = -1; - struct ipc_switching sw; + struct ipc_switching *sw = NULL; struct ipc_message m; enum ipccb r; @@ -262,22 +284,22 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx T_R ((is_valid == -1), IPC_ERROR_FD_SWITCHING__NO_FD_RECORD); - if (sw.orig == talkingfd) { - dest_fd = sw.dest; - if (sw.orig_in == NULL) { + if (sw->orig == talkingfd) { + dest_fd = sw->dest; + if (sw->orig_in == NULL) { r = default_cb_in (talkingfd, &m); } else { - r = (*sw.orig_in)(talkingfd, &m); + r = (*sw->orig_in)(talkingfd, &m); } } else { - dest_fd = sw.orig; - if (sw.dest_in == NULL) { + dest_fd = sw->orig; + if (sw->dest_in == NULL) { r = default_cb_in (talkingfd, &m); } else { - r = (*sw.dest_in)(talkingfd, &m); + r = (*sw->dest_in)(talkingfd, &m); } } @@ -330,7 +352,7 @@ struct ipc_error fd_switching_write (struct ipc_event *event, struct ipc_ctx *ct T_R ((ctx->switchdb.size == 0), IPC_ERROR_FD_SWITCHING__NO_FD_RECORD); int output_fd = ctx->pollfd[index].fd; - struct ipc_switching sw; + struct ipc_switching *sw = NULL; struct ipc_message *m = NULL; size_t i; @@ -351,20 +373,20 @@ struct ipc_error fd_switching_write (struct ipc_event *event, struct ipc_ctx *ct T_R ((is_valid == -1), IPC_ERROR_FD_SWITCHING__NO_FD_RECORD); - if (sw.orig == output_fd) { - if (sw.orig_in == NULL) { + if (sw->orig == output_fd) { + if (sw->orig_in == NULL) { r = default_cb_out (output_fd, m); } else { - r = (*sw.orig_out)(output_fd, m); + r = (*sw->orig_out)(output_fd, m); } } else { - if (sw.dest_in == NULL) { + if (sw->dest_in == NULL) { r = default_cb_out (output_fd, m); } else { - r = (*sw.dest_out)(output_fd, m); + r = (*sw->dest_out)(output_fd, m); } }