boost asio 一个聊天的基本框架
示例代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "Util.h" 2 #include "MyAsio.h" 3 #include "TcpConnectionManager.h" 4 #include "SocketMessageRecvDispatchManager.h" 5 /* 6 作 者: itdef 7 技术博客 http://www.cnblogs.com/itdef/ 8 技术交流群 群号码:324164944 9 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流 10 部分老代码存放地点 http://www.oschina.net/code/list_by_user?id=614253 11 */ 12 13 unsigned int DEF::tcp_connection::i = 0; 14 extern DEF::TcpConnectManager g_tcp_connect_manager; 15 extern DEF::SocketMessageRecvDispatchManager g_socket_message_recv_dispatch_manager; 16 17 void DEF::AsioLoopThread() { 18 try 19 { 20 boost::asio::io_service io_service; 21 DEF::tcp_server server(io_service); 22 io_service.run(); 23 } 24 catch (std::exception& e) 25 { 26 std::cerr << e.what() << std::endl; 27 } 28 } 29 30 std::thread DEF::StartAsioLoopThread() 31 { 32 std::thread t = std::thread(DEF::AsioLoopThread); 33 return t; 34 } 35 36 void DEF::tcp_connection::handle_read_head(const boost::system::error_code& error, 37 size_t bytes_transferred) 38 { 39 //收到错误格式信息 40 if (error || bufHead_.flag != '|') 41 { 42 std::weak_ptr<DEF::tcp_connection> wp(shared_from_this()); 43 g_tcp_connect_manager.LeaveWithLock(wp); 44 std::cerr << __FUNCTION__ << "(). wrong flag or " << error.message()<< std::endl; 45 return; 46 } 47 48 boost::asio::async_read(socket_, boost::asio::buffer(recvBuffBody, bufHead_.bufferLenth), 49 boost::bind(&tcp_connection::handle_read_body, shared_from_this(), 50 boost::asio::placeholders::error, 51 boost::asio::placeholders::bytes_transferred)); 52 } 53 54 void DEF::tcp_connection::start() 55 { 56 std::weak_ptr<DEF::tcp_connection> wp(shared_from_this()); 57 g_tcp_connect_manager.JoinWithLock(wp); 58 59 boost::asio::async_read(socket_, boost::asio::buffer(&bufHead_, sizeof(bufHead_)), 60 boost::bind(&tcp_connection::handle_read_head, shared_from_this(), 61 boost::asio::placeholders::error, 62 boost::asio::placeholders::bytes_transferred)); 63 } 64 65 std::string DEF::SharedPtr2StringId(const std::shared_ptr<DEF::tcp_connection>& ptr) { 66 std::stringstream ss; 67 ss << ptr; 68 std::string str_ptr_id; 69 ss >> str_ptr_id; 70 return str_ptr_id; 71 } 72 73 void DEF::tcp_connection::handle_read_body(const boost::system::error_code& error, 74 size_t bytes_transferred) 75 { 76 if (error) { 77 std::weak_ptr<DEF::tcp_connection> wp(shared_from_this()); 78 g_tcp_connect_manager.LeaveWithLock(wp); 79 std::cerr << __FUNCTION__ <<" "<< error.message()<< std::endl; 80 return; 81 } 82 recvBuffBody[bytes_transferred] = '\0'; 83 //std::cout << "recv " << recvBuffBody << std::endl; 84 g_socket_message_recv_dispatch_manager.PostSockRecvMessageAndNotify(recvBuffBody); 85 boost::asio::async_read(socket_, boost::asio::buffer(&bufHead_, sizeof(bufHead_)), 86 boost::bind(&tcp_connection::handle_read_head, shared_from_this(), 87 boost::asio::placeholders::error, 88 boost::asio::placeholders::bytes_transferred)); 89 }
消息队列表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma once 2 #include <mutex> 3 #include <deque> 4 #include <string> 5 6 #include "Util.h" 7 /* 8 作 者: itdef 9 技术博客 http://www.cnblogs.com/itdef/ 10 技术交流群 群号码:324164944 11 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流 12 部分老代码存放地点 http://www.oschina.net/code/list_by_user?id=614253 13 */ 14 NAMESPACEBEGIN(DEF) 15 16 struct MessageQueueWithLock { 17 std::mutex messageQueuemtx; 18 std::condition_variable messageQueuecv; 19 volatile bool ready = false; 20 std::deque<std::string> messageDeq; 21 22 void PostMessageAndNotify(const std::string& json) { 23 std::lock_guard<std::mutex> lck(messageQueuemtx); 24 messageDeq.push_back(json); 25 messageQueuecv.notify_one(); 26 } 27 28 std::string WaitForMessageDequeOnce() { 29 std::string s; 30 { 31 std::unique_lock<std::mutex> lck(messageQueuemtx); 32 while (messageDeq.empty()) 33 messageQueuecv.wait(lck); 34 } 35 { 36 std::lock_guard<std::mutex> lck(messageQueuemtx); 37 if (!messageDeq.empty()) { 38 s = messageDeq.front(); 39 messageDeq.pop_front(); 40 } 41 } 42 return s; 43 } 44 }; 45 46 NAMESPACEEND
一个全局管理连接的容器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma once 2 #pragma once 3 #include "Util.h" 4 #include <map> 5 #include <mutex> 6 #include <memory> 7 #include <sstream> 8 9 #include "MyAsio.h" 10 /* 11 作 者: itdef 12 技术博客 http://www.cnblogs.com/itdef/ 13 技术交流群 群号码:324164944 14 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流 15 部分老代码存放地点 http://www.oschina.net/code/list_by_user?id=614253 16 */ 17 NAMESPACEBEGIN(DEF) 18 //class tcp_connection; 19 class TcpConnectManager { 20 public: 21 typedef std::string SOCKWEAKPTRID; 22 TcpConnectManager() {} 23 ~TcpConnectManager() {} 24 25 void JoinWithLock(const std::weak_ptr<tcp_connection>& ptr); 26 27 void LeaveWithLock(const std::weak_ptr<tcp_connection>& ptr); 28 29 std::weak_ptr<tcp_connection> FindWeakPtrBySockPtrIdWithLock(const std::string& sock_ptr_id); 30 TcpConnectManager& operator=(const TcpConnectManager&) = delete; 31 TcpConnectManager(const TcpConnectManager&) = delete; 32 private: 33 std::mutex mtx; 34 std::map<SOCKWEAKPTRID, std::weak_ptr<DEF::tcp_connection>> connection_infos; 35 }; 36 37 38 NAMESPACEEND
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
![阿里打赏](https://files.cnblogs.com/files/itdef/ali2.bmp)
![微信打赏](https://files.cnblogs.com/files/itdef/weixin2.bmp)