From a423e122843cd60fea0beef0db65fdef6e0e2960 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 17 Apr 2024 13:53:54 +0200 Subject: [PATCH] stream: Define types for each API callback The amount and complexity of callbacks is increasing over time. Use typedefs to define each of them so that callbacks: - Are easier to identify (which types is used where) - Are easier to document (have a 1st class place to write doxygen documentation) Change-Id: Ib0c4a9713fa4c755e457b8c2cbde6a7724d36e28 --- include/osmocom/netif/stream.h | 37 ++++++++++++++++++++++++---------- src/stream_cli.c | 22 ++++++++++---------- src/stream_srv.c | 24 ++++++++++++---------- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h index 61bc1ad..398b277 100644 --- a/include/osmocom/netif/stream.h +++ b/include/osmocom/netif/stream.h @@ -62,6 +62,8 @@ /*! \brief Osmocom Stream Server Link: A server socket listening/accepting */ struct osmo_stream_srv_link; +typedef int (*osmo_stream_srv_link_accept_cb_t)(struct osmo_stream_srv_link *link, int fd); + struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx); void osmo_stream_srv_link_destroy(struct osmo_stream_srv_link *link); @@ -74,7 +76,7 @@ void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t p void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto); int osmo_stream_srv_link_set_type(struct osmo_stream_srv_link *link, int type); int osmo_stream_srv_link_set_domain(struct osmo_stream_srv_link *link, int domain); -void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)(struct osmo_stream_srv_link *link, int fd)); +void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, osmo_stream_srv_link_accept_cb_t accept_cb); void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data); void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link); char *osmo_stream_srv_link_get_sockname(const struct osmo_stream_srv_link *link); @@ -98,12 +100,20 @@ int osmo_stream_srv_link_set_param(struct osmo_stream_srv_link *link, enum osmo_ * osmo_stream_srv_link */ struct osmo_stream_srv; -struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, int fd, int (*read_cb)(struct osmo_stream_srv *conn), int (*closed_cb)(struct osmo_stream_srv *conn), void *data); +typedef int (*osmo_stream_srv_read_cb_t)(struct osmo_stream_srv *conn); +typedef int (*osmo_stream_srv_closed_cb_t)(struct osmo_stream_srv *conn); +typedef int (*osmo_stream_srv_read_cb2_t)(struct osmo_stream_srv *conn, struct msgb *msg); +typedef int (*osmo_stream_srv_segmentation_cb_t)(struct msgb *msg); + +struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, int fd, + osmo_stream_srv_read_cb_t read_cb, + osmo_stream_srv_closed_cb_t closed_cb, + void *data); struct osmo_stream_srv *osmo_stream_srv_create2(void *ctx, struct osmo_stream_srv_link *link, int fd, void *data); void osmo_stream_srv_set_name(struct osmo_stream_srv *conn, const char *name); const char *osmo_stream_srv_get_name(const struct osmo_stream_srv *conn); -void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, int (*read_cb)(struct osmo_stream_srv *conn, struct msgb *msg)); -void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, int (*closed_cb)(struct osmo_stream_srv *conn)); +void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, osmo_stream_srv_read_cb2_t read_cb); +void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, osmo_stream_srv_closed_cb_t close_cb); void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn); struct osmo_stream_srv_link *osmo_stream_srv_get_master(struct osmo_stream_srv *conn); const char *osmo_stream_srv_get_sockname(const struct osmo_stream_srv *conn); @@ -115,8 +125,7 @@ void osmo_stream_srv_destroy(struct osmo_stream_srv *conn); void osmo_stream_srv_set_flush_and_destroy(struct osmo_stream_srv *conn); void osmo_stream_srv_set_data(struct osmo_stream_srv *conn, void *data); -void osmo_stream_srv_set_segmentation_cb(struct osmo_stream_srv *conn, - int (*segmentation_cb)(struct msgb *msg)); +void osmo_stream_srv_set_segmentation_cb(struct osmo_stream_srv *conn, osmo_stream_srv_segmentation_cb_t segmentation_cb); void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg); int osmo_stream_srv_recv(struct osmo_stream_srv *conn, struct msgb *msg); @@ -160,6 +169,12 @@ void osmo_stream_srv_clear_tx_queue(struct osmo_stream_srv *conn); /*! \brief Osmocom Stream Client: Single client connection */ struct osmo_stream_cli; +typedef int (*osmo_stream_cli_connect_cb_t)(struct osmo_stream_cli *cli); +typedef int (*osmo_stream_cli_disconnect_cb_t)(struct osmo_stream_cli *cli); +typedef int (*osmo_stream_cli_read_cb_t)(struct osmo_stream_cli *cli); +typedef int (*osmo_stream_cli_read_cb2_t)(struct osmo_stream_cli *cli, struct msgb *msg); +typedef int (*osmo_stream_cli_segmentation_cb_t)(struct msgb *msg); + void osmo_stream_cli_set_name(struct osmo_stream_cli *cli, const char *name); const char *osmo_stream_cli_get_name(const struct osmo_stream_cli *cli); void osmo_stream_cli_set_nodelay(struct osmo_stream_cli *cli, bool nodelay); @@ -179,11 +194,11 @@ char *osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli); struct osmo_fd *osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli); int osmo_stream_cli_get_fd(const struct osmo_stream_cli *cli); struct osmo_io_fd *osmo_stream_cli_get_iofd(const struct osmo_stream_cli *cli); -void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, int (*connect_cb)(struct osmo_stream_cli *cli)); -void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, int (*disconnect_cb)(struct osmo_stream_cli *cli)); -void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli)); -void osmo_stream_cli_set_read_cb2(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli, struct msgb *msg)); -void osmo_stream_cli_set_segmentation_cb(struct osmo_stream_cli *cli, int (*segmentation_cb)(struct msgb *msg)); +void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, osmo_stream_cli_connect_cb_t connect_cb); +void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, osmo_stream_cli_disconnect_cb_t disconnect_cb); +void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, osmo_stream_cli_read_cb_t read_cb); +void osmo_stream_cli_set_read_cb2(struct osmo_stream_cli *cli, osmo_stream_cli_read_cb2_t read_cb); +void osmo_stream_cli_set_segmentation_cb(struct osmo_stream_cli *cli, osmo_stream_cli_segmentation_cb_t segmentation_cb); void osmo_stream_cli_reconnect(struct osmo_stream_cli *cli); bool osmo_stream_cli_is_connected(struct osmo_stream_cli *cli); diff --git a/src/stream_cli.c b/src/stream_cli.c index 0427032..62ea03e 100644 --- a/src/stream_cli.c +++ b/src/stream_cli.c @@ -103,11 +103,11 @@ struct osmo_stream_cli { int sk_domain; int sk_type; uint16_t proto; - int (*connect_cb)(struct osmo_stream_cli *cli); - int (*disconnect_cb)(struct osmo_stream_cli *cli); - int (*read_cb)(struct osmo_stream_cli *cli); - int (*iofd_read_cb)(struct osmo_stream_cli *cli, struct msgb *msg); - int (*segmentation_cb)(struct msgb *msg); + osmo_stream_cli_connect_cb_t connect_cb; + osmo_stream_cli_disconnect_cb_t disconnect_cb; + osmo_stream_cli_read_cb_t read_cb; + osmo_stream_cli_read_cb2_t iofd_read_cb; + osmo_stream_cli_segmentation_cb_t segmentation_cb; void *data; int flags; int reconnect_timeout; @@ -661,7 +661,7 @@ osmo_stream_cli_set_proto(struct osmo_stream_cli *cli, uint16_t proto) /* Configure client side segmentation for the iofd */ static void configure_cli_segmentation_cb(struct osmo_stream_cli *cli, - int (*segmentation_cb)(struct msgb *msg)) + osmo_stream_cli_segmentation_cb_t segmentation_cb) { /* Copy default settings */ struct osmo_io_ops client_ops; @@ -676,7 +676,7 @@ static void configure_cli_segmentation_cb(struct osmo_stream_cli *cli, * \param[in] segmentation_cb Target segmentation callback */ void osmo_stream_cli_set_segmentation_cb(struct osmo_stream_cli *cli, - int (*segmentation_cb)(struct msgb *msg)) + osmo_stream_cli_segmentation_cb_t segmentation_cb) { cli->segmentation_cb = segmentation_cb; if (cli->iofd) /* Otherwise, this will be done in osmo_stream_cli_open() */ @@ -784,7 +784,7 @@ osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli) * \param[in] connect_cb Call-back function to be called upon connect */ void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, - int (*connect_cb)(struct osmo_stream_cli *cli)) + osmo_stream_cli_connect_cb_t connect_cb) { cli->connect_cb = connect_cb; } @@ -793,7 +793,7 @@ osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, * \param[in] cli Stream Client to modify * \param[in] disconnect_cb Call-back function to be called upon disconnect */ void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, - int (*disconnect_cb)(struct osmo_stream_cli *cli)) + osmo_stream_cli_disconnect_cb_t disconnect_cb) { cli->disconnect_cb = disconnect_cb; } @@ -804,7 +804,7 @@ void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, * \param[in] read_cb Call-back function to be called when we want to read */ void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, - int (*read_cb)(struct osmo_stream_cli *cli)) + osmo_stream_cli_read_cb_t read_cb) { OSMO_ASSERT(cli->mode != OSMO_STREAM_MODE_OSMO_IO); cli->mode = OSMO_STREAM_MODE_OSMO_FD; @@ -817,7 +817,7 @@ osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, * \param[in] read_cb Call-back function to be called when data was read from the socket */ void osmo_stream_cli_set_read_cb2(struct osmo_stream_cli *cli, - int (*read_cb)(struct osmo_stream_cli *cli, struct msgb *msg)) + osmo_stream_cli_read_cb2_t read_cb) { OSMO_ASSERT(cli->mode != OSMO_STREAM_MODE_OSMO_FD); cli->mode = OSMO_STREAM_MODE_OSMO_IO; diff --git a/src/stream_srv.c b/src/stream_srv.c index a02c1c2..74193a4 100644 --- a/src/stream_srv.c +++ b/src/stream_srv.c @@ -82,7 +82,7 @@ struct osmo_stream_srv_link { int sk_domain; int sk_type; uint16_t proto; - int (*accept_cb)(struct osmo_stream_srv_link *srv, int fd); + osmo_stream_srv_link_accept_cb_t accept_cb; void *data; int flags; struct osmo_sock_init2_multiaddr_pars ma_pars; @@ -597,9 +597,9 @@ struct osmo_stream_srv { struct osmo_io_fd *iofd; }; struct llist_head tx_queue; - int (*closed_cb)(struct osmo_stream_srv *peer); - int (*read_cb)(struct osmo_stream_srv *peer); - int (*iofd_read_cb)(struct osmo_stream_srv *peer, struct msgb *msg); + osmo_stream_srv_closed_cb_t closed_cb; + osmo_stream_srv_read_cb_t read_cb; + osmo_stream_srv_read_cb2_t iofd_read_cb; void *data; int flags; }; @@ -811,10 +811,10 @@ static int osmo_stream_srv_cb(struct osmo_fd *ofd, unsigned int what) * \param[in] data User data to save in the new Stream Server struct * \returns Stream Server in case of success; NULL on error */ struct osmo_stream_srv * -osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, - int fd, - int (*read_cb)(struct osmo_stream_srv *conn), - int (*closed_cb)(struct osmo_stream_srv *conn), void *data) +osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, int fd, + osmo_stream_srv_read_cb_t read_cb, + osmo_stream_srv_closed_cb_t closed_cb, + void *data) { struct osmo_stream_srv *conn; @@ -923,7 +923,8 @@ const char *osmo_stream_srv_get_name(const struct osmo_stream_srv *conn) * * \param[in] conn Stream Server to modify * \param[in] read_cb Call-back function to be called when data was read */ -void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, int (*read_cb)(struct osmo_stream_srv *conn, struct msgb *msg)) +void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, + osmo_stream_srv_read_cb2_t read_cb) { OSMO_ASSERT(conn && conn->mode == OSMO_STREAM_MODE_OSMO_IO); conn->iofd_read_cb = read_cb; @@ -935,7 +936,8 @@ void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, int (*read_cb)(st * internal state related to this specific client/connection. * \param[in] conn Stream Server to modify * \param[in] closed_cb Call-back function to be called when the connection was closed */ -void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, int (*closed_cb)(struct osmo_stream_srv *conn)) +void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, + osmo_stream_srv_closed_cb_t closed_cb) { OSMO_ASSERT(conn); conn->closed_cb = closed_cb; @@ -974,7 +976,7 @@ osmo_stream_srv_set_data(struct osmo_stream_srv *conn, * \param[in,out] conn Target Stream Server to modify * \param[in] segmentation_cb Segmentation callback to be set */ void osmo_stream_srv_set_segmentation_cb(struct osmo_stream_srv *conn, - int (*segmentation_cb)(struct msgb *msg)) + osmo_stream_srv_segmentation_cb_t segmentation_cb) { /* Note that the following implies that iofd != NULL, since * osmo_stream_srv_create2() creates the iofd member, too */