当前位置: 首页 > news >正文

【RabbitMQ 项目】客户端:连接模块

文章目录

  • 一.实现要点
    • 构造函数
  • 二.代码实践
  • 三.搭建消费客户端和生产客户端

客户端有两种,生产客户端,消费客户端,其实连接模块就是传统意义上的的客户端,生产客户端,消费客户端都是用它来搭建的,只不过连接模块提供了很多接口。消费客户端只会使用其中的一部分,比如消费客户端会发布消息;生产客户端也只使用一部分,比如不可能订阅消息

一.实现要点

构造函数

muduo 库中的 TcpClient 成员的注册两个回调函数,一个是从接收缓冲区读取上来消息后的回调函数,注册为协议处理器的 onMessage 成员,另一个是连接建立成功后的回调,我们自己实现一个,工作就是给 Connection 内部维护的 TCP 连接赋值为这个新建的连接
此外,为分发器注册业务处理函数,即对不同响应的处理动作。第一,收到一个 CommonResponse,结合我们上节介绍的信道模块,则需要向信道的哈希表中 put 该 Response;第二,收到一个 PushMessageResponse,我们需要找到信道,调用信道的提供的处理消息的函数(工作是先找到消费者,然后调用消费者的回调函数)。但这个任务属于支线任务,Reactor 线程不想自己做,于是交给线程池来做
最后,调用 TcpClient 的 connect 接口,向服务端发起连接

二.代码实践

#pragma once
#include "muduo/protobuf/codec.h"
#include "muduo/protobuf/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpClient.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/base/CountDownLatch.h"
#include "../common/ThreadPool.hpp"
#include "Channel.hpp"
#include <functional>
#include <iostream>
namespace ns_connection
{using ThreadPoolPtr = std::shared_ptr<ns_tp::ThreadPool>;using ProtobufCodecPtr = std::shared_ptr<ProtobufCodec>;using ProtobufDispatcherPtr = std::shared_ptr<ProtobufDispatcher>;using ChannelPtr = std::shared_ptr<ns_channel::Channel>;using ThreadPoolPtr = std::shared_ptr<ns_tp::ThreadPool>;using CommonResponsePtr = std::shared_ptr<ns_protocol::CommomResponse>;using PushMessageResonsePtr = std::shared_ptr<ns_protocol::PushMessageResponse>;/************ Connection是对底层用于通信的TCP套接字封装(muduo库中的TcpConnectionPtr)* 一个Connection中包含多个信道,当Connection关闭,信道也会销毁* ******************/class Connection{private:muduo::net::EventLoopThread _loopThread;muduo::CountDownLatch _latch;muduo::net::TcpClient _client;muduo::net::TcpConnectionPtr _connPtr;ProtobufDispatcherPtr _distpatcherPtr;ProtobufCodecPtr _codecPtr;ns_channel::ChannelManager _channelManager;ThreadPoolPtr _threadPoolPtr;public:Connection(const std::string &serverIp, int serverPort, const ThreadPoolPtr &threadPoolPtr): _loopThread(),_latch(1),_client(_loopThread.startLoop(), muduo::net::InetAddress(serverIp, serverPort), "client"),_connPtr(),_channelManager(),_threadPoolPtr(threadPoolPtr){// 构造成员_distpatcherPtr = std::make_shared<ProtobufDispatcher>((std::bind(&Connection::onUnknownMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3)));_codecPtr = std::make_shared<ProtobufCodec>((std::bind(&ProtobufDispatcher::onProtobufMessage,_distpatcherPtr.get(),std::placeholders::_1,std::placeholders::_2,std::placeholders::_3)));// 给Client注册两个回调函数_client.setConnectionCallback(std::bind(&Connection::onConnection, this, std::placeholders::_1));_client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, _codecPtr.get(), std::placeholders::_1,std::placeholders::_2, std::placeholders::_3));_distpatcherPtr->registerMessageCallback<ns_protocol::CommomResponse>(std::bind(&Connection::onCommonResponse,this, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));_distpatcherPtr->registerMessageCallback<ns_protocol::PushMessageResponse>(std::bind(&Connection::onRecvMessage,this, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));connect();}void connect(){_client.connect(); // 非阻塞_latch.wait();}ChannelPtr openChannel(){// 只是在本地建立了信道auto channelPtr = _channelManager.openChannel(_connPtr, _codecPtr);// 通过该信道发送建立信道的请求,要服务端也建立对应的信道if (!channelPtr->openChannel()){LOG(WARNING) << "打开信道失败" << endl;// 关闭本地的信道,防止内存泄漏_channelManager.closeChannel(channelPtr->_id);}return channelPtr;}void closeChannel(const ChannelPtr& channelPtr){// 发送关闭信道的请求,让服务端关闭信道channelPtr->closeChannel();// 把本地信道关掉_channelManager.closeChannel(channelPtr->_id);}private:// 给_client设置的回调void onConnection(muduo::net::TcpConnectionPtr connPtr){if (connPtr->connected()){_connPtr = connPtr;_latch.countDown();}else{_connPtr.reset();}}void onUnknownMessage(const muduo::net::TcpConnectionPtr &connPtr,const MessagePtr &resp, muduo::Timestamp time){LOG(WARNING) << "未知响应" << endl;}// 业务处理函数void onCommonResponse(const muduo::net::TcpConnectionPtr &connPtr,const CommonResponsePtr &respPtr, muduo::Timestamp time){//LOG(DEBUG) << "收到CommonResponse, respId: " << respPtr->response_id() << endl;std::string channeId = respPtr->channel_id();auto channelPtr = _channelManager.getChannel(channeId);channelPtr->putCommonResponse(respPtr);}void onRecvMessage(const muduo::net::TcpConnectionPtr &connPtr,const PushMessageResonsePtr &respPtr, muduo::Timestamp time){//LOG(DEBUG) << "收到消息, body: " << respPtr->msg().saved_info().body() << endl;std::string channeId = respPtr->channel_id();auto channelPtr = _channelManager.getChannel(channeId);// 把处理消息的任务交给线程池来做_threadPoolPtr->push(std::bind(&ns_channel::Channel::consumeMessage, channelPtr.get(),respPtr->qname(), respPtr->msg()));}};
}

三.搭建消费客户端和生产客户端

思路:先创建 Connection 对象,然后用 connection 创建信道,即可使用信道提供的服务


http://www.mrgr.cn/news/37051.html

相关文章:

  • C语言指针系列2——加深理解
  • 使用 sponge + dtm 轻松实现秒杀抢购服务(HTTP),彻底解决库存与订单数据不一致的难题
  • OpenCV
  • Promise笔记
  • 【数据结构】AVL树相关知识详细梳理
  • ubuntu更换镜像源及巧妙使用Python脚本解决文件编码问题
  • 【学习笔记】网络设备(华为交换机)基础知识7——查看硬件信息 ① display device 命令详解
  • 一个证明-待验证
  • Redis配置文件详解(上)
  • Java文件上传同时传入JSON参数
  • 11. Map和Set
  • RabbitMQ下载安装运行环境搭建
  • 大数据新视界 --大数据大厂之数据清洗工具 OpenRefine 实战:清理与转换数据
  • 第18周 3-过滤器
  • 什么是开放式耳机?具有什么特色?非常值得入手的蓝牙耳机推荐
  • Python_list去重复值remove_duplicates
  • 【中级通信工程师】终端与业务(三):电信业务
  • Qt | Linux+QFileSystemWatcher文件夹和文件监视(例如监视U盘挂载目录)
  • ISP下载,IAP,ICP,USB转TTL下载SWIM、DAP-link、CMSIS-DAP、ST-LINK,SPI(通信方式),
  • LeetCode 201. 数字范围按位与