-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchannel.cpp
More file actions
87 lines (76 loc) · 1.55 KB
/
Copy pathchannel.cpp
File metadata and controls
87 lines (76 loc) · 1.55 KB
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
#include <iostream>
#include <sys/epoll.h>
#include "channel.h"
Channel::Channel() :
fd_(-1)
{
}
Channel::~Channel()
{
}
//设置文件描述符
void Channel::SetFd(int fd)
{
fd_ = fd;
}
//获取文件描述符
int Channel::GetFd() const
{
return fd_;
}
//设置触发事件
void Channel::SetEvents(uint32_t events)
{
events_ = events;
}
//获取触发事件
uint32_t Channel::GetEvents() const
{
return events_;
}
//事件分发处理
void Channel::HandleEvent()
{
if(events_ & EPOLLRDHUP)
{
//对方异常关闭事件
std::cout<< "Event EPOLLRDHUP!" << std::endl;
closehandler_();
} else if(events_ & (EPOLLIN | EPOLLPRI))
{
//读事件,对端有数据或者正常关闭
std::cout<< "Event EPOLLIN!" << std::endl;
readhandler_();
} else if(events_ & EPOLLOUT)
{
//写事件
std::cout << "Event EPOLLOUT!" << std::endl;
writehandler_();
} else
{
//连接错误
std::cout << "Event EPOLLERR!" << std::endl;
errorhandler_();
}
}
//设置读事件回调
void Channel::SetReadHandle(const Callback& cb)
{
//TODO: 提高效率,可以使用move语句,这里暂时还是存在一次拷贝
readhandler_ = cb;
}
//设置写事件回调
void Channel::SetWriteHandle(const Callback& cb)
{
writehandler_ = cb;
}
//设置错误事件回调
void Channel::SetErrorHandle(const Callback& cb)
{
errorhandler_ = cb;
}
//设置关闭事件回调
void Channel::SetCloseHandle(const Callback& cb)
{
closehandler_ = cb;
}