AtlatestRepositorysigil-ui
1
#ifndef SIGIL_UI_OWNED_WIRE_PEER_H2
#define SIGIL_UI_OWNED_WIRE_PEER_H3
/* Small wire helpers shared by owned socketpair protocol fixtures. */4
static inline int send_bytes(int fd, const void *data, size_t size)5
{6
const uint8_t *bytes = data;7
while (size) {8
ssize_t n = write(fd, bytes, size);9
if (n < 0 && errno == EINTR) continue;10
if (n <= 0) return 0;11
bytes += n;12
size -= (size_t)n;13
}14
return 1;15
}17
static inline int event(int fd, uint32_t object, uint16_t opcode, const void *data, size_t size)18
{19
uint32_t header[] = {object, ((uint32_t)(8 + size) << 16) | opcode};20
return send_bytes(fd, header, sizeof header) && send_bytes(fd, data, size);21
}23
static inline int array_event(int fd, uint32_t object, uint16_t opcode, const void *data, size_t size)24
{25
uint32_t count = (uint32_t)size, padding = 0;26
uint32_t header[] = {object, ((uint32_t)(12 + ((size + 3) & ~(size_t)3)) << 16) | opcode};27
return send_bytes(fd, header, sizeof header) && send_bytes(fd, &count, 4) &&28
send_bytes(fd, data, size) && send_bytes(fd, &padding, (4 - size % 4) % 4);29
}31
static inline int advertise(int fd, uint32_t registry, uint32_t name, const char *interface, uint32_t version)32
{33
uint32_t words[32] = {registry};34
size_t length = strlen(interface) + 1, padded = (length + 3) / 4;35
words[1] = (uint32_t)((5 + padded) * 4) << 16;36
words[2] = name;37
words[3] = (uint32_t)length;38
memcpy(&words[4], interface, length);39
words[4 + padded] = version;40
return send_bytes(fd, words, (5 + padded) * 4);41
}42
#endif