libxchg
Examples

See examples/serialization.c for a more complete example.

Setting up a new xchg_channel

const size_t MESSAGE_SIZE = 64;
const size_t RING_SIZE = 4096 + (2 * sizeof(void *));
char *ring_a = malloc(RING_SIZE); // pretend this is some kind of shared memory
char *ring_b = malloc(RING_SIZE);
struct xchg_channel channel = {};
if(!xchg_channel_init(&channel, MESSAGE_SIZE, ring_a, RING_SIZE, ring_b, RING_SIZE))
{
printf("xchg_channel_init: %s\n", xchg_channel_strerror(&channel));
return;
}

Sending an xchg_message to an xchg_channel

struct xchg_message message = {};
if(!xchg_channel_prepare(channel, &message)) {
printf("xchg_channel_prepare: %s\n", xchg_channel_strerror(channel));
return;
}
if(!xchg_message_write_uint16(message, event->type)) {
printf("xchg_message_write_uint16: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_message_write_uint64(message, event->identifier)) {
printf("xchg_message_write_uint64: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_message_write_int32(message, event->position_x)) {
printf("xchg_message_write_int32: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_message_write_int32(message, event->position_y)) {
printf("xchg_message_write_int32: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_message_write_float32(message, event->direction)) {
printf("xchg_message_write_float32: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_message_write_float32(message, event->velocity)) {
printf("xchg_message_write_float32: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_message_write_float32(message, event->force)) {
printf("xchg_message_write_float32: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_channel_send(channel, &message)) {
printf("xchg_channel_send: %s\n", xchg_channel_strerror(channel));
return;
}

Receiving an xchg_message from an xchg_channel

struct xchg_message message = {};
if(!xchg_channel_receive(channel, &message)) {
return;
}
uint16_t event_type = 0;
if(!xchg_message_read_uint16(message, &event_type)) {
printf("xchg_message_read_uint16: %s\n", xchg_message_strerror(channel));
return;
}
uint64_t identifier = 0;
if(!xchg_message_read_uint64(message, &identifier)) {
printf("xchg_message_read_uint64: %s\n", xchg_message_strerror(channel));
return;
}
int32_t position_x = 0;
if(!xchg_message_read_int32(message, &position_x)) {
printf("xchg_message_read_int32: %s\n", xchg_message_strerror(channel));
return;
}
int32_t position_y = 0;
if(!xchg_message_read_int32(message, &position_y)) {
printf("xchg_message_read_int32: %s\n", xchg_message_strerror(channel));
return;
}
float_t direction = 0.0;
if(!xchg_message_read_float32(message, &direction)) {
printf("xchg_message_read_float32: %s\n", xchg_message_strerror(channel));
return;
}
float_t velocity = 0.0;
if(!xchg_message_read_float32(message, &velocity)) {
printf("xchg_message_read_float32: %s\n", xchg_message_strerror(channel));
return;
}
float_t force = 0.0;
if(!xchg_message_read_float32(message, &force)) {
printf("xchg_message_read_float32: %s\n", xchg_message_strerror(channel));
return;
}
if(!xchg_channel_return(channel, &message)) {
printf("xchg_channel_return: %s\n", xchg_channel_strerror(channel));
return;
}
xchg_channel::xchg_channel_receive
bool xchg_channel_receive(struct xchg_channel *channel, struct xchg_message *message)
Receives the next available message from channel into message, allowing the caller to read the messag...
xchg_message::xchg_message_write_int32
bool xchg_message_write_int32(struct xchg_message *message, int32_t value)
Writes an int32_t value to message and advances the underlying buffer position.
xchg_message::xchg_message_write_uint16
bool xchg_message_write_uint16(struct xchg_message *message, uint16_t value)
Writes a uint16_t value to message and advances the underlying buffer position.
xchg_channel::xchg_channel_return
bool xchg_channel_return(struct xchg_channel *channel, const struct xchg_message *message)
Returns the backing memory of message (previously initialized via xchg_channel_receive) to channel.
xchg_message::xchg_message_write_float32
bool xchg_message_write_float32(struct xchg_message *message, float_t value)
Writes a float32_t value to message and advances the underlying buffer position.
xchg_message::xchg_message_read_int32
bool xchg_message_read_int32(struct xchg_message *message, int32_t *value)
Reads an int32_t from message and advances the underlying buffer position.
xchg_channel::xchg_channel_send
bool xchg_channel_send(struct xchg_channel *channel, const struct xchg_message *message)
Sends message (previously initialized via xchg_channel_prepare) into channel.
xchg_message::xchg_message_strerror
const char * xchg_message_strerror(const struct xchg_message *message)
Provides a static string describing the error that occurred during the last operation on message.
xchg_message
Represents a stream of values that can be encoded and decoded using the xchg_message_* family of func...
Definition: xchg.h:51
xchg_message::xchg_message_read_float32
bool xchg_message_read_float32(struct xchg_message *message, float_t *value)
Reads a float32_t from message and advances the underlying buffer position.
xchg_message::xchg_message_write_uint64
bool xchg_message_write_uint64(struct xchg_message *message, uint64_t value)
Writes a uint64_t value to message and advances the underlying buffer position.
xchg_channel
Represents a lock-free communication device backed by shared memory buffers which can be manipulated ...
Definition: xchg.h:797
xchg_channel::xchg_channel_prepare
bool xchg_channel_prepare(struct xchg_channel *channel, struct xchg_message *message)
Prepares message with writable backing memory from channel, allowing the caller to construct a messag...
xchg_channel::xchg_channel_strerror
const char * xchg_channel_strerror(const struct xchg_channel *channel)
Provides a static string describing the error that occurred during the last operation on channel
xchg_message::xchg_message_read_uint16
bool xchg_message_read_uint16(struct xchg_message *message, uint16_t *value)
Reads a uint16_t from message and advances the underlying buffer position.
xchg_message::xchg_message_read_uint64
bool xchg_message_read_uint64(struct xchg_message *message, uint64_t *value)
Reads a uint64_t from message and advances the underlying buffer position.
xchg_channel::xchg_channel_init
bool xchg_channel_init(struct xchg_channel *channel, size_t sz_message, char *ingress, size_t sz_ingress, char *egress, size_t sz_egress)
Configures channel to use ingress of size sz_ingress and egress of size sz_egress as underlying share...