-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.h
91 lines (76 loc) · 2.21 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
#pragma once
#include "Timer.h"
#include <sys/epoll.h>
#include <memory>
class EventLoop;
class HttpData;
class Channel {
private:
typedef std::function<void()> CallBack;
public:
Channel(EventLoop* loop);
Channel(EventLoop* loop, int fd);
~Channel();
int getFd();
void setFd(int fd);
void setHolder(std::shared_ptr<HttpData> holder) { holder_ = holder; }
std::shared_ptr<HttpData> getHolder() {
std::shared_ptr<HttpData> ret(holder_.lock());
return ret;
}
// &&是右值引用,避免拷贝带来的性能开销
void setReadHandler(CallBack&& readHandler) { readHandler_ = readHandler; }
void setWriteHandler(CallBack&& writeHandler) {
writeHandler_ = writeHandler;
}
void setErrorHandler(CallBack&& errorHandler) {
errorHandler_ = errorHandler;
}
void setConnHandler(CallBack&& connHandler) { connHandler_ = connHandler; }
void handleEvents() {
events_ = 0;
if ((revents_ & EPOLLHUP) && !(revents_ & EPOLLIN)) {
events_ = 0;
return;
}
if (revents_ & EPOLLERR) {
if (errorHandler_) errorHandler_();
events_ = 0;
return;
}
if(revents_ & (EPOLLIN | EPOLLPRI | EPOLLRDHUP)) {
handleRead();
}
if (revents_ & EPOLLOUT) {
handleWrite();
}
handleConn();
}
void handleRead();
void handleWrite();
void handleError(int fd, int err_num, std::string short_msg);
void handleConn();
void setRevents(__uint32_t ev) { revents_ = ev; }
void setEvents(__uint32_t ev) { events_ = ev; }
__uint32_t& getEvents() { return events_; }
bool EqualAndUpdateLastEvents() {
bool ret = (lastEvents_ == events_);
lastEvents_ = events_;
return ret;
}
__uint32_t getLastEvents() { return lastEvents_; }
private:
EventLoop* loop_;
int fd_;
__uint32_t events_;
__uint32_t revents_;
__uint32_t lastEvents_;
std::weak_ptr<HttpData> holder_;
int parse_URI();
int parse_Headers();
int analysisRequest();
CallBack readHandler_;
CallBack writeHandler_;
CallBack errorHandler_;
CallBack connHandler_;
};