-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchannel.h
110 lines (86 loc) · 2.15 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
//
// Created by yangning on 17-11-28.
//
// Descriprion :事件处理器,用于事件处理.通过添加回调函数,在fd上发生事件时,调用相应的回调函数.
//
// Copyright (c) yangning All rights reserved.
//
#ifndef BASE_NET_LIB_CHANNEL_H
#define BASE_NET_LIB_CHANNEL_H
#include <functional>
//#include <zconf.h>
#include <cassert>
#include <sys/poll.h>
namespace net {
class EventLoop;
using EventCallBack=std::function<void()>;
class Channel {
public:
static const int kNonWrite = ~POLLOUT;
static const int kNonRead = ~POLLIN;
static const int kNonEvent = 0;
static const int kWrite = POLLOUT;
static const int kRead = POLLIN;
static const int kError = POLLERR;
Channel(EventLoop* ownEventLoop_, const int fd_);
const net::EventLoop* getOwnLoop() const
{
return ownEventLoop_;
}
void disenableAllEvent();
public:
bool isWriting(){
return (events_ & kWrite) != 0;
}
void setCloseCallBack(const EventCallBack& closeCallBack_);
void setWriteCallBack(const EventCallBack& call_back)
{
writeCallBack_ = call_back;
}
void setReadCallBack(const EventCallBack& call_back)
{
readCallBack_ = call_back;
}
void setErrorCallBack(const EventCallBack& call_back)
{
errorCallBack_ = call_back;
}
void enableReading();
void disenableReading();
void enableWriting();
void disenableWriting();
void enableError();
void setRetEvents(int ret_events)
{
revents_ = static_cast<short >(ret_events);
}
short getEvents() const
{
return events_;
}
int getFd() const
{
return fd_;
}
void handleEvent();
void removeSelf();
public:
void setIsAddInLoop(bool isAddInLoop_);
~Channel()
{
assert(!eventHandling_);
}
private:
EventLoop* ownEventLoop_;
const int fd_;
short events_;
short revents_;
bool eventHandling_;
bool isAddInLoop_;
EventCallBack writeCallBack_;
EventCallBack readCallBack_;
EventCallBack errorCallBack_;
EventCallBack closeCallBack_;
};
}// namespace net
#endif //BASE_NET_LIB_CHANNEL_H