Loading src/event/epoll.cpp +52 −0 Changes for src/event/epoll.cpp: 52 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -807,4 +807,56 @@ namespace netplus { } } // ================================================================ // socketwait – epoll-based implementation // ================================================================ socketwait::socketwait() { _handle = ::epoll_create1(EPOLL_CLOEXEC); if (_handle < 0) { NetException e; e[NetException::Error] << "socketwait: epoll_create1 failed: " << strerror(errno); throw e; } } socketwait::~socketwait() { if (_handle >= 0) ::close(_handle); } bool socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct epoll_event ev{}; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(_handle, EPOLL_CTL_ADD, fd, &ev); struct epoll_event out; int r = ::epoll_wait(_handle, &out, 1, timeout_ms); epoll_ctl(_handle, EPOLL_CTL_DEL, fd, nullptr); return r > 0 && (out.events & (EPOLLIN | EPOLLHUP | EPOLLERR)); } bool socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct epoll_event ev{}; ev.events = EPOLLOUT; ev.data.fd = fd; epoll_ctl(_handle, EPOLL_CTL_ADD, fd, &ev); struct epoll_event out; int r = ::epoll_wait(_handle, &out, 1, timeout_ms); epoll_ctl(_handle, EPOLL_CTL_DEL, fd, nullptr); return r > 0 && (out.events & EPOLLOUT); } bool socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); struct epoll_event ev{}; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(_handle, EPOLL_CTL_ADD, fd, &ev); struct epoll_event out; int r = ::epoll_wait(_handle, &out, 1, 0); epoll_ctl(_handle, EPOLL_CTL_DEL, fd, nullptr); return r > 0 && (out.events & (EPOLLHUP | EPOLLERR)); } } // namespace netplus src/event/iocp.cpp +69 −0 Changes for src/event/iocp.cpp: 69 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -639,4 +639,73 @@ namespace netplus { st->workers.clear(); } // ================================================================ // socketwait – Windows implementation using select() // ================================================================ socketwait::socketwait() : _handle(-1) {} socketwait::~socketwait() {} bool socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, &rset, nullptr, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitRead select() failed: " << WSAGetLastError(); throw e; } return r > 0; } bool socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set wset, eset; FD_ZERO(&wset); FD_SET(fd, &wset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, nullptr, &wset, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitWrite select() failed: " << WSAGetLastError(); throw e; } return r > 0 && FD_ISSET(fd, &wset); } bool socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return true; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv{0, 0}; int r = ::select(0, &rset, nullptr, &eset, &tv); if (r == SOCKET_ERROR) return true; if (r == 0) return false; if (FD_ISSET(fd, &eset)) return true; if (FD_ISSET(fd, &rset)) { char tmp; int n = ::recv(fd, &tmp, 1, MSG_PEEK); if (n == 0 || n == SOCKET_ERROR) return true; } return false; } } // namespace netplus src/event/kqueue.cpp +59 −0 Changes for src/event/kqueue.cpp: 59 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -910,3 +910,62 @@ MAINWORKERLOOP: goto MAINWORKERLOOP; } } // ================================================================ // socketwait – kqueue-based implementation // ================================================================ netplus::socketwait::socketwait() { _handle = ::kqueue(); if (_handle < 0) { NetException e; e[NetException::Error] << "socketwait: kqueue() failed: " << strerror(errno); throw e; } } netplus::socketwait::~socketwait() { if (_handle >= 0) ::close(_handle); } bool netplus::socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct kevent change; EV_SET(&change, fd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, nullptr); struct kevent out; struct timespec ts_buf; struct timespec* tsp = nullptr; if (timeout_ms >= 0) { ts_buf.tv_sec = timeout_ms / 1000; ts_buf.tv_nsec = (timeout_ms % 1000) * 1000000L; tsp = &ts_buf; } int r = ::kevent(_handle, &change, 1, &out, 1, tsp); return r > 0; } bool netplus::socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct kevent change; EV_SET(&change, fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, nullptr); struct kevent out; struct timespec ts_buf; struct timespec* tsp = nullptr; if (timeout_ms >= 0) { ts_buf.tv_sec = timeout_ms / 1000; ts_buf.tv_nsec = (timeout_ms % 1000) * 1000000L; tsp = &ts_buf; } int r = ::kevent(_handle, &change, 1, &out, 1, tsp); return r > 0; } bool netplus::socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); struct kevent change; EV_SET(&change, fd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, nullptr); struct kevent out; struct timespec ts = {0, 0}; int r = ::kevent(_handle, &change, 1, &out, 1, &ts); if (r > 0 && (out.flags & EV_EOF)) return true; return false; } src/event/poll.cpp +118 −0 Changes for src/event/poll.cpp: 118 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -865,4 +865,122 @@ namespace netplus { } } // ================================================================ // socketwait – select/poll-based fallback implementation // ================================================================ #ifdef _WIN32 socketwait::socketwait() : _handle(-1) {} socketwait::~socketwait() {} bool socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, &rset, nullptr, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitRead select() failed: " << WSAGetLastError(); throw e; } return r > 0; } bool socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set wset, eset; FD_ZERO(&wset); FD_SET(fd, &wset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, nullptr, &wset, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitWrite select() failed: " << WSAGetLastError(); throw e; } return r > 0 && FD_ISSET(fd, &wset); } bool socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return true; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv{0, 0}; int r = ::select(0, &rset, nullptr, &eset, &tv); if (r == SOCKET_ERROR) return true; if (r == 0) return false; if (FD_ISSET(fd, &eset)) return true; if (FD_ISSET(fd, &rset)) { char tmp; int n = ::recv(fd, &tmp, 1, MSG_PEEK); if (n == 0 || n == SOCKET_ERROR) return true; } return false; } #else socketwait::socketwait() : _handle(-1) {} socketwait::~socketwait() {} bool socketwait::waitRead(socket &sock, int timeout_ms) { struct pollfd pfd{}; pfd.fd = sock.fd(); pfd.events = POLLIN; int r; do { r = ::poll(&pfd, 1, timeout_ms); } while (r < 0 && errno == EINTR); if (r < 0) { NetException e; e[NetException::Error] << "socketwait::waitRead poll() failed: " << strerror(errno); throw e; } return r > 0 && (pfd.revents & (POLLIN | POLLHUP | POLLERR)); } bool socketwait::waitWrite(socket &sock, int timeout_ms) { struct pollfd pfd{}; pfd.fd = sock.fd(); pfd.events = POLLOUT; int r; do { r = ::poll(&pfd, 1, timeout_ms); } while (r < 0 && errno == EINTR); if (r < 0) { NetException e; e[NetException::Error] << "socketwait::waitWrite poll() failed: " << strerror(errno); throw e; } return r > 0 && (pfd.revents & POLLOUT); } bool socketwait::isDisconnected(socket &sock) { struct pollfd pfd{}; pfd.fd = sock.fd(); pfd.events = POLLIN; int r; do { r = ::poll(&pfd, 1, 0); } while (r < 0 && errno == EINTR); if (r < 0) return true; return r > 0 && (pfd.revents & (POLLHUP | POLLERR)); } #endif } // namespace netplus src/eventapi.h +29 −0 Changes for src/eventapi.h: 29 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -38,6 +38,35 @@ namespace netplus { class EventWorker; class pollapi; // =================================================== // Cross-platform socket readiness waiting. // Abstracts epoll (Linux), kqueue (BSD), IOCP (Windows), // select/poll (fallback) behind a simple interface. // Each event backend (event/*.cpp) provides the implementation. // =================================================== class socketwait { public: socketwait(); ~socketwait(); socketwait(const socketwait&) = delete; socketwait& operator=(const socketwait&) = delete; // Wait until socket is readable. // timeout_ms: -1 = infinite, 0 = return immediately, >0 = milliseconds // Returns true if ready, false on timeout. bool waitRead(socket &sock, int timeout_ms = -1); // Wait until socket is writable. bool waitWrite(socket &sock, int timeout_ms = -1); // Check if the peer has disconnected (non-blocking). bool isDisconnected(socket &sock); private: int _handle; // platform-specific (epoll fd, kqueue fd, or -1) }; class eventapi { public: /*HTTP API Events*/ Loading Loading
src/event/epoll.cpp +52 −0 Changes for src/event/epoll.cpp: 52 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -807,4 +807,56 @@ namespace netplus { } } // ================================================================ // socketwait – epoll-based implementation // ================================================================ socketwait::socketwait() { _handle = ::epoll_create1(EPOLL_CLOEXEC); if (_handle < 0) { NetException e; e[NetException::Error] << "socketwait: epoll_create1 failed: " << strerror(errno); throw e; } } socketwait::~socketwait() { if (_handle >= 0) ::close(_handle); } bool socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct epoll_event ev{}; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(_handle, EPOLL_CTL_ADD, fd, &ev); struct epoll_event out; int r = ::epoll_wait(_handle, &out, 1, timeout_ms); epoll_ctl(_handle, EPOLL_CTL_DEL, fd, nullptr); return r > 0 && (out.events & (EPOLLIN | EPOLLHUP | EPOLLERR)); } bool socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct epoll_event ev{}; ev.events = EPOLLOUT; ev.data.fd = fd; epoll_ctl(_handle, EPOLL_CTL_ADD, fd, &ev); struct epoll_event out; int r = ::epoll_wait(_handle, &out, 1, timeout_ms); epoll_ctl(_handle, EPOLL_CTL_DEL, fd, nullptr); return r > 0 && (out.events & EPOLLOUT); } bool socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); struct epoll_event ev{}; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(_handle, EPOLL_CTL_ADD, fd, &ev); struct epoll_event out; int r = ::epoll_wait(_handle, &out, 1, 0); epoll_ctl(_handle, EPOLL_CTL_DEL, fd, nullptr); return r > 0 && (out.events & (EPOLLHUP | EPOLLERR)); } } // namespace netplus
src/event/iocp.cpp +69 −0 Changes for src/event/iocp.cpp: 69 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -639,4 +639,73 @@ namespace netplus { st->workers.clear(); } // ================================================================ // socketwait – Windows implementation using select() // ================================================================ socketwait::socketwait() : _handle(-1) {} socketwait::~socketwait() {} bool socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, &rset, nullptr, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitRead select() failed: " << WSAGetLastError(); throw e; } return r > 0; } bool socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set wset, eset; FD_ZERO(&wset); FD_SET(fd, &wset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, nullptr, &wset, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitWrite select() failed: " << WSAGetLastError(); throw e; } return r > 0 && FD_ISSET(fd, &wset); } bool socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return true; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv{0, 0}; int r = ::select(0, &rset, nullptr, &eset, &tv); if (r == SOCKET_ERROR) return true; if (r == 0) return false; if (FD_ISSET(fd, &eset)) return true; if (FD_ISSET(fd, &rset)) { char tmp; int n = ::recv(fd, &tmp, 1, MSG_PEEK); if (n == 0 || n == SOCKET_ERROR) return true; } return false; } } // namespace netplus
src/event/kqueue.cpp +59 −0 Changes for src/event/kqueue.cpp: 59 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -910,3 +910,62 @@ MAINWORKERLOOP: goto MAINWORKERLOOP; } } // ================================================================ // socketwait – kqueue-based implementation // ================================================================ netplus::socketwait::socketwait() { _handle = ::kqueue(); if (_handle < 0) { NetException e; e[NetException::Error] << "socketwait: kqueue() failed: " << strerror(errno); throw e; } } netplus::socketwait::~socketwait() { if (_handle >= 0) ::close(_handle); } bool netplus::socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct kevent change; EV_SET(&change, fd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, nullptr); struct kevent out; struct timespec ts_buf; struct timespec* tsp = nullptr; if (timeout_ms >= 0) { ts_buf.tv_sec = timeout_ms / 1000; ts_buf.tv_nsec = (timeout_ms % 1000) * 1000000L; tsp = &ts_buf; } int r = ::kevent(_handle, &change, 1, &out, 1, tsp); return r > 0; } bool netplus::socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); struct kevent change; EV_SET(&change, fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, nullptr); struct kevent out; struct timespec ts_buf; struct timespec* tsp = nullptr; if (timeout_ms >= 0) { ts_buf.tv_sec = timeout_ms / 1000; ts_buf.tv_nsec = (timeout_ms % 1000) * 1000000L; tsp = &ts_buf; } int r = ::kevent(_handle, &change, 1, &out, 1, tsp); return r > 0; } bool netplus::socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); struct kevent change; EV_SET(&change, fd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, nullptr); struct kevent out; struct timespec ts = {0, 0}; int r = ::kevent(_handle, &change, 1, &out, 1, &ts); if (r > 0 && (out.flags & EV_EOF)) return true; return false; }
src/event/poll.cpp +118 −0 Changes for src/event/poll.cpp: 118 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -865,4 +865,122 @@ namespace netplus { } } // ================================================================ // socketwait – select/poll-based fallback implementation // ================================================================ #ifdef _WIN32 socketwait::socketwait() : _handle(-1) {} socketwait::~socketwait() {} bool socketwait::waitRead(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, &rset, nullptr, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitRead select() failed: " << WSAGetLastError(); throw e; } return r > 0; } bool socketwait::waitWrite(socket &sock, int timeout_ms) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return false; fd_set wset, eset; FD_ZERO(&wset); FD_SET(fd, &wset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv_buf; timeval* tvp = nullptr; if (timeout_ms >= 0) { tv_buf.tv_sec = timeout_ms / 1000; tv_buf.tv_usec = (timeout_ms % 1000) * 1000; tvp = &tv_buf; } int r = ::select(0, nullptr, &wset, &eset, tvp); if (r == SOCKET_ERROR) { NetException e; e[NetException::Error] << "socketwait::waitWrite select() failed: " << WSAGetLastError(); throw e; } return r > 0 && FD_ISSET(fd, &wset); } bool socketwait::isDisconnected(socket &sock) { SOCKET fd = sock.fd(); if (fd == INVALID_SOCKET) return true; fd_set rset, eset; FD_ZERO(&rset); FD_SET(fd, &rset); FD_ZERO(&eset); FD_SET(fd, &eset); timeval tv{0, 0}; int r = ::select(0, &rset, nullptr, &eset, &tv); if (r == SOCKET_ERROR) return true; if (r == 0) return false; if (FD_ISSET(fd, &eset)) return true; if (FD_ISSET(fd, &rset)) { char tmp; int n = ::recv(fd, &tmp, 1, MSG_PEEK); if (n == 0 || n == SOCKET_ERROR) return true; } return false; } #else socketwait::socketwait() : _handle(-1) {} socketwait::~socketwait() {} bool socketwait::waitRead(socket &sock, int timeout_ms) { struct pollfd pfd{}; pfd.fd = sock.fd(); pfd.events = POLLIN; int r; do { r = ::poll(&pfd, 1, timeout_ms); } while (r < 0 && errno == EINTR); if (r < 0) { NetException e; e[NetException::Error] << "socketwait::waitRead poll() failed: " << strerror(errno); throw e; } return r > 0 && (pfd.revents & (POLLIN | POLLHUP | POLLERR)); } bool socketwait::waitWrite(socket &sock, int timeout_ms) { struct pollfd pfd{}; pfd.fd = sock.fd(); pfd.events = POLLOUT; int r; do { r = ::poll(&pfd, 1, timeout_ms); } while (r < 0 && errno == EINTR); if (r < 0) { NetException e; e[NetException::Error] << "socketwait::waitWrite poll() failed: " << strerror(errno); throw e; } return r > 0 && (pfd.revents & POLLOUT); } bool socketwait::isDisconnected(socket &sock) { struct pollfd pfd{}; pfd.fd = sock.fd(); pfd.events = POLLIN; int r; do { r = ::poll(&pfd, 1, 0); } while (r < 0 && errno == EINTR); if (r < 0) return true; return r > 0 && (pfd.revents & (POLLHUP | POLLERR)); } #endif } // namespace netplus
src/eventapi.h +29 −0 Changes for src/eventapi.h: 29 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -38,6 +38,35 @@ namespace netplus { class EventWorker; class pollapi; // =================================================== // Cross-platform socket readiness waiting. // Abstracts epoll (Linux), kqueue (BSD), IOCP (Windows), // select/poll (fallback) behind a simple interface. // Each event backend (event/*.cpp) provides the implementation. // =================================================== class socketwait { public: socketwait(); ~socketwait(); socketwait(const socketwait&) = delete; socketwait& operator=(const socketwait&) = delete; // Wait until socket is readable. // timeout_ms: -1 = infinite, 0 = return immediately, >0 = milliseconds // Returns true if ready, false on timeout. bool waitRead(socket &sock, int timeout_ms = -1); // Wait until socket is writable. bool waitWrite(socket &sock, int timeout_ms = -1); // Check if the peer has disconnected (non-blocking). bool isDisconnected(socket &sock); private: int _handle; // platform-specific (epoll fd, kqueue fd, or -1) }; class eventapi { public: /*HTTP API Events*/ Loading