Commit 68a9632e authored by Philip Withnall's avatar Philip Withnall Committed by Olivier Crête

agent: Integrate TcpUserData into Component

It was allocated separately and always set, which is a wasted
allocation. Instead, pull the NiceAgent and Stream pointers into the
Component directly, and eliminate the redundant allocation.

This also means the NiceAgent and Stream are available for use elsewhere
in the Component code (not just with TCP stuff).
parent 80716d38
...@@ -995,10 +995,6 @@ static void priv_destroy_component_tcp (Component *component) ...@@ -995,10 +995,6 @@ static void priv_destroy_component_tcp (Component *component)
g_object_unref (component->tcp); g_object_unref (component->tcp);
component->tcp = NULL; component->tcp = NULL;
} }
if (component->tcp_data != NULL) {
g_slice_free (TcpUserData, component->tcp_data);
component->tcp_data = NULL;
}
} }
static void priv_pseudo_tcp_error (NiceAgent *agent, Stream *stream, static void priv_pseudo_tcp_error (NiceAgent *agent, Stream *stream,
...@@ -1019,12 +1015,11 @@ adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component); ...@@ -1019,12 +1015,11 @@ adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component);
static void static void
pseudo_tcp_socket_opened (PseudoTcpSocket *sock, gpointer user_data) pseudo_tcp_socket_opened (PseudoTcpSocket *sock, gpointer user_data)
{ {
TcpUserData *data = (TcpUserData *)user_data; Component *component = user_data;
NiceAgent *agent = data->agent; NiceAgent *agent = component->agent;
Component *component = data->component; Stream *stream = component->stream;
Stream *stream = data->stream;
nice_debug ("Agent %p: s%d:%d pseudo Tcp socket Opened", data->agent, nice_debug ("Agent %p: s%d:%d pseudo Tcp socket Opened", agent,
stream->id, component->id); stream->id, component->id);
g_signal_emit (agent, signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE], 0, g_signal_emit (agent, signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE], 0,
stream->id, component->id); stream->id, component->id);
...@@ -1033,10 +1028,9 @@ pseudo_tcp_socket_opened (PseudoTcpSocket *sock, gpointer user_data) ...@@ -1033,10 +1028,9 @@ pseudo_tcp_socket_opened (PseudoTcpSocket *sock, gpointer user_data)
static void static void
pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data) pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data)
{ {
TcpUserData *data = (TcpUserData *)user_data; Component *component = user_data;
NiceAgent *agent = data->agent; NiceAgent *agent = component->agent;
Component *component = data->component; Stream *stream = component->stream;
Stream *stream = data->stream;
guint8 buf[MAX_BUFFER_SIZE]; guint8 buf[MAX_BUFFER_SIZE];
gssize len; gssize len;
...@@ -1086,13 +1080,12 @@ pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data) ...@@ -1086,13 +1080,12 @@ pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data)
static void static void
pseudo_tcp_socket_writable (PseudoTcpSocket *sock, gpointer user_data) pseudo_tcp_socket_writable (PseudoTcpSocket *sock, gpointer user_data)
{ {
TcpUserData *data = (TcpUserData *)user_data; Component *component = user_data;
NiceAgent *agent = data->agent; NiceAgent *agent = component->agent;
Component *component = data->component; Stream *stream = component->stream;
Stream *stream = data->stream;
nice_debug ("Agent %p: s%d:%d pseudo Tcp socket writable", data->agent, nice_debug ("Agent %p: s%d:%d pseudo Tcp socket writable", agent,
data->stream->id, data->component->id); stream->id, component->id);
g_signal_emit (agent, signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE], 0, g_signal_emit (agent, signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE], 0,
stream->id, component->id); stream->id, component->id);
} }
...@@ -1101,10 +1094,9 @@ static void ...@@ -1101,10 +1094,9 @@ static void
pseudo_tcp_socket_closed (PseudoTcpSocket *sock, guint32 err, pseudo_tcp_socket_closed (PseudoTcpSocket *sock, guint32 err,
gpointer user_data) gpointer user_data)
{ {
TcpUserData *data = (TcpUserData *)user_data; Component *component = user_data;
NiceAgent *agent = data->agent; NiceAgent *agent = component->agent;
Component *component = data->component; Stream *stream = component->stream;
Stream *stream = data->stream;
nice_debug ("Agent %p: s%d:%d pseudo Tcp socket closed", agent, nice_debug ("Agent %p: s%d:%d pseudo Tcp socket closed", agent,
stream->id, component->id); stream->id, component->id);
...@@ -1116,8 +1108,7 @@ static PseudoTcpWriteResult ...@@ -1116,8 +1108,7 @@ static PseudoTcpWriteResult
pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket, pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket,
const gchar *buffer, guint32 len, gpointer user_data) const gchar *buffer, guint32 len, gpointer user_data)
{ {
TcpUserData *data = (TcpUserData *)user_data; Component *component = user_data;
Component *component = data->component;
if (component->selected_pair.local != NULL) { if (component->selected_pair.local != NULL) {
NiceSocket *sock; NiceSocket *sock;
...@@ -1127,8 +1118,8 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket, ...@@ -1127,8 +1118,8 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket,
gchar tmpbuf[INET6_ADDRSTRLEN]; gchar tmpbuf[INET6_ADDRSTRLEN];
nice_address_to_string (&component->selected_pair.remote->addr, tmpbuf); nice_address_to_string (&component->selected_pair.remote->addr, tmpbuf);
nice_debug ("Agent %p : s%d:%d: sending %d bytes to [%s]:%d", data->agent, nice_debug ("Agent %p : s%d:%d: sending %d bytes to [%s]:%d",
data->stream->id, component->id, len, tmpbuf, component->agent, component->stream->id, component->id, len, tmpbuf,
nice_address_get_port (&component->selected_pair.remote->addr)); nice_address_get_port (&component->selected_pair.remote->addr));
#endif #endif
...@@ -1146,10 +1137,9 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket, ...@@ -1146,10 +1137,9 @@ pseudo_tcp_socket_write_packet (PseudoTcpSocket *socket,
static gboolean static gboolean
notify_pseudo_tcp_socket_clock (gpointer user_data) notify_pseudo_tcp_socket_clock (gpointer user_data)
{ {
TcpUserData *data = (TcpUserData *)user_data; Component *component = user_data;
Component *component = data->component; Stream *stream = component->stream;
Stream *stream = data->stream; NiceAgent *agent = component->agent;
NiceAgent *agent = data->agent;
agent_lock(); agent_lock();
...@@ -1185,7 +1175,7 @@ adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component) ...@@ -1185,7 +1175,7 @@ adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component)
component->tcp_clock = NULL; component->tcp_clock = NULL;
} }
component->tcp_clock = agent_timeout_add_with_context (agent, component->tcp_clock = agent_timeout_add_with_context (agent,
timeout, notify_pseudo_tcp_socket_clock, component->tcp_data); timeout, notify_pseudo_tcp_socket_clock, component);
} else { } else {
nice_debug ("Agent %p: component %d pseudo tcp socket should be destroyed", nice_debug ("Agent %p: component %d pseudo tcp socket should be destroyed",
agent, component->id); agent, component->id);
...@@ -1531,7 +1521,7 @@ nice_agent_add_stream ( ...@@ -1531,7 +1521,7 @@ nice_agent_add_stream (
guint i; guint i;
agent_lock(); agent_lock();
stream = stream_new (n_components); stream = stream_new (n_components, agent);
agent->streams = g_slist_append (agent->streams, stream); agent->streams = g_slist_append (agent->streams, stream);
stream->id = agent->next_stream_id++; stream->id = agent->next_stream_id++;
...@@ -1541,17 +1531,12 @@ nice_agent_add_stream ( ...@@ -1541,17 +1531,12 @@ nice_agent_add_stream (
for (i = 0; i < n_components; i++) { for (i = 0; i < n_components; i++) {
Component *component = stream_find_component_by_id (stream, i + 1); Component *component = stream_find_component_by_id (stream, i + 1);
if (component) { if (component) {
TcpUserData *data = g_slice_new0 (TcpUserData); PseudoTcpCallbacks tcp_callbacks = {component,
PseudoTcpCallbacks tcp_callbacks = {data,
pseudo_tcp_socket_opened, pseudo_tcp_socket_opened,
pseudo_tcp_socket_readable, pseudo_tcp_socket_readable,
pseudo_tcp_socket_writable, pseudo_tcp_socket_writable,
pseudo_tcp_socket_closed, pseudo_tcp_socket_closed,
pseudo_tcp_socket_write_packet}; pseudo_tcp_socket_write_packet};
data->agent = agent;
data->stream = stream;
data->component = component;
component->tcp_data = data;
component->tcp = pseudo_tcp_socket_new (0, &tcp_callbacks); component->tcp = pseudo_tcp_socket_new (0, &tcp_callbacks);
adjust_tcp_clock (agent, stream, component); adjust_tcp_clock (agent, stream, component);
nice_debug ("Agent %p: Create Pseudo Tcp Socket for component %d", nice_debug ("Agent %p: Create Pseudo Tcp Socket for component %d",
...@@ -2807,8 +2792,8 @@ nice_agent_attach_recv ( ...@@ -2807,8 +2792,8 @@ nice_agent_attach_recv (
* next incoming data. * next incoming data.
* but only do this if we know we're already readable, otherwise we might * but only do this if we know we're already readable, otherwise we might
* trigger an error in the initial, pre-connection attach. */ * trigger an error in the initial, pre-connection attach. */
if (component->tcp && component->tcp_data && component->tcp_readable) if (component->tcp && component->tcp_readable)
pseudo_tcp_socket_readable (component->tcp, component->tcp_data); pseudo_tcp_socket_readable (component->tcp, component);
} }
done: done:
......
...@@ -75,7 +75,7 @@ socket_source_free (SocketSource *source) ...@@ -75,7 +75,7 @@ socket_source_free (SocketSource *source)
Component * Component *
component_new (guint id) component_new (guint id, NiceAgent *agent, Stream *stream)
{ {
Component *component; Component *component;
...@@ -84,6 +84,8 @@ component_new (guint id) ...@@ -84,6 +84,8 @@ component_new (guint id)
component->state = NICE_COMPONENT_STATE_DISCONNECTED; component->state = NICE_COMPONENT_STATE_DISCONNECTED;
component->restart_candidate = NULL; component->restart_candidate = NULL;
component->tcp = NULL; component->tcp = NULL;
component->agent = agent;
component->stream = stream;
return component; return component;
} }
...@@ -143,10 +145,6 @@ component_free (Component *cmp) ...@@ -143,10 +145,6 @@ component_free (Component *cmp)
g_object_unref (cmp->tcp); g_object_unref (cmp->tcp);
cmp->tcp = NULL; cmp->tcp = NULL;
} }
if (cmp->tcp_data != NULL) {
g_slice_free (TcpUserData, cmp->tcp_data);
cmp->tcp_data = NULL;
}
if (cmp->ctx != NULL) { if (cmp->ctx != NULL) {
g_main_context_unref (cmp->ctx); g_main_context_unref (cmp->ctx);
......
...@@ -95,12 +95,6 @@ struct _IncomingCheck ...@@ -95,12 +95,6 @@ struct _IncomingCheck
uint16_t username_len; uint16_t username_len;
}; };
typedef struct {
NiceAgent *agent;
Stream *stream;
Component *component;
} TcpUserData;
/* A pair of a socket and the GSource which polls it from the main loop. All /* A pair of a socket and the GSource which polls it from the main loop. All
* GSources in a Component must be attached to the same main context: * GSources in a Component must be attached to the same main context:
* component->ctx. * component->ctx.
...@@ -130,16 +124,19 @@ struct _Component ...@@ -130,16 +124,19 @@ struct _Component
GMainContext *ctx; /**< context for GSources for this GMainContext *ctx; /**< context for GSources for this
component */ component */
NiceAgent *agent; /* unowned */
Stream *stream; /* unowned */
PseudoTcpSocket *tcp; PseudoTcpSocket *tcp;
GSource* tcp_clock; GSource* tcp_clock;
TcpUserData *tcp_data;
gboolean tcp_readable; gboolean tcp_readable;
guint min_port; guint min_port;
guint max_port; guint max_port;
}; };
Component * Component *
component_new (guint component_id); component_new (guint component_id, NiceAgent *agent, Stream *stream);
void void
component_free (Component *cmp); component_free (Component *cmp);
......
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
* @brief ICE stream functionality * @brief ICE stream functionality
*/ */
Stream * Stream *
stream_new (guint n_components) stream_new (guint n_components, NiceAgent *agent)
{ {
Stream *stream; Stream *stream;
guint n; guint n;
...@@ -56,7 +56,7 @@ stream_new (guint n_components) ...@@ -56,7 +56,7 @@ stream_new (guint n_components)
stream = g_slice_new0 (Stream); stream = g_slice_new0 (Stream);
for (n = 0; n < n_components; n++) { for (n = 0; n < n_components; n++) {
component = component_new (n + 1); component = component_new (n + 1, agent, stream);
stream->components = g_slist_append (stream->components, component); stream->components = g_slist_append (stream->components, component);
} }
......
...@@ -77,7 +77,7 @@ struct _Stream ...@@ -77,7 +77,7 @@ struct _Stream
Stream * Stream *
stream_new (guint n_components); stream_new (guint n_components, NiceAgent *agent);
void void
stream_free (Stream *stream); stream_free (Stream *stream);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment