Max has uploaded this change for review. ( https://gerrit.osmocom.org/12980


Change subject: Stream client: add disconnect callback
......................................................................

Stream client: add disconnect callback

It's similar to connect_cb() but called once client has been
disconnected.

Change-Id: I905adb2d6191216551a3bcdcd1aec1f96f01612a
---
M examples/ipa-stream-client.c
M examples/stream-client.c
M include/osmocom/netif/stream.h
M src/stream.c
4 files changed, 31 insertions(+), 1 deletion(-)



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

diff --git a/examples/ipa-stream-client.c b/examples/ipa-stream-client.c
index 2151090..cd8b38a 100644
--- a/examples/ipa-stream-client.c
+++ b/examples/ipa-stream-client.c
@@ -54,6 +54,13 @@
        exit(EXIT_SUCCESS);
 }

+static int disconnect_cb(struct osmo_stream_cli *conn)
+{
+       LOGP(DIPATEST, LOGL_NOTICE, "disconnected\n");
+
+       return 0;
+}
+
 static int connect_cb(struct osmo_stream_cli *conn)
 {
        int *__num_msgs = osmo_stream_cli_get_data(conn);
@@ -176,6 +183,7 @@
        osmo_stream_cli_set_addr(conn, "127.0.0.1");
        osmo_stream_cli_set_port(conn, 10000);
        osmo_stream_cli_set_connect_cb(conn, connect_cb);
+       osmo_stream_cli_set_connect_cb(conn, disconnect_cb);
        osmo_stream_cli_set_read_cb(conn, read_cb);
        osmo_stream_cli_set_data(conn, &num_msgs);

diff --git a/examples/stream-client.c b/examples/stream-client.c
index e2fb901..f590f25 100644
--- a/examples/stream-client.c
+++ b/examples/stream-client.c
@@ -43,6 +43,13 @@
        return 0;
 }

+static int disconnect_cb(struct osmo_stream_cli *conn)
+{
+       LOGP(DSTREAMTEST, LOGL_NOTICE, "disconnected: %s\n", 
osmo_stream_cli_get_sockname(conn));
+
+       return 0;
+}
+
 static int read_cb(struct osmo_stream_cli *conn)
 {
        int bytes;
@@ -121,6 +128,7 @@
        osmo_stream_cli_set_addr(conn, "127.0.0.1");
        osmo_stream_cli_set_port(conn, 10000);
        osmo_stream_cli_set_connect_cb(conn, connect_cb);
+       osmo_stream_cli_set_disconnect_cb(conn, disconnect_cb);
        osmo_stream_cli_set_read_cb(conn, read_cb);

        if (osmo_stream_cli_open(conn) < 0) {
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index 3044511..56162e4 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -64,6 +64,7 @@
 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);
 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_reconnect(struct osmo_stream_cli *cli);

diff --git a/src/stream.c b/src/stream.c
index 1880e25..c4db3d7 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -155,6 +155,7 @@
        uint16_t                        local_port;
        uint16_t                        proto;
        int (*connect_cb)(struct osmo_stream_cli *srv);
+       int (*disconnect_cb)(struct osmo_stream_cli *srv);
        int (*read_cb)(struct osmo_stream_cli *srv);
        int (*write_cb)(struct osmo_stream_cli *srv);
        void                            *data;
@@ -194,8 +195,11 @@
        close(cli->ofd.fd);
        cli->ofd.fd = -1;

-       if (cli->state == STREAM_CLI_STATE_CONNECTED)
+       if (cli->state == STREAM_CLI_STATE_CONNECTED) {
                LOGSCLI(cli, LOGL_DEBUG, "connection closed\n");
+               if (cli->disconnect_cb)
+                       cli->disconnect_cb(cli);
+       }

        cli->state = STREAM_CLI_STATE_NONE;
 }
@@ -442,6 +446,15 @@
        cli->connect_cb = connect_cb;
 }

+/*! \brief Set the call-back function called on disconnect of the stream 
client socket
+ *  \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))
+{
+       cli->disconnect_cb = disconnect_cb;
+}
+
 /*! \brief Set the call-back function called to read from the stream client 
socket
  *  \param[in] cli Stream Client to modify
  *  \param[in] read_cb Call-back function to be called when we want to read */

--
To view, visit https://gerrit.osmocom.org/12980
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I905adb2d6191216551a3bcdcd1aec1f96f01612a
Gerrit-Change-Number: 12980
Gerrit-PatchSet: 1
Gerrit-Owner: Max <[email protected]>

Reply via email to