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

海南网站建设推荐网站上那些轮播图视频怎么做的

海南网站建设推荐,网站上那些轮播图视频怎么做的,自己怎样制作公司网站,vps网站管理软件3.4Reactor服务器实现 1.connect封装 ​ 每一个连接都要有一个文件描述符和输入输出缓冲区#xff0c;还有读、写、异常处理的回调方法#xff1b; ​ 还包括指向服务器的回指指针#xff1b; class connection; class tcpserver;using func_t std::functionvoid(s…3.4Reactor服务器实现 1.connect封装 ​ 每一个连接都要有一个文件描述符和输入输出缓冲区还有读、写、异常处理的回调方法 ​ 还包括指向服务器的回指指针 class connection; class tcpserver;using func_t std::functionvoid(std::shared_ptrconnection); class connection { public:connection() {}~connection() {}private:int sockfd_; // 文件描述符std::string inbuffer_; // 输入缓冲区string缺陷就是不可以处理二进制流std::string outbuffer_; // 输出缓冲区func_t rcb_; // 设置回调函数将IO方法交给上层func_t wcb_; // 写回调func_t ecb_; // 异常回调std::shared_ptrtcpserver tsvr_; // 添加一个回指指针 };2.tcpserver封装 ​ 设置epoll对象(用来将连接的属性设置进内核)监听套接字文件描述符和连接的映射并用智能指针管理 ​ 设置nocopy对象不允许服务器拷贝 ​ 通过使用EPOLLET事件实现ET工作模式并且非阻塞文件描述符 ​ 使用addconnection函数实现管理所有的文件描述符 ​ epoll_event结构体数组来获取就绪的事件 #include iostream #include string #include memory #include functional #include unordered_map #include fcntl.h #include unistd.h #include Socket.hpp #include Log.hpp #include nocopy.hpp #include epoll.hpp #include Socket.hpp #include comm.hppclass connection; class tcpserver; uint32_t event_in (EPOLLIN | EPOLLET); uint32_t event_out (EPOLLOUT | EPOLLET); using func_t std::functionvoid(std::shared_ptrconnection); static const uint16_t defaultport 8080;class connection { public:connection(int sockfd, std::shared_ptrtcpserver tsvr) : sockfd_(sockfd), tsvr_(tsvr) {}~connection() {}public:void sethandler(func_t rcb, func_t wcb, func_t ecb){rcb_ rcb;wcb_ wcb;ecb_ ecb;}int Fd(){return sockfd_;}private:int sockfd_; // 文件描述符std::string inbuffer_; // 输入缓冲区string缺陷就是不可以处理二进制流std::string outbuffer_; // 输出缓冲区 public:func_t rcb_ nullptr; // 设置回调函数将IO方法交给上层func_t wcb_ nullptr; // 写回调func_t ecb_ nullptr; // 异常回调std::shared_ptrtcpserver tsvr_; // 添加一个回指指针 };class tcpserver : public nocopy {static const int num 64;public:tcpserver(uint16_t port defaultport): ep_ptr_(new epoll()), listensock_ptr_(new Sock()), port_(port), quit_(true){}~tcpserver(){listensock_ptr_-Close();}public:void init(){listensock_ptr_-Socket();setnonblock(listensock_ptr_-Fd());listensock_ptr_-Bind(port_);listensock_ptr_-Listen();lg(Info, create listen socket success, listensockfd: %d, listensock_ptr_-Fd());addconnection(listensock_ptr_-Fd(), event_in, std::bind(tcpserver::Accept, this, std::placeholders::_1), nullptr, nullptr);}void addconnection(int sockfd, uint32_t event, func_t rcb, func_t wcb, func_t ecb){// 1.建立sockfd对应connection对象std::shared_ptrconnection c_ptr std::make_sharedconnection(sockfd, std::shared_ptrtcpserver(this));c_ptr-sethandler(rcb, wcb, ecb);// 2.将connection添加到映射表中connections_[sockfd] c_ptr;// 3.将fd与事件添加到内核中ep_ptr_-Update(EPOLL_CTL_ADD, sockfd, event);lg(Debug, add a new connection, sockfd: %d, sockfd);}void Accept(std::shared_ptrconnection connection_ptr){while (true){sockaddr_in client;socklen_t len sizeof(client);int sockfd accept(connection_ptr-Fd(), (sockaddr *)client, len);if (sockfd 0){uint16_t clientport ntohs(client.sin_port);char ipstr[64];inet_ntop(AF_INET, client.sin_addr, ipstr, sizeof(ipstr));std::string clientip ipstr;lg(Debug, get a new client, ip: %s, port: %d, sockfd: %d, clientip.c_str(), clientport, sockfd);setnonblock(sockfd);addconnection(sockfd, event_in, nullptr, nullptr, nullptr);}else{if (errno EWOULDBLOCK){break;}else if (errno EINTR){continue;}else{break;}}}}bool IsConnectionSafe(int fd){auto iter connections_.find(fd);if (iter connections_.end()){return false;}return true;}void dispatcher(int timeout){int n ep_ptr_-Wait(r_events_, num, timeout);for (int i 0; i n; i){uint32_t event r_events_[i].events;int sockfd r_events_[i].data.fd;// 统一把异常问题转化成读写问题if (event EPOLLERR){event | (EPOLLIN | EPOLLOUT);}if (event EPOLLHUP){event | (EPOLLIN | EPOLLOUT);}// 处理读写if (IsConnectionSafe(sockfd) (event event_in)){if (connections_[sockfd]-rcb_){connections_[sockfd]-rcb_(connections_[sockfd]);}}if (IsConnectionSafe(sockfd) (event event_out)){if (connections_[sockfd]-wcb_){connections_[sockfd]-wcb_(connections_[sockfd]);}}}}void printconnection(){std::cout connections fds: std::endl;for (auto e : connections_){std::cout e.second-Fd() ;}std::cout std::endl;}void loop(){quit_ false;while (!quit_){dispatcher(3000);printconnection();}quit_ true;}private:std::shared_ptrepoll ep_ptr_; // 创建epoll对象std::shared_ptrSock listensock_ptr_; // 构建了监听套接字对象std::unordered_mapint, std::shared_ptrconnection connections_; // 构建了文件描述符和连接对象的映射epoll_event r_events_[num];uint16_t port_;bool quit_; }; void setnonblock(int fd) {int fl fcntl(fd, F_GETFL);if (fl 0){exit(NON_BLOCK);}fcntl(fd, F_SETFL, fl | O_NONBLOCK); }
http://www.w-s-a.com/news/913846/

相关文章:

  • 免费发布信息网站大全666做p2p网站费用
  • 北京 网站建设咨询顾问公司网络公司有几家
  • 设计类网站如何用ps做网站首页
  • 品牌网站建设的关键事项设计网有哪些
  • 网站没收录徐州建设工程审图中心网站
  • 网站建设记账做什么科目erp系统有哪些软件
  • 泰拳图片做网站用哪里有做空包网站的
  • 查外链网站重庆做网站微信的公司
  • 有没有外包活的网站如何做网站快捷键的元素
  • 公司网站赏析网站制作2019趋势
  • 企业进行网站建设的方式有( )推广引流违法吗
  • 按营销型网站要求重做网站 费用点金网站建设
  • 深圳做网站互联网服务
  • 网站sem托管wordpress安装无法连接数据库
  • 深圳网站建设开发公司哪家好微信小程序商家入口
  • 江门站排名优化建立什么网站赚钱
  • 科普文章在那个网站做招聘网站代做
  • 监控设备东莞网站建设游戏网站域名
  • 对商家而言网站建设的好处网址导航怎么彻底删除
  • app设计网站模板企业展厅策划设计公司有哪些
  • wordpress销售主题手机网站关键词优化
  • 怎么查一个网站是什么程序做的三亚城乡建设局网站
  • 深圳分销网站设计公司做网站一般需要多久
  • 企业网站设计代码丹东seo排名公司
  • 企业网站建设定制开发服务网站建设说课ppt
  • 大连市城乡建设局网站网站免费网站入口
  • 做暧网站网站备案ps
  • 知名网站建设公司电话长子网站建设
  • 网站建设的意义与目的建立什么船籍港
  • 广州注册公司营业执照网站建设代码优化