Message-Queues beta 1.1
A Message-Queues based Cpp
 
载入中...
搜索中...
未找到
connection.hpp
浏览该文件的文档.
1
11#include "channel.hpp"
12
13namespace XuMQ
14{
17 class Connection
18 {
19 public:
20 using ptr = std::shared_ptr<Connection>;
28 const ProtobufCodecPtr &codec, const muduo::net::TcpConnectionPtr &conn,
29 const threadpool::ptr &pool) : _conn(conn), _codec(codec), _cmp(cmp), _host(host), _pool(pool),
30 _channels(std::make_shared<ChannelManager>()) {}
34 {
35 // 判断信道id是否重复 创建信道
36 bool ret = _channels->openChannel(req->cid(), _host, _cmp, _codec, _conn, _pool);
37 if (ret == false)
38 {
39 error(logger, "创建信道时 信道id重复!");
40 basicRespFunc(false, req->rid(), req->cid());
41 return false;
42 }
43 debug(logger, "信道创建成功!信道id为:%s", req->cid().c_str());
44 // 给客户端回复
45 basicRespFunc(true, req->rid(), req->cid());
46 return true;
47 }
51 {
52 _channels->closeChannel(req->cid());
53 basicRespFunc(true, req->rid(), req->cid());
54 }
55
56 Channel::ptr getChannel(const std::string &cid)
57 {
58 return _channels->getChannel(cid);
59 }
60
61 private:
66 void basicRespFunc(bool ok, const std::string &rid, const std::string &cid)
67 {
68 basicResponse resp;
69 resp.set_rid(rid);
70 resp.set_cid(cid);
71 resp.set_ok(ok);
72 _codec->send(_conn, resp);
73 }
74
75 private:
76 muduo::net::TcpConnectionPtr _conn;
82 };
83
87 {
88 public:
89 using ptr = std::shared_ptr<ConnectionManager>;
99 const ProtobufCodecPtr &codec, const muduo::net::TcpConnectionPtr &conn,
100 const threadpool::ptr &pool)
101 {
102 std::unique_lock lock(_mutex);
103 auto it = _conns.find(conn);
104 if (it != _conns.end())
105 return;
106 Connection::ptr self_conn = std::make_shared<Connection>(host, cmp, codec, conn, pool);
107 _conns.insert(std::make_pair(conn, self_conn));
108 }
111 void deleteConnection(const muduo::net::TcpConnectionPtr &conn)
112 {
113 std::unique_lock lock(_mutex);
114 _conns.erase(conn);
115 }
119 Connection::ptr getConnection(const muduo::net::TcpConnectionPtr &conn)
120 {
121 std::unique_lock lock(_mutex);
122 auto it = _conns.find(conn);
123 if (it == _conns.end())
124 return Connection::ptr();
125 return it->second;
126 }
127
128 private:
129 std::mutex _mutex;
130 std::unordered_map<muduo::net::TcpConnectionPtr, Connection::ptr> _conns;
131 };
132}
std::shared_ptr< Channel > ptr
信道句柄
Definition channel.hpp:32
信道管理类
Definition channel.hpp:317
std::shared_ptr< ChannelManager > ptr
信道管理句柄
Definition channel.hpp:319
void basicRespFunc(bool ok, const std::string &rid, const std::string &cid)
基础响应发送函数
Definition connection.hpp:66
Connection(const VirtualHost::ptr &host, const ConsumerManager::ptr &cmp, const ProtobufCodecPtr &codec, const muduo::net::TcpConnectionPtr &conn, const threadpool::ptr &pool)
连接构造函数
Definition connection.hpp:27
ProtobufCodecPtr _codec
协议处理器
Definition connection.hpp:138
bool openChannel(const openChannelRequestPtr &req)
打开信道
Definition connection.hpp:33
ConsumerManager::ptr _cmp
消费者管理句柄
Definition connection.hpp:78
threadpool::ptr _pool
线程池
Definition connection.hpp:80
std::shared_ptr< Connection > ptr
连接管理句柄
Definition connection.hpp:31
Channel::ptr getChannel(const std::string &cid)
Definition connection.hpp:56
ChannelManager::ptr _channels
信道管理句柄
Definition connection.hpp:140
muduo::net::TcpConnectionPtr _conn
客户端连接
Definition connection.hpp:135
VirtualHost::ptr _host
虚拟机
Definition connection.hpp:79
void closeChannel(const closeChannelRequestPtr &req)
关闭信道
Definition connection.hpp:50
连接管理类
Definition connection.hpp:87
void newConnection(const VirtualHost::ptr &host, const ConsumerManager::ptr &cmp, const ProtobufCodecPtr &codec, const muduo::net::TcpConnectionPtr &conn, const threadpool::ptr &pool)
新建一个连接
Definition connection.hpp:98
std::mutex _mutex
互斥锁
Definition connection.hpp:129
std::unordered_map< muduo::net::TcpConnectionPtr, Connection::ptr > _conns
一个从muduo连接管理句柄到连接管理句柄的映射表
Definition connection.hpp:130
void deleteConnection(const muduo::net::TcpConnectionPtr &conn)
删除一个连接
Definition connection.hpp:111
std::shared_ptr< ConnectionManager > ptr
连接管理句柄
Definition connection.hpp:89
Connection::ptr getConnection(const muduo::net::TcpConnectionPtr &conn)
获取一个连接
Definition connection.hpp:119
ConnectionManager()
构造函数
Definition connection.hpp:91
std::shared_ptr< ConsumerManager > ptr
消息队列管理器指针
Definition consumer.hpp:152
std::shared_ptr< VirtualHost > ptr
Definition host.hpp:25
Definition protocol.pb.h:2834
void set_ok(bool value)
Definition protocol.pb.h:5761
void set_cid(ArgT0 &&arg0, ArgT... args)
void set_rid(ArgT0 &&arg0, ArgT... args)
std::shared_ptr< threadpool > ptr
Definition threadpool.hpp:18
XuMQ::ConsumerManager::ptr cmp
Definition mqconsumer.cpp:4
Definition channel.hpp:22
std::shared_ptr< openChannelRequest > openChannelRequestPtr
打开信道请求
Definition channel.hpp:33
Xulog::Logger::ptr logger
日志器的智能指针类型
Definition logger.hpp:24
std::shared_ptr< closeChannelRequest > closeChannelRequestPtr
关闭信道请求
Definition channel.hpp:34
std::shared_ptr< ProtobufCodec > ProtobufCodecPtr
协议处理句柄
Definition channel.hpp:24
信道与信道管理模块的头文件,包含了信道的声明和其管理类的定义。