diff options
author | Julian Bouzas <julian.bouzas@collabora.com> | 2021-04-20 04:08:58 -0400 |
---|---|---|
committer | George Kiagiadakis <george.kiagiadakis@collabora.com> | 2021-07-28 13:19:02 +0300 |
commit | f25bb13718f334bc0c96d29ea9f3a57c0a6f3a34 (patch) | |
tree | eaa30eafe2df92e3d5d75571d022c3a775246bff /lib/client.c | |
parent | 7bf96bda703dd157385cbb175ec90bd6f38af404 (diff) |
lib: add wpipc library
Simple library that uses sockets for inter-process communication. It provides an
API to create server and client objects. Users can add custom handlers in the
server, and clients can send requests for those custom handlers.
Signed-off-by: George Kiagiadakis <george.kiagiadakis@collabora.com>
Diffstat (limited to 'lib/client.c')
-rw-r--r-- | lib/client.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/client.c b/lib/client.c new file mode 100644 index 0000000..735796f --- /dev/null +++ b/lib/client.c @@ -0,0 +1,84 @@ +/* PipeWire AGL Cluster IPC + * + * Copyright © 2021 Collabora Ltd. + * @author Julian Bouzas <julian.bouzas@collabora.com> + * + * SPDX-License-Identifier: MIT + */ + +#include "private.h" +#include "protocol.h" +#include "sender.h" +#include "client.h" + +#define BUFFER_SIZE 1024 + +static void +on_lost_connection (struct icipc_sender *self, + int receiver_fd, + void *data) +{ + icipc_log_warn ("client: lost connection with server %d", receiver_fd); +} + +/* API */ + +struct icipc_client * +icipc_client_new (const char *path, bool connect) +{ + struct icipc_sender *base; + base = icipc_sender_new (path, BUFFER_SIZE, on_lost_connection, NULL, 0); + + if (connect) + icipc_sender_connect (base); + + return (struct icipc_client *)base; +} + +void +icipc_client_free (struct icipc_client *self) +{ + struct icipc_sender *base = icipc_client_to_sender (self); + icipc_sender_free (base); +} + +bool +icipc_client_send_request (struct icipc_client *self, + const char *name, + const struct spa_pod *args, + icipc_sender_reply_func_t reply, + void *data) +{ + struct icipc_sender *base = icipc_client_to_sender (self); + + /* check params */ + if (name == NULL) + return false; + + const size_t size = icipc_protocol_calculate_request_size (name, args); + uint8_t buffer[size]; + icipc_protocol_build_request (buffer, size, name, args); + return icipc_sender_send (base, buffer, size, reply, data); +} + +const struct spa_pod * +icipc_client_send_request_finish (struct icipc_sender *self, + const uint8_t *buffer, + size_t size, + const char **error) +{ + /* error */ + if (icipc_protocol_is_reply_error (buffer, size)) { + icipc_protocol_parse_reply_error (buffer, size, error); + return NULL; + } + + /* ok */ + if (icipc_protocol_is_reply_ok (buffer, size)) { + const struct spa_pod *value = NULL; + if (icipc_protocol_parse_reply_ok (buffer, size, &value)) + return value; + } + + return NULL; +} |