From 40c36db86fd78b18de4fed1b28663a842d962a30 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sun, 24 Oct 2010 15:39:13 -0400 Subject: [PATCH] wocky-test-stream: implement GPollableIOStream GTlsConnection can only wrap streams that implement GPollableIOStream, so implement that here to make some of the test cases work. https://bugs.freedesktop.org/show_bug.cgi?id=31447 --- tests/wocky-test-stream.c | 103 ++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 98 insertions(+), 5 deletions(-) diff --git a/tests/wocky-test-stream.c b/tests/wocky-test-stream.c index 1f955a1..f8849d6 100644 --- a/tests/wocky-test-stream.c +++ b/tests/wocky-test-stream.c @@ -80,12 +80,14 @@ typedef struct { GOutputStreamClass parent_class; } WockyTestInputStreamClass; +static void wocky_test_input_stream_pollable_interface_init (GPollableInputStreamInterface *iface); -G_DEFINE_TYPE (WockyTestIOStream, wocky_test_io_stream, G_TYPE_IO_STREAM); -G_DEFINE_TYPE (WockyTestInputStream, wocky_test_input_stream, - G_TYPE_INPUT_STREAM); -G_DEFINE_TYPE (WockyTestOutputStream, wocky_test_output_stream, - G_TYPE_OUTPUT_STREAM); +G_DEFINE_TYPE_WITH_CODE (WockyTestIOStream, wocky_test_io_stream, G_TYPE_IO_STREAM, + G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_IO_STREAM, NULL)) +G_DEFINE_TYPE_WITH_CODE (WockyTestInputStream, wocky_test_input_stream, G_TYPE_INPUT_STREAM, + G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, wocky_test_input_stream_pollable_interface_init)); +G_DEFINE_TYPE_WITH_CODE (WockyTestOutputStream, wocky_test_output_stream, G_TYPE_OUTPUT_STREAM, + G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, NULL)); #define WOCKY_TYPE_TEST_IO_STREAM (wocky_test_io_stream_get_type ()) #define WOCKY_TYPE_TEST_INPUT_STREAM (wocky_test_input_stream_get_type ()) @@ -458,6 +460,97 @@ wocky_test_input_stream_class_init ( stream_class->read_finish = wocky_test_input_stream_read_finish; } +static gboolean +wocky_test_input_stream_is_readable (GPollableInputStream *pollable) +{ + WockyTestInputStream *self = WOCKY_TEST_INPUT_STREAM (pollable); + + if (self->read_error) + return TRUE; + + if (self->out_array == NULL && g_async_queue_length (self->queue) == 0) + return FALSE; + + if (self->corked) + return FALSE; + + return TRUE; +} + +typedef struct { + GSource source; + + WockyTestInputStream *self; +} WockyTestInputStreamSource; + +static gboolean +wocky_test_input_stream_source_prepare (GSource *source, + gint *timeout) +{ + WockyTestInputStreamSource *wsource = (WockyTestInputStreamSource *) source; + + *timeout = -1; + return g_pollable_input_stream_is_readable (G_POLLABLE_INPUT_STREAM (wsource->self)); +} + +static gboolean +wocky_test_input_stream_source_check (GSource *source) +{ + WockyTestInputStreamSource *wsource = (WockyTestInputStreamSource *) source; + + return g_pollable_input_stream_is_readable (G_POLLABLE_INPUT_STREAM (wsource->self)); +} + +static gboolean +wocky_test_input_stream_source_dispatch (GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + GPollableSourceFunc func = (GPollableSourceFunc)callback; + WockyTestInputStreamSource *wsource = (WockyTestInputStreamSource *) source; + + return (*func) (G_OBJECT (wsource->self), user_data); +} + +static void +wocky_test_input_stream_source_finalize (GSource *source) +{ + WockyTestInputStreamSource *wsource = (WockyTestInputStreamSource *) source; + + g_object_unref (wsource->self); +} + +static GSourceFuncs wocky_test_input_stream_source_funcs = +{ + wocky_test_input_stream_source_prepare, + wocky_test_input_stream_source_check, + wocky_test_input_stream_source_dispatch, + wocky_test_input_stream_source_finalize +}; + +static GSource * +wocky_test_input_stream_create_source (GPollableInputStream *pollable, + GCancellable *cancellable) +{ + WockyTestInputStream *self = WOCKY_TEST_INPUT_STREAM (pollable); + WockyTestInputStreamSource *wsource; + GSource *source; + + source = g_source_new (&wocky_test_input_stream_source_funcs, + sizeof (WockyTestInputStreamSource)); + wsource = (WockyTestInputStreamSource *) source; + wsource->self = g_object_ref (self); + + return source; +} + +static void +wocky_test_input_stream_pollable_interface_init (GPollableInputStreamInterface *iface) +{ + iface->is_readable = wocky_test_input_stream_is_readable; + iface->create_source = wocky_test_input_stream_create_source; +} + /* Output stream */ enum { -- 1.7.3.1