Skip to content

Commit

Permalink
Merge pull request #80 from smatsuodev/hup-event
Browse files Browse the repository at this point in the history
(E)POLLHUP の通知を受け取る
  • Loading branch information
harsssh authored Jan 4, 2025
2 parents 327e39e + 4a8e681 commit 6ecc8dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/lib/core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void Server::start() {
const Event &ev = events[i];
LOG_DEBUGF("event arrived for fd %d (flags: %x)", ev.getFd(), ev.getTypeFlags());

if (ev.getTypeFlags() & Event::kError) {
if (ev.isError()) {
this->onErrorEvent(ev);
continue;
}
Expand Down Expand Up @@ -69,8 +69,7 @@ void Server::onHandlerError(const Context &ctx, const error::AppError err) {
}

void Server::onErrorEvent(const Event &event) {
if (!(event.getTypeFlags() & Event::kError)) {
// kError 以外は無視
if (!event.isError()) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/event/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ int Event::getFd() const {
uint32_t Event::getTypeFlags() const {
return typeFlags_;
}

bool Event::isError() const {
return typeFlags_ & (kError | kHangUp);
}
2 changes: 2 additions & 0 deletions src/lib/event/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Event {
kRead = 1,
kWrite = 1 << 1,
kError = 1 << 2, // register しなくても発生する. EPOLLERR に相当
kHangUp = 1 << 3, // register しなくても発生する. EPOLLHUP に相当
};

Event();
Expand All @@ -26,6 +27,7 @@ class Event {

int getFd() const;
uint32_t getTypeFlags() const;
bool isError() const;

private:
int fd_;
Expand Down
20 changes: 16 additions & 4 deletions src/lib/event/event_notifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ EpollEventNotifier::WaitEventsResult EpollEventNotifier::waitEvents() {
for (int i = 0; i < numEvents; i++) {
LOG_DEBUGF("epoll events: fd=%d, events=%d", evs[i].data.fd, evs[i].events);
const uint32_t flags = EpollEventNotifier::toEventTypeFlags(evs[i].events);
// イベント登録時のフラグと一致するイベントのみ返す
if (flags & registeredEvents_[evs[i].data.fd].getTypeFlags()) {
// エラー or イベント登録時のフラグと一致するイベントのみ返す
if (flags & (registeredEvents_[evs[i].data.fd].getTypeFlags() | EPOLLERR | EPOLLHUP)) {
events.push_back(Event(evs[i].data.fd, flags));
}
}
Expand All @@ -87,6 +87,9 @@ uint32_t EpollEventNotifier::toEventTypeFlags(const uint32_t epollEvents) {
if (epollEvents & EPOLLERR) {
flags |= Event::kError;
}
if (epollEvents & EPOLLHUP) {
flags |= Event::kHangUp;
}
return flags;
}

Expand All @@ -102,6 +105,9 @@ uint32_t EpollEventNotifier::toEpollEvents(const Event &event) {
if (flags & Event::kError) {
events |= EPOLLERR;
}
if (flags & Event::kHangUp) {
events |= EPOLLHUP;
}
return events;
}

Expand Down Expand Up @@ -149,8 +155,8 @@ IEventNotifier::WaitEventsResult PollEventNotifier::waitEvents() {
continue;
}

// イベント登録時のフラグと一致するイベントのみ返す
if (flags & registeredEvents_[pfd.fd].getTypeFlags()) {
// エラー or イベント登録時のフラグと一致するイベントのみ返す
if (flags & (registeredEvents_[pfd.fd].getTypeFlags() | POLLERR | POLLHUP)) {
events.push_back(Event(pfd.fd, flags));
}
}
Expand All @@ -170,6 +176,9 @@ short PollEventNotifier::toPollEvents(const Event &event) {
if (flags & Event::kError) {
events |= POLLERR;
}
if (flags & Event::kHangUp) {
events |= POLLHUP;
}
return events;
}

Expand All @@ -184,6 +193,9 @@ uint32_t PollEventNotifier::toEventTypeFlags(const short pollEvents) {
if (pollEvents & POLLERR) {
flags |= Event::kError;
}
if (pollEvents & POLLHUP) {
flags |= Event::kHangUp;
}
return flags;
}

Expand Down

0 comments on commit 6ecc8dd

Please sign in to comment.