Obsolete
/
libipc-old
Archived
3
0
Fork 0

message.[ch] from pubsub

more_to_read
Philippe PITTOLI 2018-10-09 16:38:27 +02:00
parent 222f7e22ee
commit ae7a6c404f
2 changed files with 153 additions and 0 deletions

132
pubsub/lib/message.c Normal file
View File

@ -0,0 +1,132 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "message.h"
#include "../../core/error.h"
void pubsub_message_serialize (const struct pubsub_msg *msg, char **data, size_t *len)
{
if (msg == NULL) {
handle_err ("pubsub_message_serialize", "msg == NULL");
return;
}
if (data == NULL) {
handle_err ("pubsub_message_serialize", "data == NULL");
return;
}
if (*data != NULL) {
handle_err ("pubsub_message_serialize", "*data != NULL");
return;
}
if (len == NULL) {
handle_err ("pubsub_message_serialize", "len == NULL");
return;
}
// buflen = pubsub msg type (1) + 2* size_t (16) + chan+data
size_t buflen = 1 + 2 * sizeof (size_t) + msg->chanlen + msg->datalen;
if (buflen > BUFSIZ) {
handle_err ("pubsub_message_serialize", "chanlen + datalen too high");
return;
}
char *buf = malloc (buflen);
memset (buf, 0, buflen);
size_t offset = 0;
// msg type
buf[offset++] = msg->type;
// chan
memcpy (buf + offset, &msg->chanlen, sizeof (size_t));
offset += sizeof (size_t);
memcpy (buf + offset, msg->chan, msg->chanlen);
offset += msg->chanlen;
// data
memcpy (buf + offset, &msg->datalen, sizeof (size_t));
offset += sizeof (size_t);
memcpy (buf + offset, msg->data, msg->datalen);
offset += msg->datalen;
*data = buf;
*len = buflen;
}
void pubsub_message_unserialize (struct pubsub_msg *msg, const char *buf, size_t mlen)
{
if (msg == NULL) {
handle_err ("pubsub_message_unserialize", "msg == NULL");
return;
}
pubsub_message_free (msg);
if (mlen > BUFSIZ) {
handle_err ("pubsub_message_unserialize", "mlen > BUFSIZ");
return;
}
size_t offset = 0;
// msg type
msg->type = buf[offset++];
// chan
memcpy (&msg->chanlen, buf + offset, sizeof (size_t));
if (msg->chanlen > BUFSIZ) {
handle_err ("pubsub_message_unserialize", "chanlen > BUFSIZ");
return;
}
msg->chan = malloc (msg->chanlen);
memset (msg->chan, 0, msg->chanlen);
offset += sizeof (size_t);
memcpy (msg->chan, buf + offset, msg->chanlen);
offset += msg->chanlen;
// data
memcpy (&msg->datalen, buf + offset, sizeof (size_t));
if (msg->datalen > BUFSIZ) {
handle_err ("pubsub_message_unserialize", "datalen > BUFSIZ");
return;
}
msg->data = malloc (msg->datalen);
memset (msg->data, 0, msg->datalen);
offset += sizeof (size_t);
memcpy (msg->data, buf + offset, msg->datalen);
offset += msg->datalen;
}
void pubsub_message_free (struct pubsub_msg *msg)
{
if (msg == NULL) {
handle_err ("pubsub_message_free", "msg == NULL");
return;
}
if (msg->chan) {
free (msg->chan);
msg->chan = NULL;
}
if (msg->data) {
free (msg->data);
msg->data = NULL;
}
}
void pubsub_message_print (const struct pubsub_msg *msg)
{
if (msg == NULL) {
handle_err ("pubsub_message_print", "msg == NULL");
return;
}
printf ("msg: type=%d chan=%s, data=%s\n"
, msg->type, msg->chan, msg->data);
}

21
pubsub/lib/message.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __PUBSUB_MSG_H__
#define __PUBSUB_MSG_H__
#define PUBSUB_MSG_TYPE_SUB 1
#define PUBSUB_MSG_TYPE_UNSUB 2
#define PUBSUB_MSG_TYPE_PUB 3
struct pubsub_msg {
unsigned char type; // message type : alert, notification, …
char *chan;
size_t chanlen;
char *data;
size_t datalen;
};
void pubsub_message_serialize (const struct pubsub_msg *msg, char **data, size_t *len);
void pubsub_message_unserialize (struct pubsub_msg *msg, const char *data, size_t len);
void pubsub_message_free (struct pubsub_msg *msg);
void pubsub_message_print (const struct pubsub_msg *msg);
#endif