[Spice-devel] [PATCH vdagent 06/11] udscs: add userdata to read_callback

Jakub Janků janku.jakub.jj at gmail.com
Tue Sep 26 20:59:42 UTC 2017


From: Victor Toso <me at victortoso.com>

Introduce new parameter to the functions:
  * udscs_create_server() - only used in vdagentd.c
  * udscs_connect() - only used in vdagent.c

Related changes were necessary to the predefined callback types and
its implementations to include this new parameter.

This change intends to remove some global variables from vdagent.c and
vdagentd.c

Signed-off-by: Victor Toso <victortoso at redhat.com>
---
 src/udscs.c             | 14 ++++++++++++--
 src/udscs.h             |  5 ++++-
 src/vdagent/vdagent.c   |  5 +++--
 src/vdagentd/vdagentd.c |  7 ++++---
 4 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/udscs.c b/src/udscs.c
index 8b16f89..f67d0a0 100644
--- a/src/udscs.c
+++ b/src/udscs.c
@@ -62,6 +62,7 @@ struct udscs_connection {
 
     /* Callbacks */
     udscs_read_callback read_callback;
+    void *read_callback_user_data;
     udscs_disconnect_callback disconnect_callback;
 
     struct udscs_connection *next;
@@ -70,6 +71,7 @@ struct udscs_connection {
 
 struct udscs_connection *udscs_connect(const char *socketname,
     udscs_read_callback read_callback,
+    void *read_callback_user_data,
     udscs_disconnect_callback disconnect_callback,
     const char * const type_to_string[], int no_types, int debug)
 {
@@ -104,6 +106,7 @@ struct udscs_connection *udscs_connect(const char *socketname,
     }
 
     conn->read_callback = read_callback;
+    conn->read_callback_user_data = read_callback_user_data;
     conn->disconnect_callback = disconnect_callback;
 
     if (conn->debug)
@@ -232,7 +235,8 @@ static void udscs_read_complete(struct udscs_connection **connp)
     }
 
     if (conn->read_callback) {
-        conn->read_callback(connp, &conn->header, conn->data.buf);
+        conn->read_callback(connp, &conn->header, conn->data.buf,
+                            conn->read_callback_user_data);
         if (!*connp) /* Was the connection disconnected by the callback ? */
             return;
     }
@@ -366,12 +370,14 @@ struct udscs_server {
     struct udscs_connection connections_head;
     udscs_connect_callback connect_callback;
     udscs_read_callback read_callback;
+    void *read_callback_user_data;
     udscs_disconnect_callback disconnect_callback;
 };
 
 struct udscs_server *udscs_create_server_for_fd(int fd,
     udscs_connect_callback connect_callback,
     udscs_read_callback read_callback,
+    void *read_callback_user_data,
     udscs_disconnect_callback disconnect_callback,
     const char * const type_to_string[], int no_types, int debug)
 {
@@ -392,6 +398,7 @@ struct udscs_server *udscs_create_server_for_fd(int fd,
     server->fd = fd;
     server->connect_callback = connect_callback;
     server->read_callback = read_callback;
+    server->read_callback_user_data = read_callback_user_data;
     server->disconnect_callback = disconnect_callback;
 
     return server;
@@ -400,6 +407,7 @@ struct udscs_server *udscs_create_server_for_fd(int fd,
 struct udscs_server *udscs_create_server(const char *socketname,
     udscs_connect_callback connect_callback,
     udscs_read_callback read_callback,
+    void *read_callback_user_data,
     udscs_disconnect_callback disconnect_callback,
     const char * const type_to_string[], int no_types, int debug)
 {
@@ -427,7 +435,8 @@ struct udscs_server *udscs_create_server(const char *socketname,
         return NULL;
     }
 
-    return udscs_create_server_for_fd(fd, connect_callback, read_callback,
+    return udscs_create_server_for_fd(fd, connect_callback,
+                                      read_callback, read_callback_user_data,
                                       disconnect_callback, type_to_string,
                                       no_types, debug);
 }
@@ -480,6 +489,7 @@ static void udscs_server_accept(struct udscs_server *server) {
     new_conn->no_types = server->no_types;
     new_conn->debug = server->debug;
     new_conn->read_callback = server->read_callback;
+    new_conn->read_callback_user_data = server->read_callback_user_data;
     new_conn->disconnect_callback = server->disconnect_callback;
 
     length = sizeof(new_conn->peer_cred);
diff --git a/src/udscs.h b/src/udscs.h
index 30a96db..04377ba 100644
--- a/src/udscs.h
+++ b/src/udscs.h
@@ -45,7 +45,7 @@ struct udscs_message_header {
  * made NULL (which udscs_destroy_connection takes care of).
  */
 typedef void (*udscs_read_callback)(struct udscs_connection **connp,
-    struct udscs_message_header *header, uint8_t *data);
+    struct udscs_message_header *header, uint8_t *data, void *user_data);
 
 /* Callbacks with this type will be called when the connection is disconnected.
  * Note:
@@ -68,6 +68,7 @@ typedef void (*udscs_disconnect_callback)(struct udscs_connection *conn);
  */
 struct udscs_connection *udscs_connect(const char *socketname,
     udscs_read_callback read_callback,
+    void *read_callback_user_data,
     udscs_disconnect_callback disconnect_callback,
     const char * const type_to_string[], int no_types, int debug);
 
@@ -128,6 +129,7 @@ typedef void (*udscs_connect_callback)(struct udscs_connection *conn);
 struct udscs_server *udscs_create_server_for_fd(int fd,
     udscs_connect_callback connect_callback,
     udscs_read_callback read_callback,
+    void *read_callback_user_data,
     udscs_disconnect_callback disconnect_callback,
     const char * const type_to_string[], int no_types, int debug);
 
@@ -145,6 +147,7 @@ struct udscs_server *udscs_create_server_for_fd(int fd,
 struct udscs_server *udscs_create_server(const char *socketname,
     udscs_connect_callback connect_callback,
     udscs_read_callback read_callback,
+    void *read_callback_user_data,
     udscs_disconnect_callback disconnect_callback,
     const char * const type_to_string[], int no_types, int debug);
 
diff --git a/src/vdagent/vdagent.c b/src/vdagent/vdagent.c
index 51a7618..21f88f8 100644
--- a/src/vdagent/vdagent.c
+++ b/src/vdagent/vdagent.c
@@ -140,7 +140,8 @@ static gboolean vdagent_finalize_file_xfer(void)
 }
 
 static void daemon_read_complete(struct udscs_connection **connp,
-    struct udscs_message_header *header, uint8_t *data)
+    struct udscs_message_header *header, uint8_t *data,
+    G_GNUC_UNUSED void *user_data)
 {
     switch (header->type) {
     case VDAGENTD_MONITORS_CONFIG:
@@ -226,7 +227,7 @@ static int client_setup(int reconnect)
 {
     while (!quit) {
         client = udscs_connect(vdagentd_socket, daemon_read_complete, NULL,
-                               vdagentd_messages, VDAGENTD_NO_MESSAGES,
+                               NULL, vdagentd_messages, VDAGENTD_NO_MESSAGES,
                                debug);
         if (client || !reconnect || quit) {
             break;
diff --git a/src/vdagentd/vdagentd.c b/src/vdagentd/vdagentd.c
index 682761a..590e36a 100644
--- a/src/vdagentd/vdagentd.c
+++ b/src/vdagentd/vdagentd.c
@@ -877,7 +877,8 @@ static void agent_disconnect(struct udscs_connection *conn)
 }
 
 static void agent_read_complete(struct udscs_connection **connp,
-    struct udscs_message_header *header, uint8_t *data)
+    struct udscs_message_header *header, uint8_t *data,
+    G_GNUC_UNUSED void *user_data)
 {
     struct agent_data *agent_data = udscs_get_user_data(*connp);
 
@@ -1147,7 +1148,7 @@ int main(int argc, char *argv[])
         return 1;
     } else if (n_fds == 1) {
         server = udscs_create_server_for_fd(SD_LISTEN_FDS_START, agent_connect,
-                                            agent_read_complete,
+                                            agent_read_complete, NULL,
                                             agent_disconnect,
                                             vdagentd_messages,
                                             VDAGENTD_NO_MESSAGES, debug);
@@ -1157,7 +1158,7 @@ int main(int argc, char *argv[])
 #endif /* WITH_SYSTEMD_SOCKET_ACTIVATION */
     {
         server = udscs_create_server(vdagentd_socket, agent_connect,
-                                     agent_read_complete, agent_disconnect,
+                                     agent_read_complete, NULL, agent_disconnect,
                                      vdagentd_messages, VDAGENTD_NO_MESSAGES,
                                      debug);
     }
-- 
2.13.5



More information about the Spice-devel mailing list