libshmemq/examples/raw_receiver.c

87 lines
1.8 KiB
C
Raw Normal View History

2020-12-12 11:40:16 +00:00
#include "raw.h"
#define _POSIX_C_SOURCE 200809L
#include <shmemq.h>
2020-12-12 11:40:16 +00:00
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static bool running = true;
static void signal_handler(const int signo)
{
if (signo == SIGINT) {
running = false;
}
}
int main()
{
signal(SIGINT, signal_handler);
printf("Create queue.\n");
2020-12-12 11:40:16 +00:00
2020-12-13 17:41:32 +00:00
ShmemqError shmemq_error;
2020-12-13 15:51:28 +00:00
Shmemq shmemq = shmemq_new("/buffer1", true, &shmemq_error);
2020-12-12 11:40:16 +00:00
assert(shmemq_error == SHMEMQ_ERROR_NONE);
assert(shmemq != NULL);
2020-12-12 11:40:16 +00:00
printf("Main loop.\n");
while (running) {
2020-12-14 16:17:26 +00:00
const ShmemqFrame frame = shmemq_pop_start(shmemq);
2020-12-12 11:40:16 +00:00
2020-12-14 16:17:26 +00:00
if (frame == NULL) {
2020-12-12 11:40:16 +00:00
printf("No messages.\n");
sleep(1);
continue;
}
2020-12-14 16:17:26 +00:00
const struct Message *const message = (struct Message*)frame->data;
2020-12-12 11:40:16 +00:00
switch (message->type) {
case FINISH:
printf("Message: finish.\n");
running = false;
break;
case ONEBYTE:
printf("Message: 1 byte (%u)\n", *(unsigned char*)message->data);
break;
case NULLSTR:
printf("Message: null-terminated string (%s)\n", message->data);
break;
default:
printf("Invalid message.\n");
running = false;
}
2020-12-14 16:17:26 +00:00
shmemq_pop_end(shmemq, &shmemq_error);
if (shmemq_error != SHMEMQ_ERROR_NONE) {
2021-08-17 09:14:01 +00:00
printf(
"Error: %u (SHMEMQ_ERROR_%s).\n",
shmemq_error,
shmemq_error_str(shmemq_error)
);
2020-12-14 16:17:26 +00:00
break;
}
2020-12-12 11:40:16 +00:00
}
printf("Destroy queue.\n");
2020-12-12 11:40:16 +00:00
2020-12-15 07:22:52 +00:00
SHMEMQ_DELETE(shmemq, NULL);
2020-12-12 11:40:16 +00:00
return 0;
}