lcm源码链接,可以编译生成对应的库liblcm.so:
下载地址: https://download.csdn.net/download/ding283595861/12014778
// file : userlcm.cpp
#include
#include "userlcm_manage.h"
#define USER_LCM_LOG_ON 0
#if USER_LCM_LOG_ON
#define USERLCM_LOG(format,arg...) printf("[%s:%s][%u]: " format,__FILE__,__FUNCTION__,__LINE__,## arg)
#else
#define USERLCM_LOG(format,arg...)
#endif
cLcmMsgManage::cLcmMsgManage()
{
memset(&m_MsgInfo, 0, sizeof(m_MsgInfo));
}
cLcmMsgManage::~cLcmMsgManage()
{
}
void cLcmMsgManage::handleMessage(const lcm::ReceiveBuffer *rbuf, const std::string &channel)
{
if (NULL == rbuf) {
USERLCM_LOG("ReceiveBuffer = NULL,return!\n");
return;
}
if (strcmp(channel.c_str(), m_MsgInfo.channel) == 0) {
void *data_class = m_MsgInfo.func;
m_MsgInfo.f(rbuf->data, rbuf->data_size, (char *)channel.c_str(), data_class);
} else {
USERLCM_LOG("Received messgae on channel \"%s\" and it is not channal \"%s\"\n", channel.c_str(), m_MsgInfo.channel);
}
}
void cLcmMsgManage::Set_Msg_Info(Receive_msg_t *msg)
{
if (msg != NULL) {
m_MsgInfo = *msg;
}
}
void *register_lcm_message(void *data)
{
if (NULL == data) {
USERLCM_LOG("f or channel is NULL,return!\n");
return NULL;
}
cLcmMsgManage *pMsgManage = new (std::nothrow)cLcmMsgManage;
if (NULL == pMsgManage) {
USERLCM_LOG("pMsgManage is NULL,return!\n");
return NULL;
}
// init lcm
lcm::LCM lcm;
if (!lcm.good()) {
USERLCM_LOG("lcm is not good,return!\n");
return NULL;
}
// store user's info
Receive_msg_t *ptMsgInfo = (Receive_msg_t *)data;
pMsgManage->Set_Msg_Info(ptMsgInfo);
char channel[64] = {0};
memcpy(channel, ptMsgInfo->channel, 64);
USERLCM_LOG("lcm 's channel= %s\n", channel);
lcm.subscribe(channel, &cLcmMsgManage::handleMessage, pMsgManage);
// It seems we are safe to delete this object because it is no longer needed
// outside.
//ptMsgInfo receives address from "calloc",so we use "free"
if(NULL != ptMsgInfo){
free(ptMsgInfo);
ptMsgInfo = NULL;
}
while (0 == lcm.handle());
if (NULL != pMsgManage){
delete pMsgManage;
pMsgManage = NULL;
}
return NULL;
}
// Send Messages to LCM
int send_msg_to_lcm(userlcm_packet_t *buf, uint32_t buflen, const char *channel)
{
if (NULL == buf || NULL == channel) {
USERLCM_LOG("buf or channel is NULL,return!\n");
return 0;
}
lcm::LCM lcm;
if (!lcm.good()) {
USERLCM_LOG("lcm is not good,return!\n");
return 0;
}
buflen += (uint32_t)offsetof(userlcm_packet_t, msg_buf);
int result = lcm.publish(channel, (void *)buf, buflen);
if (0 == result) {
return 1;
} else {
return 0;
}
}
头文件:userlcm_manage.h
// file : userlcm_manage.h
// Don't include this file
#ifndef _USERLCM_MANAGE_H_
#define _USERLCM_MANAGE_H_
#include
#include
#include
#include
#include
#include
#include
注册lcm回调:
static void listen_lcm_msg_init()
{
Receive_msg_t *pudp_lcm_recv_msg = (Receive_msg_t *)calloc(1, sizeof(Receive_msg_t));
if (!pudp_lcm_recv_msg)
{
UDP_INFO("[%s]:pudp_lcm_recv_msg is null!\n", __FUNCTION__);
return;
}
strncpy(pudp_lcm_recv_msg->channel, LCM_CHANNEL_TO_TEST, strlen(LCM_CHANNEL_TO_TEST));
pudp_lcm_recv_msg->f = listen_lcm_msg;
pthread_t listen_lcm_msg_thread;
pthread_create(&listen_lcm_msg_thread, NULL, ®ister_lcm_message, (void *)pudp_lcm_recv_msg);
pthread_setname_np(listen_lcm_msg_thread, "listen_lcm_msg"); //set thread name
pthread_detach(listen_lcm_msg_thread); //auto recover thread resource
}
回调函数对应的实现:
static void listen_lcm_msg(void *rbuf, uint32_t buflen, char *LocalChannel, void *data)
{
UNUSED(data);
if (rbuf == NULL || LocalChannel == NULL)
{
return;
}
//check LocalChannel
if (strcmp(LocalChannel, LCM_CHANNEL_TO_UDP))
{
UDP_INFO("[%s][%d]:The LocalChannel should be \"%s\" instead of \"%s\"!\n",
__FUNCTION__, __LINE__, LCM_CHANNEL_TO_UDP, LocalChannel);
return;
}
//copy buf to userlcm_packet
userlcm_packet_t userlcm_packet;
memset(&userlcm_packet, 0, sizeof(userlcm_packet_t));
memcpy(&userlcm_packet, (userlcm_packet_t *)rbuf, buflen);
if (!strcmp(userlcm_packet.channel, LCM_CHANNEL_FROM_RTSP))
{
//handle_msg_from_rtsp(&userlcm_packet);
具体实现
return;
}
else
{
UDP_INFO("[%s]:The unsupported source channel is %s \n", __FUNCTION__, userlcm_packet.channel);
}
return;
}
其它进程通过调用库接口函数 send_msg_to_lcm ,设置参数通道值LCM_CHANNEL_FROM_RTSP(通道值可以随意定,只要进程之间协商好就行), 注册的进程 回调函数listen_lcm_msg 就能收到此消息. 然后解析相应的buf ,就能获取对应的值,做相应的事情