ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 3) Report Client设计与实现 ​ report client主要是实现thread_queue的回调业务,udp server会定期的上传上报数据到reporter,那么请求对于report client就是透传给reporter serivce即可。 > lars_loadbalance_agent/src/reporter_client.cpp ```c #include "lars_reactor.h" #include "main_server.h" #include <string> #include <pthread.h> //typedef void io_callback(event_loop *loop, int fd, void *args); //只要thread_queue有数据,loop就会触发此回调函数来处理业务 void new_report_request(event_loop *loop, int fd, void *args) { tcp_client *client = (tcp_client*)args; //1. 将请求数据从thread_queue中取出, std::queue<lars::ReportStatusRequest> msgs; //2. 将数据放在queue队列中 report_queue->recv(msgs); //3. 遍历队列,通过client依次将每个msg发送给reporter service while (!msgs.empty()) { lars::ReportStatusRequest req = msgs.front(); msgs.pop(); std::string requestString; req.SerializeToString(&requestString); //client 发送数据 client->send_message(requestString.c_str(), requestString.size(), lars::ID_ReportStatusRequest); } } void *report_client_thread(void* args) { printf("report client thread start\n"); event_loop loop; //1 加载配置文件得到repoter ip + port std::string ip = config_file::instance()->GetString("reporter", "ip", ""); short port = config_file::instance()->GetNumber("reporter", "port", 0); //2 创建客户端 tcp_client client(&loop, ip.c_str(), port, "reporter client"); //3 将 thread_queue消息回调事件,绑定到loop中 report_queue->set_loop(&loop); report_queue->set_callback(new_report_request, &client); //4 启动事件监听 loop.event_process(); return NULL; } void start_report_client() { //开辟一个线程 pthread_t tid; //启动线程业务函数 int ret = pthread_create(&tid, NULL, report_client_thread, NULL); if (ret == -1) { perror("pthread_create"); exit(1); } //设置分离模式 pthread_detach(tid); } ``` ## 4) Dns Client设计与实现 ​ dns client 和report client的业务十分相似,只是针对的协议不同了。dns client的thread_queue 回调业务主要是透传`lars::GetRouteRequest`数据包。 > lars_loadbalance_agent/src/dns_client.cpp ```c #include "lars_reactor.h" #include "main_server.h" #include <pthread.h> //typedef void io_callback(event_loop *loop, int fd, void *args); //只要thread_queue有数据,loop就会触发此回调函数来处理业务 void new_dns_request(event_loop *loop, int fd, void *args) { tcp_client *client = (tcp_client*)args; //1. 将请求数据从thread_queue中取出, std::queue<lars::GetRouteRequest> msgs; //2. 将数据放在queue队列中 dns_queue->recv(msgs); //3. 遍历队列,通过client依次将每个msg发送给reporter service while (!msgs.empty()) { lars::GetRouteRequest req = msgs.front(); msgs.pop(); std::string requestString; req.SerializeToString(&requestString); //client 发送数据 client->send_message(requestString.c_str(), requestString.size(), lars::ID_GetRouteRequest); } } void *dns_client_thread(void* args) { printf("dns client thread start\n"); event_loop loop; //1 加载配置文件得到dns service ip + port std::string ip = config_file::instance()->GetString("dnsserver", "ip", ""); short port = config_file::instance()->GetNumber("dnsserver", "port", 0); //2 创建客户端 tcp_client client(&loop, ip.c_str(), port, "dns client"); //3 将thread_queue消息回调事件,绑定到loop中 dns_queue->set_loop(&loop); dns_queue->set_callback(new_dns_request, &client); //4 启动事件监听 loop.event_process(); return NULL; } void start_dns_client() { //开辟一个线程 pthread_t tid; //启动线程业务函数 int ret = pthread_create(&tid, NULL, dns_client_thread, NULL); if (ret == -1) { perror("pthread_create"); exit(1); } //设置分离模式 pthread_detach(tid); } ``` --- ### 关于作者: 作者:`Aceld(刘丹冰)` mail: [danbing.at@gmail.com](mailto:danbing.at@gmail.com) github: [https://github.com/aceld](https://github.com/aceld) 原创书籍: [https://www.kancloud.cn/@aceld](https://www.kancloud.cn/@aceld) ![](https://img.kancloud.cn/b0/d1/b0d11a21ba62e96aef1c11d5bfff2cf8_227x227.jpg) >**原创声明:未经作者允许请勿转载, 如果转载请注明出处**