2021-06-17 14:43:29 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Channel.cc
|
|
|
|
* An Tao
|
|
|
|
*
|
|
|
|
* Public header file in trantor lib.
|
|
|
|
*
|
|
|
|
* Copyright 2018, An Tao. All rights reserved.
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the License file.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Channel.h"
|
|
|
|
#include <trantor/net/EventLoop.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "Wepoll.h"
|
|
|
|
#define POLLIN EPOLLIN
|
|
|
|
#define POLLPRI EPOLLPRI
|
|
|
|
#define POLLOUT EPOLLOUT
|
|
|
|
#define POLLHUP EPOLLHUP
|
|
|
|
#define POLLNVAL 0
|
|
|
|
#define POLLERR EPOLLERR
|
|
|
|
#else
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
#include <iostream>
|
2021-06-17 14:53:13 +02:00
|
|
|
namespace trantor {
|
2021-06-17 14:43:29 +02:00
|
|
|
const int Channel::kNoneEvent = 0;
|
|
|
|
|
|
|
|
const int Channel::kReadEvent = POLLIN | POLLPRI;
|
|
|
|
const int Channel::kWriteEvent = POLLOUT;
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
Channel::Channel(EventLoop *loop, int fd) :
|
|
|
|
loop_(loop), fd_(fd), events_(0), revents_(0), index_(-1), tied_(false) {
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
void Channel::remove() {
|
|
|
|
assert(events_ == kNoneEvent);
|
|
|
|
addedToLoop_ = false;
|
|
|
|
loop_->removeChannel(this);
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
void Channel::update() {
|
|
|
|
loop_->updateChannel(this);
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
void Channel::handleEvent() {
|
|
|
|
// LOG_TRACE<<"revents_="<<revents_;
|
|
|
|
if (tied_) {
|
|
|
|
std::shared_ptr<void> guard = tie_.lock();
|
|
|
|
if (guard) {
|
|
|
|
handleEventSafely();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handleEventSafely();
|
|
|
|
}
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
void Channel::handleEventSafely() {
|
|
|
|
if (eventCallback_) {
|
|
|
|
eventCallback_();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) {
|
|
|
|
// LOG_TRACE<<"handle close";
|
|
|
|
if (closeCallback_)
|
|
|
|
closeCallback_();
|
|
|
|
}
|
|
|
|
if (revents_ & (POLLNVAL | POLLERR)) {
|
|
|
|
// LOG_TRACE<<"handle error";
|
|
|
|
if (errorCallback_)
|
|
|
|
errorCallback_();
|
|
|
|
}
|
2021-06-17 14:43:29 +02:00
|
|
|
#ifdef __linux__
|
2021-06-17 14:53:13 +02:00
|
|
|
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
|
2021-06-17 14:43:29 +02:00
|
|
|
#else
|
2021-06-17 14:53:13 +02:00
|
|
|
if (revents_ & (POLLIN | POLLPRI))
|
2021-06-17 14:43:29 +02:00
|
|
|
#endif
|
2021-06-17 14:53:13 +02:00
|
|
|
{
|
|
|
|
// LOG_TRACE<<"handle read";
|
|
|
|
if (readCallback_)
|
|
|
|
readCallback_();
|
|
|
|
}
|
2021-06-17 14:43:29 +02:00
|
|
|
#ifdef _WIN32
|
2021-06-17 14:53:13 +02:00
|
|
|
if ((revents_ & POLLOUT) && !(revents_ & POLLHUP))
|
2021-06-17 14:43:29 +02:00
|
|
|
#else
|
2021-06-17 14:53:13 +02:00
|
|
|
if (revents_ & POLLOUT)
|
2021-06-17 14:43:29 +02:00
|
|
|
#endif
|
2021-06-17 14:53:13 +02:00
|
|
|
{
|
|
|
|
// LOG_TRACE<<"handle write";
|
|
|
|
if (writeCallback_)
|
|
|
|
writeCallback_();
|
|
|
|
}
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
} // namespace trantor
|