From 430e8db5076bef6e112d34fa07b51ed0a6ded49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Cr=C3=AAte?= Date: Thu, 30 Jan 2014 21:59:33 -0500 Subject: [PATCH] agent: Replace nice_agent_build_io_stream() with nice_agent_get_io_stream() Also made the GIOStream into a singleton, it always returns the same one. Also make it impossible to create a GIOStream for a non-existing stream/component --- agent/agent.c | 39 +++++++++++++------------- agent/agent.h | 15 +++++----- agent/component.c | 2 ++ agent/component.h | 2 ++ nice/libnice.sym | 2 +- tests/test-build-io-stream.c | 53 ++++++----------------------------- tests/test-io-stream-common.c | 2 +- 7 files changed, 42 insertions(+), 73 deletions(-) diff --git a/agent/agent.c b/agent/agent.c index 0c69e336..7e6af0d0 100644 --- a/agent/agent.c +++ b/agent/agent.c @@ -4465,33 +4465,32 @@ nice_agent_parse_remote_candidate_sdp (NiceAgent *agent, guint stream_id, return candidate; } -/** - * nice_agent_build_io_stream: - * @agent: A reliable #NiceAgent - * @stream_id: The ID of the agent’s stream to wrap - * @component_id: The ID of the agent’s component to wrap - * - * Create a new #NiceIOStream wrapping the given stream/component from @agent, - * which must be a reliable #NiceAgent. The given stream/component must be - * created using nice_agent_add_stream() before any I/O operations are performed - * on the #NiceIOStream. - * - * Note that if multiple I/O streams are created for a single stream/component, - * only one of them may be used at any time. - * - * Returns: (transfer full): The new #NiceIOStream object - * - * Since: 0.1.5 - */ + NICEAPI_EXPORT GIOStream * -nice_agent_build_io_stream (NiceAgent *agent, guint stream_id, +nice_agent_get_io_stream (NiceAgent *agent, guint stream_id, guint component_id) { + GIOStream *iostream = NULL; + Component *component; + g_return_val_if_fail (NICE_IS_AGENT (agent), NULL); g_return_val_if_fail (stream_id >= 1, NULL); g_return_val_if_fail (component_id >= 1, NULL); g_return_val_if_fail (agent->reliable, NULL); - return nice_io_stream_new (agent, stream_id, component_id); + agent_lock (); + + if (!agent_find_component (agent, stream_id, component_id, NULL, &component)) + goto done; + + if (component->iostream == NULL) + component->iostream = nice_io_stream_new (agent, stream_id, component_id); + + iostream = g_object_ref (component->iostream); + + done: + agent_unlock (); + + return iostream; } diff --git a/agent/agent.h b/agent/agent.h index bd10bec7..f0acd140 100644 --- a/agent/agent.h +++ b/agent/agent.h @@ -60,7 +60,7 @@ * Each stream can receive data in one of two ways: using * nice_agent_attach_recv() or nice_agent_recv_messages() (and the derived * #NiceInputStream and #NiceIOStream classes accessible using - * nice_agent_build_io_stream()). nice_agent_attach_recv() is non-blocking: it + * nice_agent_get_io_stream()). nice_agent_attach_recv() is non-blocking: it * takes a user-provided callback function and attaches the stream’s socket to * the provided #GMainContext, invoking the callback in that context for every * packet received. nice_agent_recv_messages() instead blocks on receiving a @@ -1373,25 +1373,26 @@ nice_agent_parse_remote_candidate_sdp ( const gchar *sdp); /** - * nice_agent_build_io_stream: + * nice_agent_get_io_stream: * @agent: A #NiceAgent * @stream_id: The ID of the stream to wrap * @component_id: The ID of the component to wrap * - * Build a #GIOStream wrapper around the given stream and component in + * Gets a #GIOStream wrapper around the given stream and component in * @agent. The I/O stream will be valid for as long as @stream_id is valid. * The #GInputStream and #GOutputStream implement #GPollableInputStream and * #GPollableOutputStream. * - * This function may only be called on reliable #NiceAgents. It is an error to - * try and create an I/O stream wrapper for an unreliable stream. + * This function may only be called on reliable #NiceAgents. It is a + * programming error to try and create an I/O stream wrapper for an + * unreliable stream. * - * Returns: (transfer full): A new #GIOStream. + * Returns: (transfer full): A #GIOStream. * * Since: 0.1.5 */ GIOStream * -nice_agent_build_io_stream ( +nice_agent_get_io_stream ( NiceAgent *agent, guint stream_id, guint component_id); diff --git a/agent/component.c b/agent/component.c index 9060a15f..a2044f0a 100644 --- a/agent/component.c +++ b/agent/component.c @@ -209,6 +209,8 @@ component_free (Component *cmp) g_slice_free (GOutputVector, vec); } + g_clear_object (&cmp->iostream); + g_mutex_clear (&cmp->io_mutex); g_slice_free (Component, cmp); diff --git a/agent/component.h b/agent/component.h index d76de0fd..5c011583 100644 --- a/agent/component.h +++ b/agent/component.h @@ -190,6 +190,8 @@ struct _Component gboolean tcp_readable; GCancellable *tcp_writable_cancellable; + GIOStream *iostream; + guint min_port; guint max_port; diff --git a/nice/libnice.sym b/nice/libnice.sym index b1d0d157..d083d9e4 100644 --- a/nice/libnice.sym +++ b/nice/libnice.sym @@ -16,7 +16,6 @@ nice_address_set_port nice_address_to_string nice_agent_add_local_address nice_agent_add_stream -nice_agent_build_io_stream nice_agent_recv nice_agent_recv_messages nice_agent_recv_nonblocking @@ -27,6 +26,7 @@ nice_agent_generate_local_candidate_sdp nice_agent_generate_local_sdp nice_agent_generate_local_stream_sdp nice_agent_get_default_local_candidate +nice_agent_get_io_stream nice_agent_get_local_candidates nice_agent_get_local_credentials nice_agent_get_remote_candidates diff --git a/tests/test-build-io-stream.c b/tests/test-build-io-stream.c index f9e45361..312e80aa 100644 --- a/tests/test-build-io-stream.c +++ b/tests/test-build-io-stream.c @@ -47,39 +47,18 @@ test_invalid_stream (NiceAddress *addr) { NiceAgent *agent; GIOStream *io_stream; - GInputStream *input_stream; - GOutputStream *output_stream; - uint8_t data[65536]; - GError *error = NULL; agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245); nice_agent_add_local_address (agent, addr); /* Try building an I/O stream for an invalid stream. All its operations should * return G_IO_ERROR_BROKEN_PIPE. */ - io_stream = nice_agent_build_io_stream (agent, 5, 5); - g_assert (G_IS_IO_STREAM (io_stream)); - g_assert (NICE_IS_IO_STREAM (io_stream)); - - input_stream = g_io_stream_get_input_stream (G_IO_STREAM (io_stream)); - g_assert ( - g_input_stream_read (input_stream, data, G_N_ELEMENTS (data), - NULL, &error) == -1); - g_assert_error (error, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE); - g_clear_error (&error); - - output_stream = g_io_stream_get_output_stream (G_IO_STREAM (io_stream)); - g_assert ( - g_output_stream_write (output_stream, data, G_N_ELEMENTS (data), - NULL, &error) == -1); - g_assert_error (error, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE); - g_clear_error (&error); - - g_object_unref (io_stream); + io_stream = nice_agent_get_io_stream (agent, 5, 5); + g_assert (io_stream == NULL); } static void -test_io_stream_properties (NiceAddress *addr, gboolean add_stream_first) +test_io_stream_properties (NiceAddress *addr) { NiceAgent *agent; guint stream_id; @@ -90,26 +69,13 @@ test_io_stream_properties (NiceAddress *addr, gboolean add_stream_first) agent = nice_agent_new_reliable (NULL, NICE_COMPATIBILITY_RFC5245); nice_agent_add_local_address (agent, addr); - if (add_stream_first) { - /* Add a stream. */ - stream_id = nice_agent_add_stream (agent, 1); - } else { - /* Guess at the stream ID, then check it later. This tests the case where - * the I/O stream is built before the stream is created in the NiceAgent - * (but no I/O operations are performed on it until after both have been - * created). */ - stream_id = 1; - } + stream_id = nice_agent_add_stream (agent, 1); /* Try building an I/O stream around it. */ - io_stream = nice_agent_build_io_stream (agent, stream_id, 1); + io_stream = nice_agent_get_io_stream (agent, stream_id, 1); g_assert (G_IS_IO_STREAM (io_stream)); g_assert (NICE_IS_IO_STREAM (io_stream)); - if (!add_stream_first) { - g_assert (stream_id == nice_agent_add_stream (agent, 1)); - } - /* Check various initial properties. */ g_assert (!g_io_stream_is_closed (G_IO_STREAM (io_stream))); g_assert (!g_io_stream_has_pending (G_IO_STREAM (io_stream))); @@ -163,7 +129,7 @@ test_pollable_properties (NiceAddress *addr) stream_id = nice_agent_add_stream (agent, 1); /* Try building an I/O stream around it. */ - io_stream = nice_agent_build_io_stream (agent, stream_id, 1); + io_stream = nice_agent_get_io_stream (agent, stream_id, 1); g_assert (G_IS_IO_STREAM (io_stream)); g_assert (NICE_IS_IO_STREAM (io_stream)); @@ -334,7 +300,7 @@ test_pollable_cancellation (NiceAddress *addr) stream_id = nice_agent_add_stream (agent, 1); /* Try building an I/O stream around it. */ - io_stream = nice_agent_build_io_stream (agent, stream_id, 1); + io_stream = nice_agent_get_io_stream (agent, stream_id, 1); g_assert (G_IS_IO_STREAM (io_stream)); g_assert (NICE_IS_IO_STREAM (io_stream)); @@ -410,7 +376,7 @@ test_zero_length_reads_writes (NiceAddress *addr) stream_id = nice_agent_add_stream (agent, 1); /* Try building an I/O stream around it. */ - io_stream = nice_agent_build_io_stream (agent, stream_id, 1); + io_stream = nice_agent_get_io_stream (agent, stream_id, 1); g_assert (G_IS_IO_STREAM (io_stream)); g_assert (NICE_IS_IO_STREAM (io_stream)); @@ -477,8 +443,7 @@ main (void) g_assert (nice_address_set_from_string (&addr, "127.0.0.1")); test_invalid_stream (&addr); - test_io_stream_properties (&addr, TRUE); - test_io_stream_properties (&addr, FALSE); + test_io_stream_properties (&addr); test_pollable_properties (&addr); test_pollable_cancellation (&addr); test_zero_length_reads_writes (&addr); diff --git a/tests/test-io-stream-common.c b/tests/test-io-stream-common.c index fe63db89..b74e9d94 100644 --- a/tests/test-io-stream-common.c +++ b/tests/test-io-stream-common.c @@ -292,7 +292,7 @@ run_agent (TestIOStreamThreadData *data, NiceAgent *agent) if (data->reliable) { data->io_stream = - G_IO_STREAM (nice_agent_build_io_stream (agent, stream_id, 1)); + G_IO_STREAM (nice_agent_get_io_stream (agent, stream_id, 1)); g_object_set_data (G_OBJECT (agent), "io-stream", data->io_stream); } else { data->io_stream = NULL; -- GitLab