daniel has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmo-netif/+/33253 )


Change subject: stream: Factor out reconnection handling
......................................................................

stream: Factor out reconnection handling

Change-Id: I418377eabd465ee4ffce9b4440e96287c7734924
---
M src/stream.c
1 file changed, 48 insertions(+), 27 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/53/33253/1

diff --git a/src/stream.c b/src/stream.c
index 2e38916..5f74127 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -420,44 +420,56 @@
 #endif
 }

+static void stream_cli_handle_connecting(struct osmo_stream_cli *cli, int res)
+{
+       int error, ret = res;
+       socklen_t len = sizeof(error);
+
+       int fd = osmo_stream_cli_fd(cli);
+
+       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:
+               _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(fd);
+               }
+               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;
-       int error, ret;
-       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;
-               }
+               stream_cli_handle_connecting(cli, 0);

                /* 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);
-                               sctp_sock_activate_events(ofd->fd);
-                       }
-                       break;
-               default:
-                       break;
-               }
-               if (cli->connect_cb)
-                       cli->connect_cb(cli);
                break;
        case STREAM_CLI_STATE_CONNECTED:
                if (what & OSMO_FD_READ) {

--
To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/33253
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-Change-Id: I418377eabd465ee4ffce9b4440e96287c7734924
Gerrit-Change-Number: 33253
Gerrit-PatchSet: 1
Gerrit-Owner: daniel <[email protected]>
Gerrit-MessageType: newchange

Reply via email to