pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-netif/+/38826?usp=email )
Change subject: stream_cli: Allow setting nodelay sockopt after opening sock ......................................................................
stream_cli: Allow setting nodelay sockopt after opening sock
This allows users who wish to first open the socket and set the nodelay option at a later point. This has also a use case according to man 7 tcp: """ setting this option forces an explicit flush of pending output, even if TCP_CORK is currently set. """
Change-Id: Ic178d924da03968de1f140bfc8805c972b849e7b --- M src/stream_cli.c 1 file changed, 24 insertions(+), 1 deletion(-)
git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/26/38826/1
diff --git a/src/stream_cli.c b/src/stream_cli.c index d4067d6..5d8a2c2 100644 --- a/src/stream_cli.c +++ b/src/stream_cli.c @@ -148,6 +148,17 @@ return cli->state == STREAM_CLI_STATE_CONNECTED; }
+/*! Check if Osmocom Stream Client is opened (has an FD available) according to + * its current state. + * \param[in] cli Osmocom Stream Client + * \return true if fd is available (osmo_stream_cli_get_fd()), false otherwise + */ +static bool stream_cli_is_opened(const struct osmo_stream_cli *cli) +{ + return cli->state == STREAM_CLI_STATE_CONNECTING || + cli->state == STREAM_CLI_STATE_CONNECTED; +} + static void osmo_stream_cli_close_iofd(struct osmo_stream_cli *cli) { if (!cli->iofd) @@ -911,17 +922,29 @@ /*! Set the NODELAY socket option to avoid Nagle-like behavior. * Setting this to nodelay=true will automatically set the NODELAY * socket option on any socket established via \ref osmo_stream_cli_open - * or any re-connect. You have to set this _before_ opening the + * or any re-connect. This can be set either before or after opening the * socket. * \param[in] cli Stream client whose sockets are to be configured * \param[in] nodelay whether to set (true) NODELAY before connect() */ void osmo_stream_cli_set_nodelay(struct osmo_stream_cli *cli, bool nodelay) { + int fd; if (nodelay) cli->flags |= OSMO_STREAM_CLI_F_NODELAY; else cli->flags &= ~OSMO_STREAM_CLI_F_NODELAY; + + if (!stream_cli_is_opened(cli)) + return; /* Config will be applied upon open() time */ + + if ((fd = osmo_stream_cli_get_fd(cli)) < 0) { + LOGSCLI(cli, LOGL_ERROR, "set_nodelay(%u): failed obtaining socket\n", nodelay); + return; + } + if (stream_setsockopt_nodelay(fd, cli->proto, nodelay ? 1 : 0) < 0) + LOGSCLI(cli, LOGL_ERROR, "set_nodelay(%u): failed setsockopt err=%d\n", + nodelay, errno); }
/*! Open connection of an Osmocom stream client.