Debian Codegraph @

lib/packagekit-glib2/pk-socket-example.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <gio/gio.h>
23 #include <gio/gunixsocketaddress.h>
24
25 static gboolean
26 pk_socket_example_accept_connection_cb (GSocket *socket, GIOCondition condition, gpointer user_data)
27 {
28 gsize len;
29 gchar buffer[1024];
30 GMainLoop *loop = (GMainLoop *) user_data;
31 g_autoptr(GError) error = NULL;
32
33 /* the helper process exited */
34 if ((condition & G_IO_HUP) > 0) {
35 g_warning ("socket was disconnected");
36 g_main_loop_quit (loop);
37 return FALSE;
38 }
39
40 /* there is data */
41 if ((condition & G_IO_IN) > 0) {
42 len = g_socket_receive (socket, buffer, 1024, NULL, &error);
43 if (error != NULL) {
44 g_warning ("failed to get data: %s", error->message);
45 return FALSE;
46 }
47 if (len == 0)
48 return TRUE;
49 g_debug ("got data: %s : %" G_GSIZE_FORMAT, buffer, len);
50 }
51 return TRUE;
52 }
53
54 gint
55 main (void)
56 {
57 gboolean ret;
58 gsize wrote;
59 const gchar *buffer = "ping\n";
60 const gchar *socket_filename = "/tmp/pk-self-test.socket";
61 GSource *source;
62 g_autoptr(GError) error = NULL;
63 g_autoptr(GMainLoop) loop = NULL;
64 g_autoptr(GSocketAddress) address = NULL;
65 g_autoptr(GSocket) socket = NULL;
66
67 loop = g_main_loop_new (NULL, FALSE);
68
69 /* create socket */
70 socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
71 if (socket == NULL) {
72 g_warning ("failed to create socket: %s", error->message);
73 return 1;
74 }
75 g_socket_set_blocking (socket, FALSE);
76 g_socket_set_keepalive (socket, TRUE);
77
78 /* connect to it */
79 address = g_unix_socket_address_new (socket_filename);
80 ret = g_socket_connect (socket, address, NULL, &error);
81 if (!ret) {
82 g_warning ("failed to connect to socket: %s", error->message);
83 return 1;
84 }
85
86 /* socket has data */
87 source = g_socket_create_source (socket, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL);
88 g_source_set_callback (source, G_SOURCE_FUNC (pk_socket_example_accept_connection_cb), loop, NULL);
89 g_source_attach (source, NULL);
90
91 /* send some data */
92 wrote = g_socket_send (socket, buffer, 5, NULL, &error);
93 if (wrote != 5) {
94 g_warning ("failed to write 5 bytes");
95 return 1;
96 }
97
98 /* run main loop */
99 g_debug ("running main loop");
100 g_main_loop_run (loop);
101 return 0;
102 }