19#include <trantor/exports.h>
35 using EventCallback = std::function<void()>;
55 void setReadCallback(EventCallback &&cb)
57 readCallback_ = std::move(cb);
71 void setWriteCallback(EventCallback &&cb)
73 writeCallback_ = std::move(cb);
85 void setCloseCallback(EventCallback &&cb)
87 closeCallback_ = std::move(cb);
99 void setErrorCallback(EventCallback &&cb)
101 errorCallback_ = std::move(cb);
115 void setEventCallback(EventCallback &&cb)
117 eventCallback_ = std::move(cb);
158 return events_ == kNoneEvent;
167 events_ = kNoneEvent;
193 events_ |= kReadEvent;
203 events_ &= ~kReadEvent;
213 events_ |= kWriteEvent;
223 events_ &= ~kWriteEvent;
235 return events_ & kWriteEvent;
246 return events_ & kReadEvent;
269 void tie(
const std::shared_ptr<void> &obj)
275 static const int kNoneEvent;
276 static const int kReadEvent;
277 static const int kWriteEvent;
281 friend class EpollPoller;
283 friend class PollPoller;
286 void handleEventSafely();
287 int setRevents(
int revt)
297 void setIndex(
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_;
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