summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorYouness Alaoui <youness.alaoui@collabora.co.uk>2013-02-06 20:27:32 -0500
committerYouness Alaoui <youness.alaoui@collabora.co.uk>2013-02-06 20:27:32 -0500
commit030270775d9fc8d4f0381361b2ded0a38eb727b9 (patch)
tree378ebde5cd5ca3c08fb71e73079f466283fcb66e /examples
parent73e28d8aa6255b48184180dceff92a152fab725e (diff)
downloadlibnice-030270775d9fc8d4f0381361b2ded0a38eb727b9.tar.gz
examples: Make input non blocking to allow exit when remote hangs up
Diffstat (limited to 'examples')
-rw-r--r--examples/sdp-example.c15
-rw-r--r--examples/threaded-example.c14
2 files changed, 21 insertions, 8 deletions
diff --git a/examples/sdp-example.c b/examples/sdp-example.c
index caf1cc2..7092156 100644
--- a/examples/sdp-example.c
+++ b/examples/sdp-example.c
@@ -41,6 +41,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include <unistd.h>
#include <agent.h>
@@ -117,6 +118,7 @@ example_thread(void *data)
gchar *sdp, *sdp64;
io_stdin = g_io_channel_unix_new(fileno(stdin));
+ g_io_channel_set_flags (io_stdin, G_IO_FLAG_NONBLOCK, NULL);
// Create the nice agent
agent = nice_agent_new(g_main_loop_get_context (gloop),
@@ -174,8 +176,8 @@ example_thread(void *data)
printf("> ");
fflush (stdout);
while (!exit_thread) {
- if (g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL) ==
- G_IO_STATUS_NORMAL) {
+ GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
+ if (s == G_IO_STATUS_NORMAL) {
gsize sdp_len;
sdp = (gchar *) g_base64_decode (line, &sdp_len);
@@ -193,6 +195,8 @@ example_thread(void *data)
}
g_free (sdp);
g_free (line);
+ } else if (s == G_IO_STATUS_AGAIN) {
+ usleep (100000);
}
}
@@ -209,12 +213,15 @@ example_thread(void *data)
printf("> ");
fflush (stdout);
while (!exit_thread) {
- if (g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL) ==
- G_IO_STATUS_NORMAL) {
+ GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
+
+ if (s == G_IO_STATUS_NORMAL) {
nice_agent_send(agent, stream_id, 1, strlen(line), line);
g_free (line);
printf("> ");
fflush (stdout);
+ } else if (s == G_IO_STATUS_AGAIN) {
+ usleep (100000);
} else {
// Ctrl-D was pressed.
nice_agent_send(agent, stream_id, 1, 1, "\0");
diff --git a/examples/threaded-example.c b/examples/threaded-example.c
index c102b0c..d4513bc 100644
--- a/examples/threaded-example.c
+++ b/examples/threaded-example.c
@@ -41,6 +41,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include <unistd.h>
#include <agent.h>
@@ -127,6 +128,7 @@ example_thread(void *data)
int rval;
io_stdin = g_io_channel_unix_new(fileno(stdin));
+ g_io_channel_set_flags (io_stdin, G_IO_FLAG_NONBLOCK, NULL);
// Create the nice agent
agent = nice_agent_new(g_main_loop_get_context (gloop),
@@ -183,8 +185,8 @@ example_thread(void *data)
printf("> ");
fflush (stdout);
while (!exit_thread) {
- if (g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL) ==
- G_IO_STATUS_NORMAL) {
+ GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
+ if (s == G_IO_STATUS_NORMAL) {
// Parse remote candidate list and set it on the agent
rval = parse_remote_data(agent, stream_id, 1, line);
if (rval == EXIT_SUCCESS) {
@@ -197,6 +199,8 @@ example_thread(void *data)
fflush (stdout);
}
g_free (line);
+ } else if (s == G_IO_STATUS_AGAIN) {
+ usleep (100000);
}
}
@@ -225,12 +229,14 @@ example_thread(void *data)
printf("> ");
fflush (stdout);
while (!exit_thread) {
- if (g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL) ==
- G_IO_STATUS_NORMAL) {
+ GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL);
+ if (s == G_IO_STATUS_NORMAL) {
nice_agent_send(agent, stream_id, 1, strlen(line), line);
g_free (line);
printf("> ");
fflush (stdout);
+ } else if (s == G_IO_STATUS_AGAIN) {
+ usleep (100000);
} else {
// Ctrl-D was pressed.
nice_agent_send(agent, stream_id, 1, 1, "\0");