rcpp_framework/core/net/channel.cpp

114 lines
3.2 KiB
C++
Raw Normal View History

// This file is originally from Trantor - Channel.cc
// Copyright (c) 2016-2021, Tao An. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Tao An nor the names of other contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2021-06-17 14:43:29 +02:00
2022-02-10 01:24:36 +01:00
#include "channel.h"
#include "core/loops/event_loop.h"
2022-02-10 01:24:36 +01:00
2021-06-17 14:43:29 +02:00
#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>
2022-02-10 12:07:47 +01:00
2021-06-17 14:43:29 +02:00
const int Channel::kNoneEvent = 0;
const int Channel::kReadEvent = POLLIN | POLLPRI;
const int Channel::kWriteEvent = POLLOUT;
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
}
void Channel::remove() {
assert(events_ == kNoneEvent);
addedToLoop_ = false;
loop_->removeChannel(this);
2021-06-17 14:43:29 +02:00
}
void Channel::update() {
loop_->updateChannel(this);
2021-06-17 14:43:29 +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
}
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__
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
2021-06-17 14:43:29 +02:00
#else
if (revents_ & (POLLIN | POLLPRI))
2021-06-17 14:43:29 +02:00
#endif
{
// LOG_TRACE<<"handle read";
if (readCallback_)
readCallback_();
}
2021-06-17 14:43:29 +02:00
#ifdef _WIN32
if ((revents_ & POLLOUT) && !(revents_ & POLLHUP))
2021-06-17 14:43:29 +02:00
#else
if (revents_ & POLLOUT)
2021-06-17 14:43:29 +02:00
#endif
{
// LOG_TRACE<<"handle write";
if (writeCallback_)
writeCallback_();
}
2021-06-17 14:43:29 +02:00
}