1 Star 0 Fork 1

AndyYang/vsomeip-test

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
request-sample.cpp 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
zhangjingcheng 提交于 2022-01-02 07:18 . vsomeip-test
#include <iostream>
#include <vsomeip\vsomeip.hpp>
#include <memory>
#include <thread>
#define SERVICE_ID 0x1234
#define SERVICE_INSTANCE_ID 0x5678
#define METHOD_ID 0x0001
#define SERVICE_SET_METHOD_ID 0x0002
#define SERVICE_GET_METHOD_ID 0x0003
#define SERVICE_EVENT_GROUP_ID 0x4465
#define SERVICE_EVENT_ID 0x8778
using namespace std;
using namespace vsomeip;
shared_ptr<application> app;
void on_available(service_t _s, instance_t _ins, bool is_available)
{
cout << "zhangjingcheng thread_id: " << std::this_thread::get_id() << " server: " << std::hex << "[" << _s << "." << _ins << "]" << (is_available ? "available" : "not available") << endl;
if (!is_available) return;
std::set<vsomeip::eventgroup_t> its_groups;
its_groups.insert(SERVICE_EVENT_GROUP_ID);
app->request_event(SERVICE_ID, SERVICE_INSTANCE_ID, SERVICE_EVENT_ID, its_groups, true);
app->subscribe(SERVICE_ID, SERVICE_INSTANCE_ID, SERVICE_EVENT_GROUP_ID);
}
void on_message(std::shared_ptr<vsomeip::message> msg)
{
cout << "zhangjingcheng thread_id: " << std::this_thread::get_id() << endl;
auto server_id = msg->get_service();
auto instance_id = msg->get_instance();
auto method_id = msg->get_method();
std::string str("Receive: ");
str.append(reinterpret_cast<const char*>(msg->get_payload()->get_data()), 0, msg->get_payload()->get_length());
std::cout << str << " method_id: " << method_id << std::endl;
}
int main()
{
app = runtime::get()->create_application("client_sample");
app->init();
app->register_message_handler(vsomeip::ANY_SERVICE, vsomeip::ANY_INSTANCE, vsomeip::ANY_METHOD, std::bind(&on_message, std::placeholders::_1));
app->register_availability_handler(SERVICE_ID, SERVICE_INSTANCE_ID, on_available);
app->request_service(SERVICE_ID, SERVICE_INSTANCE_ID);
std::cout << "start..." << std::endl;
std::thread t = thread(std::bind(&application::start, app));
std::cout << "start... done" << std::endl;
cout << "zhangjingcheng main thread_id: " << std::this_thread::get_id() << endl;
t.join();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/andyangj/vsomeip-test.git
git@gitee.com:andyangj/vsomeip-test.git
andyangj
vsomeip-test
vsomeip-test
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385