Commit fabdbf0c authored by Dafydd Harries's avatar Dafydd Harries

nice_agent_main_context_attach ()

darcs-hash:20070213123531-c9803-142ec605b66a2319b1ad0caee7b5e31426c44186.gz
parent e20deca8
......@@ -34,7 +34,8 @@ check_PROGRAMS = \
test-send \
test-stun \
test-priority \
test-poll
test-poll \
test-mainloop
TESTS = $(check_PROGRAMS)
......@@ -52,5 +53,7 @@ test_priority_LDADD = $(COMMON_LDADD)
test_poll_LDADD = $(COMMON_LDADD)
test_mainloop_LDADD = $(COMMON_LDADD)
pkginclude_HEADERS = agent.h candidate.h event.h
......@@ -966,3 +966,76 @@ nice_agent_free (NiceAgent *agent)
g_slice_free (NiceAgent, agent);
}
static gboolean
nice_agent_g_source_cb (
GIOChannel *source,
G_GNUC_UNUSED
GIOCondition condition,
gpointer data)
{
/* return value is whether to keep the source */
NiceAgent *agent = data;
NiceCandidate *candidate;
Stream *stream;
gchar buf[1024];
guint len;
candidate = _local_candidate_lookup_by_fd (agent,
g_io_channel_unix_get_fd (source));
if (candidate == NULL)
return TRUE;
stream = _stream_lookup (agent, candidate->stream_id);
if (stream == NULL)
return TRUE;
len = _nice_agent_recv (agent, stream, candidate, 1024, buf);
if (len > 0)
agent->read_func (agent, candidate->stream_id, candidate->component_id,
len, buf, agent->read_func_data);
return TRUE;
}
gboolean
nice_agent_main_context_attach (
NiceAgent *agent,
GMainContext *ctx,
NiceAgentRecvFunc func,
gpointer data)
{
if (agent->main_context_set)
return FALSE;
/* attach candidates */
{
GSList *i;
for (i = agent->local_candidates; i; i = i->next)
{
NiceCandidate *candidate = i->data;
GIOChannel *io;
GSource *source;
io = g_io_channel_unix_new (candidate->sock.fileno);
source = g_io_create_watch (io, G_IO_IN);
g_source_set_callback (source, (GSourceFunc) nice_agent_g_source_cb,
agent, NULL);
g_source_attach (source, ctx);
}
}
agent->main_context = ctx;
agent->main_context_set = TRUE;
agent->read_func = func;
agent->read_func_data = data;
return TRUE;
}
......@@ -30,6 +30,10 @@ struct _NiceAgent
GSList *remote_candidates;
GSList *streams;
GSList *events;
gboolean main_context_set;
GMainContext *main_context;
NiceAgentRecvFunc read_func;
gpointer read_func_data;
};
NiceAgent *
......@@ -88,6 +92,13 @@ const GSList *
nice_agent_get_local_candidates (
NiceAgent *agent);
gboolean
nice_agent_main_context_attach (
NiceAgent *agent,
GMainContext *ctx,
NiceAgentRecvFunc func,
gpointer data);
G_END_DECLS
#endif /* _AGENT_H */
......
#include <string.h>
#include <nice/nice.h>
static GMainLoop *loop = NULL;
static void
recv_cb (
NiceAgent *agent,
guint stream_id,
guint component_id,
guint len,
gchar *buf,
gpointer data)
{
g_assert (agent != NULL);
g_assert (stream_id == 1);
g_assert (component_id == 1);
g_assert (len == 6);
g_assert (0 == strncmp (buf, "\x80hello", len));
g_assert (42 == GPOINTER_TO_UINT (data));
g_main_loop_quit (loop);
}
int
main (void)
{
NiceAgent *agent;
NiceAddress addr = {0,};
NiceUDPSocketFactory factory;
nice_udp_fake_socket_factory_init (&factory);
agent = nice_agent_new (&factory);
nice_address_set_ipv4 (&addr, 0x7f000001);
nice_agent_add_local_address (agent, &addr);
nice_agent_add_stream (agent, 1);
// attach to default main context
nice_agent_main_context_attach (agent, NULL, recv_cb, GUINT_TO_POINTER (42));
{
NiceUDPSocket *sock;
NiceCandidate *candidate;
candidate = agent->local_candidates->data;
sock = &candidate->sock;
nice_udp_fake_socket_push_recv (sock, &addr, 6, "\x80hello");
}
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
nice_udp_socket_factory_close (&factory);
nice_agent_free (agent);
return 0;
}
......@@ -13,6 +13,7 @@ T nice_agent_add_remote_candidate
T nice_agent_add_stream
T nice_agent_free
T nice_agent_get_local_candidates
T nice_agent_main_context_attach
T nice_agent_new
T nice_agent_poll_read
T nice_agent_recv
......
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