Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created May 29, 2020 14:35
Show Gist options
  • Select an option

  • Save fernandoc1/b31c75e0fe7e572bc86b7b05232db60c to your computer and use it in GitHub Desktop.

Select an option

Save fernandoc1/b31c75e0fe7e572bc86b7b05232db60c to your computer and use it in GitHub Desktop.
#include "shared_memory_communicator.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{
MSG_DV_TYPE_START = 0,
MSG_DV_TYPE_MSG1,
MSG_DV_TYPE_MSG2,
MSG_DV_TYPE_MSG3,
MSG_DV_TYPE_MSG4,
MSG_DV_TYPE_MSG5,
MSG_DV_TYPE_MSG6,
MSG_DV_TYPE_MSG7,
MSG_DV_TYPE_MSG8,
MSG_DV_TYPE_END
};
#define DV_MSG_KEY 0x61766C79
struct MsgStr
{
long type;
char msg[128];
};
int main(int argc, char** argv)
{
SharedMemoryCommunicator shm(DV_MSG_KEY, MSG_DV_TYPE_MSG1);
MsgStr msgStr;
memset(&msgStr, 0, sizeof(MsgStr));
msgStr.type = MSG_DV_TYPE_MSG1;
int ret = shm.recvMessage(&msgStr, sizeof(msgStr));
printf("RET=%d : MSG=%s\n", ret, msgStr.msg);
return 0;
}
#include "shared_memory_communicator.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{
MSG_DV_TYPE_START = 0,
MSG_DV_TYPE_MSG1,
MSG_DV_TYPE_MSG2,
MSG_DV_TYPE_MSG3,
MSG_DV_TYPE_MSG4,
MSG_DV_TYPE_MSG5,
MSG_DV_TYPE_MSG6,
MSG_DV_TYPE_MSG7,
MSG_DV_TYPE_MSG8,
MSG_DV_TYPE_END
};
#define DV_MSG_KEY 0x61766C79
struct MsgStruct
{
long type;
char buf[128];
};
int main(int argc, char** argv)
{
SharedMemoryCommunicator shm(DV_MSG_KEY, MSG_DV_TYPE_MSG1);
char msg[] = "ABCDEFG";
MsgStruct msgStr;
memset(&msgStr, 0, sizeof(msgStr));
msgStr.type = MSG_DV_TYPE_MSG1;
strcpy(msgStr.buf, msg);
shm.sendMessage(&msgStr, sizeof(msgStr));
/*
int queueId;
key_t key = DV_MSG_KEY;
queueId = msgget(key, 0);
if(queueId < 0)
{
queueId = msgget(key, IPC_CREAT|0666);
printf("Created queue id: %d\n", queueId);
}
printf("Queue id:%d\n", queueId);
*/
return 0;
}
#include "shared_memory_communicator.hpp"
#include <iostream>
#include <stdio.h>
#include <linux/errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
SharedMemoryCommunicator::SharedMemoryCommunicator(int msgKey, int messageType)
: msgKey(msgKey)
, messageQueueId(0)
, messageType(messageType)
{
this->messageQueueId = msgget(this->msgKey, 0);
if(this->messageQueueId < 0)
{
this->messageQueueId = msgget(this->msgKey, IPC_CREAT|0666);
std::cout << "Created message queue " << this->messageQueueId << std::endl;
}
std::cout << "Connected to message queue " << this->messageQueueId << std::endl;
}
int SharedMemoryCommunicator::sendMessage(void* data, int size)
{
return msgsnd(this->messageQueueId, data, size - sizeof(long), 0);
}
int SharedMemoryCommunicator::recvMessage(void* data, int size)
{
return msgrcv(this->messageQueueId, data, size - sizeof(long), this->messageType, 0);
}
#ifndef __SHARED_MEMORY_COMMUNICATOR_HPP__
#define __SHARED_MEMORY_COMMUNICATOR_HPP__
class SharedMemoryCommunicator
{
int msgKey;
int messageQueueId;
int messageType;
public:
SharedMemoryCommunicator(int msgKey, int messageType);
int sendMessage(void* data, int size);
int recvMessage(void* data, int size);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment