2 * This file is part of the Nice GLib ICE library.
4 * (C) 2012 Collabora Ltd.
5 * Contact: George Kiagiadakis
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is the Nice GLib ICE library.
19 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20 * Corporation. All Rights Reserved.
23 * George Kiagiadakis, Collabora Ltd.
25 * Alternatively, the contents of this file may be used under the terms of the
26 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27 * case the provisions of LGPL are applicable instead of those above. If you
28 * wish to allow use of your version of this file only under the terms of the
29 * LGPL and not to allow others to use your version of this file under the
30 * MPL, indicate your decision by deleting the provisions above and replace
31 * them with the notice and other provisions required by the LGPL. If you do
32 * not delete the provisions above, a recipient may use your version of this
33 * file under either the MPL or the LGPL.
41 #include <gio/gnetworking.h>
45 GMainLoop *mainloop = NULL;
46 NiceSocket *active_sock, *client;
47 NiceSocket *passive_sock, *server;
52 on_server_connection_available (gpointer user_data)
54 server = nice_tcp_passive_socket_accept (passive_sock);
57 g_main_loop_quit (mainloop);
63 on_server_input_available (gpointer user_data)
65 g_assert_cmpint (5, ==, nice_socket_recv (server, &tmp, 5, buf));
66 g_assert (nice_address_equal (&tmp, &client->addr));
68 g_main_loop_quit (mainloop);
74 on_client_input_available (gpointer user_data)
76 g_assert_cmpint (5, ==, nice_socket_recv (client, &tmp, 5, buf));
77 g_assert (nice_address_equal (&tmp, &server->addr));
79 g_main_loop_quit (mainloop);
87 NiceAddress active_bind_addr, passive_bind_addr;
88 GSource *srv_listen_source, *srv_input_source, *cli_input_source;
92 mainloop = g_main_loop_new (NULL, FALSE);
94 nice_address_init (&active_bind_addr);
95 g_assert (nice_address_set_from_string (&active_bind_addr, "::1"));
97 nice_address_init (&passive_bind_addr);
98 g_assert (nice_address_set_from_string (&passive_bind_addr, "127.0.0.1"));
99 nice_address_set_port (&passive_bind_addr, 0);
101 nice_address_init (&tmp);
103 passive_sock = nice_tcp_passive_socket_new (g_main_loop_get_context (mainloop),
105 g_assert (passive_sock);
107 srv_listen_source = g_socket_create_source (passive_sock->fileno,
109 g_source_set_callback (srv_listen_source,
110 on_server_connection_available, NULL, NULL);
111 g_source_attach (srv_listen_source, g_main_loop_get_context (mainloop));
113 active_sock = nice_tcp_active_socket_new (g_main_loop_get_context (mainloop),
115 g_assert (active_sock);
117 client = nice_tcp_active_socket_connect (active_sock, &passive_sock->addr);
119 nice_socket_free (active_sock);
122 g_main_loop_run (mainloop); /* -> on_server_connection_available */
125 srv_input_source = g_socket_create_source (server->fileno, G_IO_IN, NULL);
126 g_source_set_callback (srv_input_source,
127 on_server_input_available, NULL, NULL);
128 g_source_attach (srv_input_source, g_main_loop_get_context (mainloop));
130 cli_input_source = g_socket_create_source (client->fileno, G_IO_IN, NULL);
131 g_source_set_callback (cli_input_source,
132 on_client_input_available, NULL, NULL);
133 g_source_attach (cli_input_source, g_main_loop_get_context (mainloop));
135 g_assert (nice_address_get_port (&client->addr) != 0);
137 g_assert (nice_address_set_from_string (&tmp, "127.0.0.1"));
138 nice_address_set_port (&tmp, nice_address_get_port (&server->addr));
139 g_assert (nice_address_get_port (&tmp) != 0);
142 g_assert_cmpint (5, ==, nice_socket_send (client, &tmp, 5, "hello"));
143 g_main_loop_run (mainloop); /* -> on_server_input_available */
144 g_assert (0 == strncmp (buf, "hello", 5));
146 g_assert_cmpint (5, ==, nice_socket_send (server, &tmp, 5, "uryyb"));
147 g_main_loop_run (mainloop); /* -> on_client_input_available */
148 g_assert (0 == strncmp (buf, "uryyb", 5));
150 nice_socket_free (client);
151 nice_socket_free (server);
152 nice_socket_free (passive_sock);
154 g_source_unref (srv_listen_source);
155 g_source_unref (srv_input_source);
156 g_source_unref (cli_input_source);
157 g_main_loop_unref (mainloop);