-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.h
More file actions
125 lines (99 loc) · 3.37 KB
/
Copy pathChannel.h
File metadata and controls
125 lines (99 loc) · 3.37 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
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
114
115
116
117
118
119
120
121
122
123
124
125
#if !defined(MUDUO_CHANNEL_H)
#define MUDUO_CHANNEL_H
#include <muduo/base/allocator/Allocatable.h>
#include <muduo/EventLoop.h>
#include <functional>
#include <memory>
#include <chrono>
#include <cassert>
namespace muduo {
/**
* This class doesn't own the file descriptor.
* The file descriptor could be a socket \ event fd \ timer fd \ signal fd
* Channel is responsible to register and handle IO events
*/
#ifdef MUDUO_USE_MEMPOOL
class Channel : public base::detail::Allocatable {
#else
class Channel {
#endif
Channel(const Channel&) = delete; // non-copyable & non-moveable
static const int kNoneEvent;
static const int kReadEvent;
static const int kWriteEvent;
public:
Channel(EventLoop* owner, int fd);
using EventCallback_t = std::function<void()>;
using ReceiveTimePoint_t = EventLoop::ReceiveTimePoint_t;
using RDCallback_t = std::function<void(const ReceiveTimePoint_t&)>;
~Channel() {
assert(!eventHandling_);
/// TODO: assert(!addedToLoop_);
};
/**
* 将当前Channel绑定给由shared_ptr管理的所有者对象
* 防止所有者对象在Poller::handleEvent中被销毁
*/
void Tie(const std::shared_ptr<void>& obj) {
tie_ = obj;
tied_ = true;
}
void SetReadCallback(const RDCallback_t& cb) { readCb_ = cb; }
void SetWriteCallback(const EventCallback_t& cb) { writeCb_ = cb; }
void SetCloseCallback(const EventCallback_t& cb) { closeCb_ = cb; }
void SetErrorCallback(const EventCallback_t& cb) { errorCb_ = cb; }
void EnableReading() { events_ |= kReadEvent; Update(); }
void enableWriting() { events_ |= kWriteEvent; Update(); }
void disableWriting() { events_ &= ~kWriteEvent; Update(); }
void disableAllEvents() { events_ = kNoneEvent; Update(); }
bool IsNoneEvent() const { return events_ == kNoneEvent; }
bool IsWriting() const { return events_ & kWriteEvent; }
void NotLogHup() { logHup_ = false; }
/**
* handle incoming events
*/
void HandleEvents(ReceiveTimePoint_t receiveTime);
/**
* return the loop instance whos own the current channel
*/
EventLoop* Owner() { return owner_; }
const EventLoop* Owner() const { return owner_; }
int FileDescriptor() const { return fd_; }
int CurrentEvent() const { return events_; }
int Index() const { return index_; }
/**
* used by pollers
*/
void Set_REvent(const int revt) { revents_ = revt; }
void SetIndex(const std::size_t i) { index_ = i; }
void Remove();
/**
* for debug
*/
std::string EventTostring() const {
return EventToString(fd_, events_);
}
std::string REventsToString() const {
return EventToString(fd_, revents_);
}
private:
void Update();
void HandleEventsWithGuard(ReceiveTimePoint_t receiveTime);
std::string EventToString(int fd, int ev) const;
private:
EventLoop* owner_; // 聚合关系 (1 channel only belong to 1 loop instance)
const int fd_;
int events_; // interested I/O events
bool logHup_; // for POLLHUP
bool eventHandling_;
int revents_; // poll/epoll返回的事件
int index_;
bool tied_;
std::weak_ptr<void> tie_;
RDCallback_t readCb_;
EventCallback_t writeCb_;
EventCallback_t closeCb_;
EventCallback_t errorCb_;
};
} // namespace muduo
#endif // MUDUO_CHANNEL_H