基于asio库使用c++工厂模式及protobuf3的Any特性实现的功能及消息插件化的通信程序
todo:目前不清楚asio异步通讯中是否自带线程资源保护,故未在读写处理函数中添加线程锁,若有线程冲突可在异步读写的处理函数中添加线程锁
git clone https://github.com/mingjitianming/transmit_asio.git
cd transmit_asio
mkdir build && cd build
cmake ..
make
make install
# 设置transmit_asio的install路径
set(transmit_DIR "/home/zmy/project_ws/hk_transport/install/share/cmake/transmit")
# 查找package
find_package(transmit)
add_executable(test_server
${CMAKE_CURRENT_LIST_DIR}/test_server.cpp
)
# 使用命名空间链接
target_link_libraries(test_server
transmit::server
)
#include "base_plugin.h"
#include <functional>
virtual void clientHandle(message::Message &message) override;
virtual void serverHandle(message::Message &message) override;
decltype(auto) setClientProcess(auto &&func)
{
handle_plc_ = std::forward<decltype(func)>(func);
}
decltype(auto) setServerProcess(auto &&func)
{
server_handle_ = std::forward<decltype(func)>(func);
}
在插件cpp文件中使用如下命令进行注册
PLUGIN_EXPORT(PlcTransmit, "plc");
其中PlcTransmit为插件类,"plc"为插件名称。
message LogInfo
{
string name = 1;
}
message Message
{
sfixed32 msg_head = 1;
sint32 msg_id = 2;
sint32 src_id = 3;
sint32 dest_id = 4;
google.protobuf.Any msg_data = 5;
sfixed32 msg_tail = 6;
}
todo:由于没有生产测试,该消息定义需根据实际使用进行更改
syntax="proto3";
package message.data;
message PlcData
{
message Request
{
int32 x = 1;
int32 y = 2;
}
message Response
{
float x = 1;
float y = 2;
float theta = 3;
}
Request request = 1;
Response response = 2;
}
plugin_message data;
message::Message msg;
msg.mutable_msg_data()->PackFrom(data);
plugin_message data;
message::Message msg;
msg.msg_data().UnpackTo(&data);
plugins:
- { name: plc, msg_id: 5 }
- { name: plc, msg_id: 6 }
std::dynamic_pointer_cast<transmit::plugins::PlcTransmit>(ser.getMethod("plc"))
->setServerProcess(
[](int a, int b, cv::Point2f c) {
std::cout << a + b << std::endl;
});