vdagent FD_SET -> FD_ISSET
[vd_agent.git] / udscs.h
blobe7a9ee02b56396aba3b8ca4e627c332215ff147a
1 /* udscs.h Unix Domain Socket Client Server framework header file
3 Copyright 2010 Red Hat, Inc.
5 Red Hat Authors:
6 Hans de Goede <hdegoede@redhat.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef __UDSCS_H
23 #define __UDSCS_H
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <sys/select.h>
29 struct udscs_connection;
30 struct udscs_server;
31 struct udscs_message_header {
32 uint32_t type;
33 uint32_t opaque;
34 uint32_t size;
37 /* Callbacks with this type will be called when a new connection to a
38 server is accepted. */
39 typedef void (*udscs_connect_callback)(struct udscs_connection *conn);
40 /* Callbacks with this type will be called when a complete message has been
41 received. Sometimes the callback may want to close the connection, in this
42 case do *not* call udscs_destroy_connection from the callback. The desire
43 to close the connection can be indicated be returning -1 from the callback,
44 in other cases return 0. */
45 typedef int (*udscs_read_callback)(struct udscs_connection *conn,
46 struct udscs_message_header *header, const uint8_t *data);
47 /* Callbacks with this type will be called when the connection is disconnected.
48 Note:
49 1) udscs will destroy the connection in question itself after
50 this callback has completed!
51 2) This callback is always called, even if the disconnect is initiated
52 by the udscs user through returning -1 from a read callback, or
53 by explictly calling udscs_destroy_connection */
54 typedef void (*udscs_disconnect_callback)(struct udscs_connection *conn);
56 /* Create a unix domain socket named name and start listening on it. */
57 struct udscs_server *udscs_create_server(const char *socketname,
58 udscs_connect_callback connect_callback,
59 udscs_read_callback read_callback,
60 udscs_disconnect_callback disconnect_callback,
61 const char * const type_to_string[], int no_types,
62 FILE *logfile, FILE *errfile);
64 void udscs_destroy_server(struct udscs_server *server);
66 /* Connect to a unix domain socket named name. */
67 struct udscs_connection *udscs_connect(const char *socketname,
68 udscs_read_callback read_callback,
69 udscs_disconnect_callback disconnect_callback,
70 const char * const type_to_string[], int no_types,
71 FILE *logfile, FILE *errfile);
73 /* The contents of connp will be made NULL */
74 void udscs_destroy_connection(struct udscs_connection **connp);
77 /* Given an udscs server or client fill the fd_sets pointed to by readfds and
78 writefds for select() usage.
80 Return value: value of the highest fd + 1 */
81 int udscs_server_fill_fds(struct udscs_server *server, fd_set *readfds,
82 fd_set *writefds);
84 int udscs_client_fill_fds(struct udscs_connection *conn, fd_set *readfds,
85 fd_set *writefds);
87 /* Handle any events flagged by select for the given udscs server or client. */
88 void udscs_server_handle_fds(struct udscs_server *server, fd_set *readfds,
89 fd_set *writefds);
91 /* Note the connection may be destroyed (when disconnected) by this call
92 in this case the disconnect calllback will get called before the
93 destruction and the contents of connp will be made NULL */
94 void udscs_client_handle_fds(struct udscs_connection **connp, fd_set *readfds,
95 fd_set *writefds);
98 /* Queue a message for delivery to the client connected through conn.
100 Returns 0 on success -1 on error (only happens when malloc fails) */
101 int udscs_write(struct udscs_connection *conn, uint32_t type, uint32_t opaque,
102 const uint8_t *data, uint32_t size);
104 /* Like udscs_write, but then send the message to all clients connected to
105 the server */
106 int udscs_server_write_all(struct udscs_server *server,
107 uint32_t type, uint32_t opaque,
108 const uint8_t *data, uint32_t size);
110 #endif