summaryrefslogtreecommitdiffstats
path: root/lib/client.c
blob: 735796f85e7b27835522b090a0466fe2524cda27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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;
}