aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Willmann <dwillmann@sysmocom.de>2023-06-09 15:01:39 +0200
committerDaniel Willmann <dwillmann@sysmocom.de>2023-06-12 16:05:22 +0200
commita5d5b33a91987415db40d436a012deb60948351c (patch)
tree56fe00a0f2facc65c73d5103724f640770705725
parentd455f9e60a07a9a0007caf1cd101369ad91f46aa (diff)
stream: Factor out reconnection handling
-rw-r--r--src/stream.c74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/stream.c b/src/stream.c
index 2e38916..7c8e21e 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -420,44 +420,56 @@ static int _setsockopt_nosigpipe(struct osmo_stream_cli *cli)
#endif
}
-static int osmo_stream_cli_fd_cb(struct osmo_fd *ofd, unsigned int what)
+static void stream_cli_handle_connecting(struct osmo_stream_cli *cli, int res)
{
- struct osmo_stream_cli *cli = ofd->data;
- int error, ret;
+ int error, ret = res;
socklen_t len = sizeof(error);
- switch(cli->state) {
- case STREAM_CLI_STATE_CONNECTING:
- ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
- if (ret >= 0 && error > 0) {
- osmo_stream_cli_reconnect(cli);
- return 0;
- }
+ int fd = osmo_stream_cli_fd(cli);
- /* If messages got enqueued while 'connecting', keep WRITE flag
- up to dispatch them upon next main loop step */
- if (llist_empty(&cli->tx_queue))
- osmo_fd_write_disable(&cli->ofd);
+ if (ret < 0) {
+ osmo_stream_cli_reconnect(cli);
+ return;
+ }
+ ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
+ if (ret >= 0 && error > 0) {
+ osmo_stream_cli_reconnect(cli);
+ return;
+ }
- LOGSCLI(cli, LOGL_DEBUG, "connection established\n");
- cli->state = STREAM_CLI_STATE_CONNECTED;
- switch (cli->sk_domain) {
- case AF_UNIX:
+ /* If messages got enqueued while 'connecting', keep WRITE flag
+ up to dispatch them upon next main loop step */
+ if (llist_empty(&cli->tx_queue))
+ osmo_fd_write_disable(&cli->ofd);
+
+ LOGSCLI(cli, LOGL_DEBUG, "connection established\n");
+ cli->state = STREAM_CLI_STATE_CONNECTED;
+ switch (cli->sk_domain) {
+ case AF_UNIX:
+ _setsockopt_nosigpipe(cli);
+ break;
+ case AF_UNSPEC:
+ case AF_INET:
+ case AF_INET6:
+ if (cli->proto == IPPROTO_SCTP) {
_setsockopt_nosigpipe(cli);
- break;
- case AF_UNSPEC:
- case AF_INET:
- case AF_INET6:
- if (cli->proto == IPPROTO_SCTP) {
- _setsockopt_nosigpipe(cli);
- sctp_sock_activate_events(ofd->fd);
- }
- break;
- default:
- break;
+ sctp_sock_activate_events(fd);
}
- if (cli->connect_cb)
- cli->connect_cb(cli);
+ break;
+ default:
+ break;
+ }
+ if (cli->connect_cb)
+ cli->connect_cb(cli);
+}
+
+static int osmo_stream_cli_fd_cb(struct osmo_fd *ofd, unsigned int what)
+{
+ struct osmo_stream_cli *cli = ofd->data;
+
+ switch (cli->state) {
+ case STREAM_CLI_STATE_CONNECTING:
+ stream_cli_handle_connecting(cli, 0);
break;
case STREAM_CLI_STATE_CONNECTED:
if (what & OSMO_FD_READ) {