diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index d8d80b28b..9cc2b0cfa 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -50,14 +50,21 @@ void KqueueEvent::AddEvent(uint64_t id, int fd, int mask) { } void KqueueEvent::DelEvent(int fd) { - struct kevent change; - EV_SET(&change, fd, EVENT_READ, EV_DELETE, 0, 0, nullptr); - if (kevent(EvFd(), &change, 1, nullptr, 0, nullptr) == -1) { - ERROR("KqueueEvent Del read Event EvFd:{},fd:{}, kevent error:{}", EvFd(), fd, errno); + if (mode_ & EVENT_MODE_READ) { + struct kevent change; + EV_SET(&change, fd, EVENT_READ, EV_DELETE, 0, 0, nullptr); + if (kevent(EvFd(), &change, 1, nullptr, 0, nullptr) == -1) { + ERROR("KqueueEvent Del read Event EvFd:{},fd:{}, kevent error:{}", EvFd(), fd, errno); + } } - EV_SET(&change, fd, EVENT_WRITE, EV_DELETE, 0, 0, nullptr); - if (kevent(EvFd(), &change, 1, nullptr, 0, nullptr) == -1) { - ERROR("KqueueEvent Del write Event EvFd:{},fd:{}, kevent error:{}", EvFd(), fd, errno); + if (mode_ & EVENT_MODE_WRITE) { + struct kevent change; + EV_SET(&change, fd, EVENT_WRITE, EV_DELETE, 0, 0, nullptr); + if (kevent(EvFd(), &change, 1, nullptr, 0, nullptr) == -1) { + if (errno != ENOENT) { // If the event does not exist, it will return ENOENT + ERROR("KqueueEvent Del write Event EvFd:{},fd:{}, kevent error:{}", EvFd(), fd, errno); + } + } } } @@ -191,18 +198,26 @@ void KqueueEvent::DoWrite(const struct kevent &event, const std::shared_ptr(event.udata), conn->fd_); -# ifdef HAVE_32BIT +# ifdef HAVE_64BIT + auto connId = reinterpret_cast(event.udata); +# else + auto _connId = reinterpret_cast(event.udata); + uint64_t connId = *_connId; delete event.udata; # endif + DelWriteEvent(connId, conn->fd_); } } void KqueueEvent::DoError(const struct kevent &event, std::string &&err) { - onClose_(reinterpret_cast(event.udata), std::move(err)); -# ifdef HAVE_32BIT +# ifdef HAVE_64BIT + auto connId = reinterpret_cast(event.udata); +# else + auto _connId = reinterpret_cast(event.udata); + uint64_t connId = *_connId; delete event.udata; # endif + onClose_(connId, std::move(err)); } } // namespace net diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index e22e2cffd..32881ab0d 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -51,7 +51,7 @@ class KqueueEvent : public BaseEvent { void DoError(const struct kevent &event, std::string &&err); private: - const int eventsSize = 1020; + const int eventsSize = 1024; }; } // namespace net