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

社交网站有哪些如何做wordpress编辑器段间距

社交网站有哪些如何做,wordpress编辑器段间距,数字营销策略有哪些,免费的视频网站推广软件ChatServer 一个TCP服务器必然会有连接的接收,维持,收发数据等逻辑。那我们就要基于asio完成这个服务的搭建。主服务是这个样子的 #include "LogicSystem.h"#include <csignal>#include <thread>#include <mutex>#include "AsioIOServiceP…

ChatServer

一个TCP服务器必然会有连接的接收,维持,收发数据等逻辑。那我们就要基于asio完成这个服务的搭建。主服务是这个样子的

 
  1. #include "LogicSystem.h"
  2. #include <csignal>
  3. #include <thread>
  4. #include <mutex>
  5. #include "AsioIOServicePool.h"
  6. #include "CServer.h"
  7. #include "ConfigMgr.h"
  8. using namespace std;
  9. bool bstop = false;
  10. std::condition_variable cond_quit;
  11. std::mutex mutex_quit;
  12. int main()
  13. {
  14. try {
  15. auto &cfg = ConfigMgr::Inst();
  16. auto pool = AsioIOServicePool::GetInstance();
  17. boost::asio::io_context io_context;
  18. boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
  19. signals.async_wait([&io_context, pool](auto, auto) {
  20. io_context.stop();
  21. pool->Stop();
  22. });
  23. auto port_str = cfg["SelfServer"]["Port"];
  24. CServer s(io_context, atoi(port_str.c_str()));
  25. io_context.run();
  26. }
  27. catch (std::exception& e) {
  28. std::cerr << "Exception: " << e.what() << endl;
  29. }
  30. }

CServer类的声明

 
  1. #include <boost/asio.hpp>
  2. #include "CSession.h"
  3. #include <memory.h>
  4. #include <map>
  5. #include <mutex>
  6. using namespace std;
  7. using boost::asio::ip::tcp;
  8. class CServer
  9. {
  10. public:
  11. CServer(boost::asio::io_context& io_context, short port);
  12. ~CServer();
  13. void ClearSession(std::string);
  14. private:
  15. void HandleAccept(shared_ptr<CSession>, const boost::system::error_code & error);
  16. void StartAccept();
  17. boost::asio::io_context &_io_context;
  18. short _port;
  19. tcp::acceptor _acceptor;
  20. std::map<std::string, shared_ptr<CSession>> _sessions;
  21. std::mutex _mutex;
  22. };

构造函数中监听对方连接

 
  1. CServer::CServer(boost::asio::io_context& io_context, short port):_io_context(io_context), _port(port),
  2. _acceptor(io_context, tcp::endpoint(tcp::v4(),port))
  3. {
  4. cout << "Server start success, listen on port : " << _port << endl;
  5. StartAccept();
  6. }

接受连接的函数

 
  1. void CServer::StartAccept() {
  2. auto &io_context = AsioIOServicePool::GetInstance()->GetIOService();
  3. shared_ptr<CSession> new_session = make_shared<CSession>(io_context, this);
  4. _acceptor.async_accept(new_session->GetSocket(), std::bind(&CServer::HandleAccept, this, new_session, placeholders::_1));
  5. }

AsioIOServicePool

从AsioIOServicePool中返回一个可用的iocontext构造Session,然后将接受的新链接的socket写入这个Session保管。

AsioIOServicePool已经在前面讲解很多次了,它的声明如下

 
  1. #include <vector>
  2. #include <boost/asio.hpp>
  3. #include "Singleton.h"
  4. class AsioIOServicePool:public Singleton<AsioIOServicePool>
  5. {
  6. friend Singleton<AsioIOServicePool>;
  7. public:
  8. using IOService = boost::asio::io_context;
  9. using Work = boost::asio::io_context::work;
  10. using WorkPtr = std::unique_ptr<Work>;
  11. ~AsioIOServicePool();
  12. AsioIOServicePool(const AsioIOServicePool&) = delete;
  13. AsioIOServicePool& operator=(const AsioIOServicePool&) = delete;
  14. // 使用 round-robin 的方式返回一个 io_service
  15. boost::asio::io_context& GetIOService();
  16. void Stop();
  17. private:
  18. AsioIOServicePool(std::size_t size = std::thread::hardware_concurrency());
  19. std::vector<IOService> _ioServices;
  20. std::vector<WorkPtr> _works;
  21. std::vector<std::thread> _threads;
  22. std::size_t _nextIOService;
  23. };

AsioIOServicePool具体实现

 
  1. #include "AsioIOServicePool.h"
  2. #include <iostream>
  3. using namespace std;
  4. AsioIOServicePool::AsioIOServicePool(std::size_t size):_ioServices(size),
  5. _works(size), _nextIOService(0){
  6. for (std::size_t i = 0; i < size; ++i) {
  7. _works[i] = std::unique_ptr<Work>(new Work(_ioServices[i]));
  8. }
  9. //遍历多个ioservice,创建多个线程,每个线程内部启动ioservice
  10. for (std::size_t i = 0; i < _ioServices.size(); ++i) {
  11. _threads.emplace_back([this, i]() {
  12. _ioServices[i].run();
  13. });
  14. }
  15. }
  16. AsioIOServicePool::~AsioIOServicePool() {
  17. std::cout << "AsioIOServicePool destruct" << endl;
  18. }
  19. boost::asio::io_context& AsioIOServicePool::GetIOService() {
  20. auto& service = _ioServices[_nextIOService++];
  21. if (_nextIOService == _ioServices.size()) {
  22. _nextIOService = 0;
  23. }
  24. return service;
  25. }
  26. void AsioIOServicePool::Stop(){
  27. //因为仅仅执行work.reset并不能让iocontext从run的状态中退出
  28. //当iocontext已经绑定了读或写的监听事件后,还需要手动stop该服务。
  29. for (auto& work : _works) {
  30. //把服务先停止
  31. work->get_io_context().stop();
  32. work.reset();
  33. }
  34. for (auto& t : _threads) {
  35. t.join();
  36. }
  37. }

CServer的处理连接逻辑

 
  1. void CServer::HandleAccept(shared_ptr<CSession> new_session, const boost::system::error_code& error){
  2. if (!error) {
  3. new_session->Start();
  4. lock_guard<mutex> lock(_mutex);
  5. _sessions.insert(ma

文章转载自:

http://ERrN9sqB.pfnwt.cn
http://1XYYVXBV.pfnwt.cn
http://zTReFYkw.pfnwt.cn
http://HfG9AXPq.pfnwt.cn
http://FDHLsXxc.pfnwt.cn
http://jYsRBaxc.pfnwt.cn
http://6dRYIIVu.pfnwt.cn
http://cgg1I7Ip.pfnwt.cn
http://FkNRVeka.pfnwt.cn
http://plRm7p08.pfnwt.cn
http://U5SN1oQ8.pfnwt.cn
http://BJchwqTa.pfnwt.cn
http://W4DRFaGA.pfnwt.cn
http://KPb0uD2n.pfnwt.cn
http://p9xdrKwk.pfnwt.cn
http://oYiGpkET.pfnwt.cn
http://r1L11G1A.pfnwt.cn
http://TY8S9fJF.pfnwt.cn
http://HZvIPrMi.pfnwt.cn
http://ve4jgBxM.pfnwt.cn
http://OOLcnf4l.pfnwt.cn
http://DxfBoDCG.pfnwt.cn
http://Dsjhk99Q.pfnwt.cn
http://wxU1EdPK.pfnwt.cn
http://gzYJvviD.pfnwt.cn
http://NFW7fB1j.pfnwt.cn
http://cBjZ7LjB.pfnwt.cn
http://Nl17X20f.pfnwt.cn
http://Bl3oXbRC.pfnwt.cn
http://5GSgDPbk.pfnwt.cn
http://www.dtcms.com/wzjs/689265.html

相关文章:

  • 如何快速做单页面网站seo计费系统登录
  • 永州网站建设优化十大看免费行情的软件下载大全
  • 互联网网站建设公司两学一做 官方网站
  • 有没有专业做挂的网站盐城网站关键词优化
  • 开化网站建设东莞网站优化教程
  • 罗湖区网站建设多少钱手工制作小店铺
  • 端午节网站建设装修公司网站建设方案
  • 做化工贸易要用那些网站推广网站域名备案号查询
  • 怀化网站优化联系方式数商云官网
  • 网站机房建设流程腾讯云远程安装wordpress
  • 大连网站建设哪个公司好互联网怎么学
  • 龙岗网站改版搜索引擎查重
  • 织梦网站手机版怎么做最吉利旺财的建筑公司名字
  • 怎么做qq空间支付网站网站建设公司 销量
  • 广东平台网站建设制作官方网站撰写策划书
  • 做新闻封面的网站wordpress调整小工具位置
  • 做网站跳转怎么收费群晖服务器做网站
  • 网站导航html源码金融代理平台代理加盟
  • 网站怎么做安全购物网站后台怎么做
  • 做网站需要用什么技术主机做网站工具
  • 网站制作哪里做得好免费网站生成
  • 手机怎么做钓鱼网站wordpress 坐标
  • 成都网站制作028net四川宜宾网站建设
  • 学校网站建设存在的问题购物网站开发一般使用什么语言
  • 招网站开发人员wordpress用户个人主页
  • 给公司做门户网站d开头的做网站的软件
  • 网站建设开发员丰县建设局规划局网站
  • 涿州市网站建设做电影网站被找版权问题怎么处理
  • 网站用asp还是php苏州装饰公司
  • 只用html5可以做网站吗吴江区建设工程招标网站