diff options
author | Daniel Willmann <dwillmann@sysmocom.de> | 2023-03-17 11:37:10 +0100 |
---|---|---|
committer | Daniel Willmann <dwillmann@sysmocom.de> | 2023-06-14 18:17:13 +0200 |
commit | c873b62929adc1dcae75cc76197951c374614be5 (patch) | |
tree | 61db12bb33342da8ffcc5d22010e1c13d23bbb8d /examples | |
parent | 0e7028f742dedefd9fccc2d5b26875fca4036f58 (diff) |
examples: Use new stream API in {ipa-,}stream-{client,server}
Change-Id: I97a9979199c816686b32080534627f6f033e009e
Diffstat (limited to 'examples')
-rw-r--r-- | examples/ipa-stream-client.c | 19 | ||||
-rw-r--r-- | examples/ipa-stream-server.c | 24 | ||||
-rw-r--r-- | examples/stream-client.c | 24 | ||||
-rw-r--r-- | examples/stream-server.c | 28 |
4 files changed, 20 insertions, 75 deletions
diff --git a/examples/ipa-stream-client.c b/examples/ipa-stream-client.c index 0c9c589..6cd212f 100644 --- a/examples/ipa-stream-client.c +++ b/examples/ipa-stream-client.c @@ -103,21 +103,10 @@ static int connect_cb(struct osmo_stream_cli *conn) return 0; } -static int read_cb(struct osmo_stream_cli *conn) +static int read_cb(struct osmo_stream_cli *conn, struct msgb *msg) { - struct msgb *msg; + LOGP(DIPATEST, LOGL_DEBUG, "received message from stream (len=%d)\n", msgb_length(msg)); - LOGP(DIPATEST, LOGL_DEBUG, "received message from stream\n"); - - msg = osmo_ipa_msg_alloc(0); - if (msg == NULL) { - LOGP(DIPATEST, LOGL_ERROR, "cannot allocate message\n"); - return 0; - } - if (osmo_stream_cli_recv(conn, msg) <= 0) { - LOGP(DIPATEST, LOGL_ERROR, "cannot receive message\n"); - return 0; - } if (osmo_ipa_process_msg(msg) < 0) { LOGP(DIPATEST, LOGL_ERROR, "bad IPA message\n"); return 0; @@ -175,7 +164,7 @@ int main(int argc, char *argv[]) * initialize stream client. */ - conn = osmo_stream_cli_create(tall_test); + conn = osmo_stream_cli_create2(tall_test, "ipa_test_client"); if (conn == NULL) { fprintf(stderr, "cannot create client\n"); exit(EXIT_FAILURE); @@ -184,7 +173,7 @@ int main(int argc, char *argv[]) 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_read_cb2(conn, read_cb); osmo_stream_cli_set_data(conn, &num_msgs); osmo_stream_cli_set_nodelay(conn, true); diff --git a/examples/ipa-stream-server.c b/examples/ipa-stream-server.c index 1ca1aaf..b84f2c9 100644 --- a/examples/ipa-stream-server.c +++ b/examples/ipa-stream-server.c @@ -47,23 +47,10 @@ void sighandler(int foo) exit(EXIT_SUCCESS); } -int read_cb(struct osmo_stream_srv *conn) +int read_cb(struct osmo_stream_srv *conn, struct msgb *msg) { - struct msgb *msg; + LOGP(DSTREAMTEST, LOGL_DEBUG, "received message from stream (len=%d)\n", msgb_length(msg)); - LOGP(DSTREAMTEST, LOGL_DEBUG, "received message from stream\n"); - - msg = osmo_ipa_msg_alloc(0); - if (msg == NULL) { - LOGP(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n"); - return 0; - } - if (osmo_stream_srv_recv(conn, msg) <= 0) { - LOGP(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n"); - osmo_stream_srv_destroy(conn); - msgb_free(msg); - return -EBADF; - } if (osmo_ipa_process_msg(msg) < 0) { LOGP(DSTREAMTEST, LOGL_ERROR, "Bad IPA message\n"); msgb_free(msg); @@ -84,17 +71,18 @@ static int accept_cb(struct osmo_stream_srv_link *srv, int fd) { if (conn != NULL) { LOGP(DSTREAMTEST, LOGL_ERROR, "Sorry, this example only " - "support one client simultaneously\n"); + "supports one client simultaneously\n"); return -1; } - conn = osmo_stream_srv_create(tall_test, srv, fd, - read_cb, close_cb, NULL); + conn = osmo_stream_srv_create2(tall_test, "ipa_srv", srv, fd, NULL); if (conn == NULL) { LOGP(DSTREAMTEST, LOGL_ERROR, "error while creating connection\n"); return -1; } + osmo_stream_srv_set_read_cb(conn, read_cb); + osmo_stream_srv_set_closed_cb(conn, close_cb); return 0; } diff --git a/examples/stream-client.c b/examples/stream-client.c index 428402e..93fbdd3 100644 --- a/examples/stream-client.c +++ b/examples/stream-client.c @@ -50,27 +50,11 @@ static int disconnect_cb(struct osmo_stream_cli *conn) return 0; } -static int read_cb(struct osmo_stream_cli *conn) +static int read_cb(struct osmo_stream_cli *conn, struct msgb *msg) { - int bytes; - struct msgb *msg; - LOGP(DSTREAMTEST, LOGL_NOTICE, "receiving message from stream... "); - msg = msgb_alloc(1024, "STREAMCLIENT/test"); - if (msg == NULL) { - LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n"); - return 0; - } - - bytes = osmo_stream_cli_recv(conn, msg); - - if (bytes < 0) { - LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n"); - return 0; - } - - LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d (%d) bytes: %s\n", bytes, msg->len, msgb_hexdump(msg)); + LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d bytes: %s\n", msg->len, msgb_hexdump(msg)); msgb_free(msg); return 0; @@ -120,7 +104,7 @@ int main(void) * initialize stream cli. */ - conn = osmo_stream_cli_create(tall_test); + conn = osmo_stream_cli_create2(tall_test, "stream_client"); if (conn == NULL) { fprintf(stderr, "cannot create cli\n"); exit(EXIT_FAILURE); @@ -129,7 +113,7 @@ int main(void) 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_read_cb2(conn, read_cb); if (osmo_stream_cli_open(conn) < 0) { fprintf(stderr, "cannot open cli\n"); diff --git a/examples/stream-server.c b/examples/stream-server.c index e4ca480..9e077f0 100644 --- a/examples/stream-server.c +++ b/examples/stream-server.c @@ -42,29 +42,11 @@ void sighandler(int foo) exit(EXIT_SUCCESS); } -int read_cb(struct osmo_stream_srv *conn) +int read_cb(struct osmo_stream_srv *conn, struct msgb *msg) { - int bytes; - struct msgb *msg; - LOGP(DSTREAMTEST, LOGL_NOTICE, "receiving message from stream... "); - msg = msgb_alloc(1024, "STREAMSERVER/test"); - if (msg == NULL) { - LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n"); - return 0; - } - - bytes = osmo_stream_srv_recv(conn, msg); - - if (bytes <= 0) { - if (bytes < 0) - LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot receive message: %s\n", strerror(-bytes)); - else - LOGPC(DSTREAMTEST, LOGL_ERROR, "client closed connection\n"); - osmo_stream_srv_destroy(conn); - } else - LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d (%d) bytes: %s\n", bytes, msg->len, msgb_hexdump(msg)); + LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d bytes: %s\n", msg->len, msgb_hexdump(msg)); msgb_free(msg); return 0; @@ -72,6 +54,7 @@ int read_cb(struct osmo_stream_srv *conn) static int close_cb(struct osmo_stream_srv *dummy) { + LOGPC(DSTREAMTEST, LOGL_ERROR, "client closed connection\n"); conn = NULL; return 0; } @@ -86,13 +69,14 @@ static int accept_cb(struct osmo_stream_srv_link *srv, int fd) return -1; } - conn = osmo_stream_srv_create(tall_test, srv, fd, read_cb, - close_cb, NULL); + conn = osmo_stream_srv_create2(tall_test, "stream_server", srv, fd, NULL); if (conn == NULL) { LOGP(DSTREAMTEST, LOGL_ERROR, "error while creating connection\n"); return -1; } + osmo_stream_srv_set_read_cb(conn, read_cb); + osmo_stream_srv_set_closed_cb(conn, close_cb); osmo_sock_get_name_buf(buf, OSMO_SOCK_NAME_MAXLEN, fd); LOGP(DSTREAMTEST, LOGL_NOTICE, "accepted client: %s\n", buf); |