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

微网站建设及微信公众号女生适合学前端还是后端

微网站建设及微信公众号,女生适合学前端还是后端,企业年金怎么缴纳,建站导航简介 poller是I/O多路复用接口抽象虚基类,对I/O多路复用API的封装,muduo提供了EPollPoller和PollPoller派生类(epoll和poll),所以不支持select. newDefaultPoller()默认选择epoll 主要接口 poll 是Poller的核心功能#xff0c;使用派生类的poll或者epoll_wait来阻塞等待I…简介 poller是I/O多路复用接口抽象虚基类,对I/O多路复用API的封装,muduo提供了EPollPoller和PollPoller派生类(epoll和poll),所以不支持select. newDefaultPoller()默认选择epoll 主要接口 poll 是Poller的核心功能使用派生类的poll或者epoll_wait来阻塞等待IO事件发生 通过派生类的实现来填充EventLoop的activeChannelList_ static createNewPoller 工厂函数创建一个Poller实例 在EpollPoller中每个实例对应一个epollfd update 更新I/O多路复用的状态例如epoll_ctl的ADDMODDEL 主要成员 loop 控制当前Poller的EventLoop指针 其余成员由派生类实现 源码剖析 poller.h #ifndef MUDUO_NET_POLLER_H #define MUDUO_NET_POLLER_H#include map #include vector#include muduo/base/Timestamp.h #include muduo/net/EventLoop.hnamespace muduo { namespace net {class Channel;/// /// Base class for IO Multiplexing /// /// This class doesnt own the Channel objects. class Poller : noncopyable {public:typedef std::vectorChannel* ChannelList;Poller(EventLoop* loop);virtual ~Poller();/// Polls the I/O events./// Must be called in the loop thread.virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) 0;/// Changes the interested I/O events./// Must be called in the loop thread.virtual void updateChannel(Channel* channel) 0;/// Remove the channel, when it destructs./// Must be called in the loop thread.virtual void removeChannel(Channel* channel) 0;//判断是否存在virtual bool hasChannel(Channel* channel) const;//创建一个poller,默认是epollstatic Poller* newDefaultPoller(EventLoop* loop);void assertInLoopThread() const{ownerLoop_-assertInLoopThread();}protected:typedef std::mapint, Channel* ChannelMap;ChannelMap channels_;private:EventLoop* ownerLoop_; };} // namespace net } // namespace muduo#endif // MUDUO_NET_POLLER_H poller.cc #include muduo/net/Poller.h #include muduo/net/Channel.husing namespace muduo; using namespace muduo::net;Poller::Poller(EventLoop* loop): ownerLoop_(loop) { }Poller::~Poller() default;bool Poller::hasChannel(Channel* channel) const {assertInLoopThread();ChannelMap::const_iterator it channels_.find(channel-fd());return it ! channels_.end() it-second channel; }EPollPoller.h #ifndef MUDUO_NET_POLLER_EPOLLPOLLER_H #define MUDUO_NET_POLLER_EPOLLPOLLER_H#include muduo/net/Poller.h#include vectorstruct epoll_event;namespace muduo { namespace net {/// /// IO Multiplexing with epoll(4). /// class EPollPoller : public Poller {public:EPollPoller(EventLoop* loop);~EPollPoller() override;Timestamp poll(int timeoutMs, ChannelList* activeChannels) override;void updateChannel(Channel* channel) override;void removeChannel(Channel* channel) override;private:static const int kInitEventListSize 16;static const char* operationToString(int op);void fillActiveChannels(int numEvents,ChannelList* activeChannels) const;void update(int operation, Channel* channel);typedef std::vectorstruct epoll_event EventList;int epollfd_;EventList events_; };} // namespace net } // namespace muduo #endif // MUDUO_NET_POLLER_EPOLLPOLLER_H EPollPoller.cc // Copyright 2010, Shuo Chen. All rights reserved. // http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)#include muduo/net/poller/EPollPoller.h#include muduo/base/Logging.h #include muduo/net/Channel.h#include assert.h #include errno.h #include poll.h #include sys/epoll.h #include unistd.husing namespace muduo; using namespace muduo::net;/*struct epoll_event {uint32_t events; //Epoll eventsepoll_data_t data; //User data variable } __attribute__ ((__packed__));typedef union epoll_data {void *ptr;int fd;uint32_t u32;uint64_t u64; } epoll_data_t;*/// On Linux, the constants of poll(2) and epoll(4) // are expected to be the same. static_assert(EPOLLIN POLLIN, epoll uses same flag values as poll); static_assert(EPOLLPRI POLLPRI, epoll uses same flag values as poll); static_assert(EPOLLOUT POLLOUT, epoll uses same flag values as poll); static_assert(EPOLLRDHUP POLLRDHUP, epoll uses same flag values as poll); static_assert(EPOLLERR POLLERR, epoll uses same flag values as poll); static_assert(EPOLLHUP POLLHUP, epoll uses same flag values as poll);namespace { const int kNew -1; //channel尚未添加到poller中 const int kAdded 1; //已经添加了 const int kDeleted 2; //之前监听过了后来移除了监听 }//当flag EPOLL_CLOEXEC创建的epfd会设置FD_CLOEXEC //FD_CLOEXEC表示当程序执行exec函数时本fd将被系统自动关闭,表示不传递给exec创建的新进程 EPollPoller::EPollPoller(EventLoop* loop): Poller(loop),//创建epollfd使用带1的版本//如果参数为0,则与epoll_create版本相同,设置为O_CLOEXEC,查看open函数的这个参数解释,//子进程fork并调用exec时会关闭这个fdepollfd_(::epoll_create1(EPOLL_CLOEXEC)), events_(kInitEventListSize) //vector这样用时初始化kInitEventListSize个大小空间,默认16 {if (epollfd_ 0) //在构造函数中判断0就abort(){LOG_SYSFATAL EPollPoller::EPollPoller;} }EPollPoller::~EPollPoller() {::close(epollfd_); }Timestamp EPollPoller::poll(int timeoutMs, ChannelList* activeChannels)//ChannelList是一个存放channel的vector {LOG_TRACE fd total count channels_.size();int numEvents ::epoll_wait(epollfd_,*events_.begin(), //events_已初始化,是存放epoll_event的vectorstatic_castint(events_.size()), //监控套接字的数目timeoutMs);int savedErrno errno;Timestamp now(Timestamp::now());if (numEvents 0){LOG_TRACE numEvents events happened;fillActiveChannels(numEvents, activeChannels);if (implicit_castsize_t(numEvents) events_.size()) //如果返回的事件数目等于当前事件数组大小就分配2倍空间{events_.resize(events_.size()*2);}}else if (numEvents 0){LOG_TRACE nothing happened;}else{// error happens, log uncommon onesif (savedErrno ! EINTR){errno savedErrno;LOG_SYSERR EPollPoller::poll();}}return now; }//把返回到的这么多个事件添加到activeChannels void EPollPoller::fillActiveChannels(int numEvents,ChannelList* activeChannels) const {assert(implicit_castsize_t(numEvents) events_.size());for (int i 0; i numEvents; i) //确定它的大小小于events_的大小因为events_是预留的事件vector{Channel* channel static_castChannel*(events_[i].data.ptr); #ifndef NDEBUGint fd channel-fd(); //debug时做一下检测ChannelMap::const_iterator it channels_.find(fd);assert(it ! channels_.end());assert(it-second channel); #endifchannel-set_revents(events_[i].events); //把已发生的事件传给channel,写到通道当中activeChannels-push_back(channel); //并且push_back进activeChannels} }//这个函数被调用是因为channel-enablereading()被调用再调用channel-update()再event_loop-updateChannel()再-epoll或poll的updateChannel被调用 // void EPollPoller::updateChannel(Channel* channel) {Poller::assertInLoopThread(); //在IO线程const int index channel-index(); //初始状态index是-1LOG_INFO fd channel-fd() events channel-events() index index;// 当是新的或是之前监听过后来移除了监听// 两者的区别在于,新的channel 之前没有在epoll 中保存// 而 del 的之前在 channels_ 中保存了但是没有被放入epoll_ctl中监听if (index kNew || index kDeleted) //index是在poll中是下标在epoll中是三种状态上面有三个常量{// a new one, add with EPOLL_CTL_ADDint fd channel-fd();if (index kNew){assert(channels_.find(fd) channels_.end()); //channels_是一个Mapchannels_[fd] channel;}else // index kDeleted{assert(channels_.find(fd) ! channels_.end());assert(channels_[fd] channel);}channel-set_index(kAdded);update(EPOLL_CTL_ADD, channel); //注册事件}else{// update existing one with EPOLL_CTL_MOD/DELint fd channel-fd();(void)fd;assert(channels_.find(fd) ! channels_.end());assert(channels_[fd] channel);assert(index kAdded);// 既然已经添加了那么可能的修改就是修改监听的时间或者不在监听// 因此这里先判断是否是没有监听的事件了如果是那么直接移除、if (channel-isNoneEvent()) //判断无事件{update(EPOLL_CTL_DEL, channel); //删除事件channel-set_index(kDeleted); //删除后被设置为kDeleted}else{update(EPOLL_CTL_MOD, channel); //修改已注册的监听事件}} }void EPollPoller::removeChannel(Channel* channel) {Poller::assertInLoopThread(); //判断是否在IO线程int fd channel-fd();LOG_TRACE fd fd;assert(channels_.find(fd) ! channels_.end());assert(channels_[fd] channel);assert(channel-isNoneEvent());int index channel-index();assert(index kAdded || index kDeleted);size_t n channels_.erase(fd); //删除(void)n; assert(n 1);if (index kAdded){update(EPOLL_CTL_DEL, channel);}channel-set_index(kNew); }void EPollPoller::update(int operation, Channel* channel) {printf(-------%s,line.%d-------\n,__FUNCTION__,__LINE__);struct epoll_event event; //存放数据的结构体memZero(event, sizeof event);event.events channel-events(); //注册的事件event.data.ptr channel;int fd channel-fd();LOG_INFO epoll_ctl op operationToString(operation) fd fd event { channel-eventsToString() };if (::epoll_ctl(epollfd_, operation, fd, event) 0)//epoll_ctl失败返回-1{if (operation EPOLL_CTL_DEL){LOG_SYSERR epoll_ctl op operationToString(operation) fd fd;}else{LOG_SYSFATAL epoll_ctl op operationToString(operation) fd fd;}} }const char* EPollPoller::operationToString(int op) {switch (op){case EPOLL_CTL_ADD:return ADD;case EPOLL_CTL_DEL:return DEL;case EPOLL_CTL_MOD:return MOD;default:assert(false ERROR op);return Unknown Operation;} }
http://wiki.neutronadmin.com/news/170480/

相关文章:

  • 网站建设人员的安排沧州网站建设益志科技
  • 湖南做网站磐石网络电子商务网站页面设计图片
  • 怎么样做美术招生信息网站那个网站做淘宝推广比较好
  • OA 公司网站 铁道建设报12366纳税服务平台
  • 凡科建设网站股权分配系统建设网站
  • 手机网站开发流程.咨询公司的经营范围有哪些
  • 网站建设比较好的智能手机app开发
  • 中国做国际期货最大的网站网站建设有关书籍
  • 德育工作网站建设方案江苏百度推广代理商
  • 网站为什么被降权杭州vi设计策划
  • 高端建设网站公司哪家好收到网站打入0.1元怎么做分录
  • 网站建设流程表wordpress搬家修改域名
  • 郑州网站建设动态松江品划网络做网站
  • 网站开发案例pdf微网站建设目的
  • 网站建设网络推广方案前端开发人员
  • 酷 网站模板做小程序的公司有哪些比较好?
  • 哪个网站可以做微商宜昌做网站的
  • 求一个做交通分析的底图网站网站链接跳转如何做
  • 高端的网站邹平网站设计
  • 什么网站上面能接点小活做湖北 商城网站建设
  • 东莞网站的制作做网站来联盟怎么样
  • 佛山市外贸网站建设校园微网站建设方案ppt模板
  • 网上做效果图网站有哪些网站跟域名是什么关系
  • 百度推广文案seo公司系统
  • 残疾人网站服务平台wordpress用网站测速
  • 云南建设企业网站智慧团建怎么转团关系
  • 男女直接做的视频网站一份完整的活动策划
  • 新注册公司网站建设网站在哪里设置关键词
  • 怎样自己做qq网站网络开发理论
  • 创建网站的各项费用wordpress 提交