aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax <msuraev@sysmocom.de>2019-02-20 17:12:34 +0100
committerMax <msuraev@sysmocom.de>2019-02-20 17:33:32 +0100
commitaf63d87a76ef4ae010e230d86ce9c07c6081cb80 (patch)
treefa0106633f0016b0b9b5fb5644d729f6e1fd2492
parentfe3527da2ac691c961f767c97d70bfe00d1e4d10 (diff)
Stream client: add disconnect callback
It's similar to connect_cb() but called once client has been disconnected. Change-Id: I905adb2d6191216551a3bcdcd1aec1f96f01612a
-rw-r--r--examples/ipa-stream-client.c8
-rw-r--r--examples/stream-client.c8
-rw-r--r--include/osmocom/netif/stream.h1
-rw-r--r--src/stream.c15
4 files changed, 31 insertions, 1 deletions
diff --git a/examples/ipa-stream-client.c b/examples/ipa-stream-client.c
index 2151090..ec7d2b3 100644
--- a/examples/ipa-stream-client.c
+++ b/examples/ipa-stream-client.c
@@ -54,6 +54,13 @@ void sighandler(int foo)
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 @@ int main(int argc, char *argv[])
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);
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 @@ static int connect_cb(struct osmo_stream_cli *conn)
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 @@ int main(void)
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 @@ void *osmo_stream_cli_get_data(struct osmo_stream_cli *cli);
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 @@ struct osmo_stream_cli {
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 @@ void osmo_stream_cli_close(struct osmo_stream_cli *cli)
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 @@ osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli,
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 */