2017-08-28 00:03:35 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-10-09 13:20:26 +02:00
|
|
|
#include "message.h"
|
2017-08-28 00:03:35 +02:00
|
|
|
#include "../../core/error.h"
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
void remote_message_serialize (const struct remoted_msg *msg, char **data, size_t *len)
|
2017-08-28 00:03:35 +02:00
|
|
|
{
|
|
|
|
if (msg == NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_serialize", "msg == NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data == NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_serialize", "data == NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*data != NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_serialize", "*data != NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_serialize", "len == NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// buflen = remote msg type (1) + size_t (16) + data
|
|
|
|
size_t buflen = 1 + sizeof (size_t) + msg->datalen;
|
|
|
|
|
|
|
|
if (buflen > BUFSIZ) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_serialize", "datalen too high");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = malloc (buflen);
|
|
|
|
memset (buf, 0, buflen);
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
// msg type
|
|
|
|
buf[offset++] = msg->type;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
void remote_message_unserialize (struct remoted_msg *msg, const char *buf, size_t mlen)
|
2017-08-28 00:03:35 +02:00
|
|
|
{
|
|
|
|
if (msg == NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_unserialize", "msg == NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
remote_message_free (msg);
|
2017-08-28 00:03:35 +02:00
|
|
|
|
|
|
|
if (mlen > BUFSIZ) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_unserialize", "mlen > BUFSIZ");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
// msg type
|
|
|
|
msg->type = buf[offset++];
|
|
|
|
|
|
|
|
// data
|
|
|
|
memcpy (&msg->datalen, buf + offset, sizeof (size_t));
|
|
|
|
if (msg->datalen > BUFSIZ) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_unserialize", "datalen > BUFSIZ");
|
2017-08-28 00:03:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
void remote_message_free (struct remoted_msg *msg)
|
2017-08-28 00:03:35 +02:00
|
|
|
{
|
|
|
|
if (msg == NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_free", "msg == NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->data) {
|
|
|
|
free (msg->data);
|
|
|
|
msg->data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
void remote_message_print (const struct remoted_msg *msg)
|
2017-08-28 00:03:35 +02:00
|
|
|
{
|
|
|
|
if (msg == NULL) {
|
2018-10-03 21:52:11 +02:00
|
|
|
handle_err ("remote remote_message_print", "msg == NULL");
|
2017-08-28 00:03:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("msg: type=%d data=%s\n", msg->type, msg->data);
|
|
|
|
}
|