-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.h
113 lines (97 loc) · 2.26 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef CHANNEL_H_
#define CHANNEL_H_
#include"base/noncopyable.h"
#include<functional>
#include<memory>
class EventLoop;
class Channel:noncopyable
{
public:
typedef int64_t Timestamp;
typedef std::function<void()> EventCallback;
// typedef std::function<void(Timestamp)> ReadEventCallback;//used to wake up loop
Channel(EventLoop *loop,int fd);
~Channel();
void handleEvent();
//used for init
void setReadCallback(EventCallback cb)
{readCallback_=std::move(cb);}
void setWriteCallback(EventCallback cb)
{writeCallback_=std::move(cb);}
void setCloseCallback(EventCallback cb)
{closeCallback_=std::move(cb);}
void setErrorCallback(EventCallback cb)
{errorCallback_=std::move(cb);};
int fd()const{return fd_;}
int events()const{return events_;}
void set_revents(int revt){revents_=revt;}
bool isNoneEvent()const{return events_==kNoneEvent;}
void enableReading()
{
events_|=kReadEvent;
update();
}
void disableReading()
{
events_&=~kReadEvent;
update();
}
void enableWritinging()
{
events_|=kWriteEvent;
update();
}
void disableWriting()
{
events_&=~kWriteEvent;
update();
}
void disableAll()
{
events_=kNoneEvent;
update();
}
bool isWriting()const
{
return events_&kWriteEvent;
}
bool isReading()const
{
return events_&kReadEvent;
}
int indx()const
{
return index_;
}
void set_index(int idx)
{
index_=idx;
}
EventLoop *ownerLoop()
{
return loop_;
}
void remove();
void tie(const std::shared_ptr<void> &obj);
private:
void update();
void handleEventWithGuard();
static std::string eventsToString(int fd,int ev);
static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;
EventLoop* loop_;
const int fd_;
int events_;
int revents_;
int index_;
std::weak_ptr<void> tie_;
bool tied_;
bool eventHandling_;
bool addedToLoop_;
EventCallback readCallback_;
EventCallback writeCallback_;
EventCallback closeCallback_;
EventCallback errorCallback_;
};
#endif