Channel类详解[1-3]
Posted ygmdream
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Channel类详解[1-3]相关的知识,希望对你有一定的参考价值。
Channel.h
// 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)
//
// This is an internal header file, you should not include this.
#ifndef MUDUO_NET_CHANNEL_H
#define MUDUO_NET_CHANNEL_H
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <muduo/base/Timestamp.h>
namespace muduo
namespace net
class EventLoop;
///
/// A selectable I/O channel.
///
/// This class doesn't own the file descriptor.
/// The file descriptor could be a socket,
/// an eventfd, a timerfd, or a signalfd
class Channel : boost::noncopyable
public:
/*
EventCallback 为指向Channel 类的成员函数void();
ReadEventCallback 为指向Channel 类的成员函数void(Timestamp)
*/
typedef boost::function<void()> EventCallback;
typedef boost::function<void(Timestamp)> ReadEventCallback;
Channel(EventLoop* loop, int fd);
~Channel();
void handleEvent(Timestamp receiveTime);
/*
将ReadEventCallback 指向的函数赋给对象成员
将EventCallback 指向的函数赋给对象成员
*/
void setReadCallback(const ReadEventCallback& cb)
readCallback_ = cb;
void setWriteCallback(const EventCallback& cb)
writeCallback_ = cb;
void setCloseCallback(const EventCallback& cb)
closeCallback_ = cb;
void setErrorCallback(const EventCallback& cb)
errorCallback_ = cb;
/// Tie this channel to the owner object managed by shared_ptr,
/// prevent the owner object being destroyed in handleEvent.
void tie(const boost::shared_ptr<void>&);
int fd() const return fd_;
int events() const return events_;
void set_revents(int revt) revents_ = revt; // used by pollers
// int revents() const return revents_;
bool isNoneEvent() const return events_ == kNoneEvent;
void enableReading() events_ |= kReadEvent; update();
// void disableReading() events_ &= ~kReadEvent; update();
void enableWriting() events_ |= kWriteEvent; update();
void disableWriting() events_ &= ~kWriteEvent; update();
void disableAll() events_ = kNoneEvent; update();
bool isWriting() const return events_ & kWriteEvent;
// for Poller
int index() return index_;
void set_index(int idx) index_ = idx;
// for debug
string reventsToString() const;
void doNotLogHup() logHup_ = false;
EventLoop* ownerLoop() return loop_;
void remove();
private:
void update();
void handleEventWithGuard(Timestamp receiveTime);
static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;
EventLoop* loop_;
const int fd_;
int events_;
int revents_;
int index_; // used by Poller.
bool logHup_;
boost::weak_ptr<void> tie_;
bool tied_;
bool eventHandling_;
ReadEventCallback readCallback_;
EventCallback writeCallback_;
EventCallback closeCallback_;
EventCallback errorCallback_;
;
#endif // MUDUO_NET_CHANNEL_H
Channel.cpp
// 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/base/Logging.h>
#include <muduo/net/Channel.h>
#include <muduo/net/EventLoop.h>
#include <sstream>
#include <poll.h>
using namespace muduo;
using namespace muduo::net;
const int Channel::kNoneEvent = 0;
const int Channel::kReadEvent = POLLIN | POLLPRI;
const int Channel::kWriteEvent = POLLOUT;
Channel::Channel(EventLoop* loop, int fd__)
: loop_(loop),
fd_(fd__),
events_(0),
revents_(0),
index_(-1),
logHup_(true),
tied_(false),
eventHandling_(false)
Channel::~Channel()
assert(!eventHandling_);
/*弱函数指针tie_ 指向了强函数指针obj,
tied_ = true表明弱函数指针tie_ 已经被赋值*/
void Channel::tie(const boost::shared_ptr<void>& obj)
tie_ = obj;
tied_ = true;
void Channel::update()
loop_->updateChannel(this);
void Channel::remove()
assert(isNoneEvent());
loop_->removeChannel(this);
void Channel::handleEvent(Timestamp receiveTime)
boost::shared_ptr<void> guard;
if (tied_)
guard = tie_.lock();
if (guard)
handleEventWithGuard(receiveTime);
else
handleEventWithGuard(receiveTime);
/*根据当前socketfd 对应的revents_ 发生的事件调用
设置好的不同的回调函数
readCallback_;
writeCallback_;
closeCallback_;
errorCallback_;
*/
void Channel::handleEventWithGuard(Timestamp receiveTime)
eventHandling_ = true;
if ((revents_ & POLLHUP) && !(revents_ & POLLIN))
if (logHup_)
LOG_WARN << "Channel::handle_event() POLLHUP";
if (closeCallback_) closeCallback_();
if (revents_ & POLLNVAL)
LOG_WARN << "Channel::handle_event() POLLNVAL";
if (revents_ & (POLLERR | POLLNVAL))
if (errorCallback_) errorCallback_();
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
if (readCallback_) readCallback_(receiveTime);
if (revents_ & POLLOUT)
if (writeCallback_) writeCallback_();
eventHandling_ = false;
string Channel::reventsToString() const
std::ostringstream oss;
oss << fd_ << ": ";
if (revents_ & POLLIN)
oss << "IN ";
if (revents_ & POLLPRI)
oss << "PRI ";
if (revents_ & POLLOUT)
oss << "OUT ";
if (revents_ & POLLHUP)
oss << "HUP ";
if (revents_ & POLLRDHUP)
oss << "RDHUP ";
if (revents_ & POLLERR)
oss << "ERR ";
if (revents_ & POLLNVAL)
oss << "NVAL ";
return oss.str().c_str();
以上是关于Channel类详解[1-3]的主要内容,如果未能解决你的问题,请参考以下文章