trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
Channel.h
Go to the documentation of this file.
1
14
15#pragma once
16
19#include <trantor/exports.h>
20#include <functional>
21#include <assert.h>
22#include <memory>
23namespace trantor
24{
25class EventLoop;
32class TRANTOR_EXPORT Channel : NonCopyable
33{
34 public:
35 using EventCallback = std::function<void()>;
42 Channel(EventLoop *loop, int fd);
43
51 void setReadCallback(const EventCallback &cb)
52 {
53 readCallback_ = cb;
54 };
55 void setReadCallback(EventCallback &&cb)
56 {
57 readCallback_ = std::move(cb);
58 }
59
67 void setWriteCallback(const EventCallback &cb)
68 {
69 writeCallback_ = cb;
70 };
71 void setWriteCallback(EventCallback &&cb)
72 {
73 writeCallback_ = std::move(cb);
74 }
75
81 void setCloseCallback(const EventCallback &cb)
82 {
83 closeCallback_ = cb;
84 }
85 void setCloseCallback(EventCallback &&cb)
86 {
87 closeCallback_ = std::move(cb);
88 }
89
95 void setErrorCallback(const EventCallback &cb)
96 {
97 errorCallback_ = cb;
98 }
99 void setErrorCallback(EventCallback &&cb)
100 {
101 errorCallback_ = std::move(cb);
102 }
103
111 void setEventCallback(const EventCallback &cb)
112 {
113 eventCallback_ = cb;
114 }
115 void setEventCallback(EventCallback &&cb)
116 {
117 eventCallback_ = std::move(cb);
118 }
119
125 int fd() const
126 {
127 return fd_;
128 }
129
135 int events() const
136 {
137 return events_;
138 }
139
145 int revents() const
146 {
147 return revents_;
148 }
149
156 bool isNoneEvent() const
157 {
158 return events_ == kNoneEvent;
159 };
160
166 {
167 events_ = kNoneEvent;
168 update();
169 }
170
175 void remove();
176
182 EventLoop *ownerLoop()
183 {
184 return loop_;
185 };
186
192 {
193 events_ |= kReadEvent;
194 update();
195 }
196
202 {
203 events_ &= ~kReadEvent;
204 update();
205 }
206
212 {
213 events_ |= kWriteEvent;
214 update();
215 }
216
222 {
223 events_ &= ~kWriteEvent;
224 update();
225 }
226
233 bool isWriting() const
234 {
235 return events_ & kWriteEvent;
236 }
237
244 bool isReading() const
245 {
246 return events_ & kReadEvent;
247 }
248
255 {
256 events_ = events;
257 update();
258 }
259
269 void tie(const std::shared_ptr<void> &obj)
270 {
271 tie_ = obj;
272 tied_ = true;
273 }
274
275 static const int kNoneEvent;
276 static const int kReadEvent;
277 static const int kWriteEvent;
278
279 private:
280 friend class EventLoop;
281 friend class EpollPoller;
282 friend class KQueue;
283 friend class PollPoller;
284 void update();
285 void handleEvent();
286 void handleEventSafely();
287 int setRevents(int revt)
288 {
289 // LOG_TRACE<<"revents="<<revt;
290 revents_ = revt;
291 return revt;
292 };
293 int index()
294 {
295 return index_;
296 };
297 void setIndex(int index)
298 {
299 index_ = index;
300 };
301 EventLoop *loop_;
302 const int fd_;
303 int events_;
304 int revents_;
305 int index_;
306 bool addedToLoop_{false};
307 EventCallback readCallback_;
308 EventCallback writeCallback_;
309 EventCallback errorCallback_;
310 EventCallback closeCallback_;
311 EventCallback eventCallback_;
312 std::weak_ptr<void> tie_;
313 bool tied_;
314};
315} // namespace trantor
void setEventCallback(const EventCallback &cb)
Set the event callback.
Definition Channel.h:111
void setWriteCallback(const EventCallback &cb)
Set the write callback.
Definition Channel.h:67
void disableReading()
Disable the read event on the socket.
Definition Channel.h:201
bool isNoneEvent() const
Check whether there is no event enabled on the socket.
Definition Channel.h:156
void disableAll()
Disable all events on the socket.
Definition Channel.h:165
int fd() const
Return the fd of the socket.
Definition Channel.h:125
void setCloseCallback(const EventCallback &cb)
Set the close callback.
Definition Channel.h:81
void enableWriting()
Enable the write event on the socket.
Definition Channel.h:211
void disableWriting()
Disable the write event on the socket.
Definition Channel.h:221
Channel(EventLoop *loop, int fd)
Construct a new Channel instance.
void updateEvents(int events)
Set and update the events enabled.
Definition Channel.h:254
EventLoop * ownerLoop()
Return the event loop.
Definition Channel.h:182
int events() const
Return the events enabled on the socket.
Definition Channel.h:135
bool isWriting() const
Check whether the write event is enabled on the socket.
Definition Channel.h:233
void remove()
Remove the socket from the poller in the event loop.
int revents() const
Return the events that occurred on the socket.
Definition Channel.h:145
void setReadCallback(const EventCallback &cb)
Set the read callback.
Definition Channel.h:51
void setErrorCallback(const EventCallback &cb)
Set the error callback.
Definition Channel.h:95
void enableReading()
Enable the read event on the socket.
Definition Channel.h:191
bool isReading() const
Check whether the read event is enabled on the socket.
Definition Channel.h:244
void tie(const std::shared_ptr< void > &obj)
This method is used to ensure that the callback owner is valid when a callback is called.
Definition Channel.h:269
As the name implies, this class represents an event loop that runs in a particular thread....
Definition EventLoop.h:56
Definition EventLoop.h:34