-
Notifications
You must be signed in to change notification settings - Fork 2
/
Channel.h
86 lines (68 loc) · 2.27 KB
/
Channel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//@author Jingyu Tong
//@email: jingyutong0806@gmail.com
#ifndef CHANNEL_H_
#define CHANNEL_H_
#include "base/noncopyable.h"
#include "EventLoop.h"
#include <functional>
class EventLoop;
//用于IO事件分发
//一个Channel负责一个fd
class Channel : noncopyable
{
public:
typedef std::function<void()> CallBack;
Channel(EventLoop* loop, int fd);
~Channel();
//注册读写错误回调
void setReadCallback(CallBack rh) {
readHandler_ = std::move(rh);
}
void setWriteCallback(CallBack wh) {
writeHandler_ = std::move(wh);
}
void setErrorCallback(CallBack eh) {
errorHandler_ = std::move(eh);
}
void setCloseCallback(CallBack cb) {
closeHandler_ = std::move(cb);
}
//开关事件,并更新poller等待的相应事件
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(); }
void handleEvent(); //事件处理
void setRevents(int revents)
{ revents_ = revents; }
void setEvents(int events)
{ events_ = events; }
//返回channel负责的描述符
int getFd() const
{ return fd_; }
//返回events
int getEvents() const
{ return events_; }
int getRevents() const
{ return revents_; }
//判断写事件是否已经开启,减少一次update
bool isWriting() const
{return events_ & kWriteEvent; }
//将Channel加入reacotr
void update();
static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;
private:
EventLoop* loop_; //属于的reactor
const int fd_; //负责的fd
int events_; //poll/epoll关心的事件
int revents_; //活动事件
bool event_handleing_;
CallBack readHandler_;
CallBack writeHandler_;
CallBack errorHandler_;
CallBack closeHandler_;
};
#endif