Loading src/CMakeLists.txt +1 −0 Changes for src/CMakeLists.txt: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -76,6 +76,7 @@ set(headers crypto/tls.h crypto/curve25519.h utils/certgen.h threadpool.h ) add_library(netplus SHARED ${netplussrc} ${headers}) Loading src/connection.h +37 −0 Changes for src/connection.h: 37 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -191,6 +191,14 @@ namespace netplus { time_t lasteventime; // Epoll (or equivalent) fd this connection's primary socket is // registered under. Set once at accept time (ConnectEventHandler). // Lets code outside the event loop (e.g. a background worker // finishing async work for this connection) force a re-poll via // requestWritablePoll() below without needing its own reference // to the owning poll instance. int ownerPollFd = -1; // Proactively write out whatever is currently queued in SendData, // instead of waiting for the event loop's post-RequestEvent drain. // Safe to call from within a RequestEvent handler on the same Loading @@ -203,4 +211,33 @@ namespace netplus { con(); friend class poll; }; // Look up a live connection by its primary socket fd. Safe to call // from any thread (guarded internally by the same lock the event // loop uses to track connections). Returns nullptr if the fd isn't // (or is no longer) a tracked connection. std::shared_ptr<con> lookupConnection(int fd); // Force a connection's fd to be re-polled for EPOLLOUT (or the // platform equivalent), even if it is otherwise fully idle. For use // by code running outside the event loop (e.g. a background worker // that just finished async work for this connection and queued data // into its SendData) that needs the connection's socket to become // writable-pollable again without waiting for unrelated inbound // traffic. // // PRECONDITION: caller holds c.event_mutex. That's what makes this // safe to call from an arbitrary thread: whoever holds event_mutex // is already the sole authority over this fd's next poll mask (the // same invariant the event loop's own rearm-on-scope-exit guards // rely on), so there is no other thread that could be concurrently // deciding a different mask for the same fd. // // Implemented per event backend (epoll/kqueue). On backends where // this isn't yet supported (select-based poll.cpp, iocp.cpp) this // is a documented no-op — no regression for those, since the // feature that relies on it is new and opt-in, but a stream parked // waiting on this wakeup on those backends only resumes once some // other, unrelated event touches the connection. void requestWritablePoll(con &c); }; src/event/epoll.cpp +22 −0 Changes for src/event/epoll.cpp: 22 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -260,6 +260,7 @@ namespace netplus { std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(fd, ccon); } ccon->ownerPollFd = _pollFD; // Serialize connection setup with I/O for this fd { Loading Loading @@ -998,4 +999,25 @@ namespace netplus { return r > 0 && (out.events & (EPOLLHUP | EPOLLERR)); } std::shared_ptr<con> lookupConnection(int fd) { std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return nullptr; return it->second; } void requestWritablePoll(con &c) { // Caller must hold c.event_mutex (see declaration in connection.h) — // that's what makes calling epoll_ctl from an arbitrary thread safe // here, same invariant EpollArmGuard's own rearm relies on. if (!c.slots[0].csock || c.ownerPollFd < 0) return; int fd = c.slots[0].csock->fd(); if (fd < 0) return; epoll_event ev{}; ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT; ev.data.fd = fd; if (epoll_ctl(c.ownerPollFd, EPOLL_CTL_MOD, fd, &ev) < 0 && errno == ENOENT) epoll_ctl(c.ownerPollFd, EPOLL_CTL_ADD, fd, &ev); } } // namespace netplus src/event/iocp.cpp +11 −0 Changes for src/event/iocp.cpp: 11 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -936,4 +936,15 @@ namespace netplus { return false; } // IOCP tracks connections per-EventState (sockToCon above), keyed by // SOCKET, with no global fd-keyed registry across instances the way // epoll/kqueue's CONNECTIONS map provides. Not implemented for this // backend yet: callers relying on this (the H2 background-dispatch // offload feature, new and opt-in) will simply see a null connection // and drop the response as if the connection had already been torn // down — not a crash, not silent corruption, just the wakeup this // feature relies on not being available on IOCP yet. std::shared_ptr<con> lookupConnection(int /*fd*/) { return nullptr; } void requestWritablePoll(con &) {} } // namespace netplus src/event/kqueue.cpp +26 −0 Changes for src/event/kqueue.cpp: 26 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -391,6 +391,7 @@ namespace netplus { std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(fd, ccon); } ccon->ownerPollFd = _kqfd; // Add to kqueue (EV_ONESHOT with batched re-arming) _batch.add_change(fd, EVFILT_READ, EV_ADD | EV_ONESHOT); Loading Loading @@ -1066,3 +1067,28 @@ bool netplus::socketwait::isDisconnected(socket &sock) { if (r > 0 && (out.flags & EV_EOF)) return true; return false; } namespace netplus { std::shared_ptr<con> lookupConnection(int fd) { std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return nullptr; return it->second; } void requestWritablePoll(con &c) { // Caller must hold c.event_mutex (see declaration in connection.h). // Bypasses the per-poll-instance change batch deliberately — this // may run from a thread that has no such instance in scope; a // direct one-shot kevent() call is safe to issue from any thread. if (!c.slots[0].csock || c.ownerPollFd < 0) return; int fd = c.slots[0].csock->fd(); if (fd < 0) return; struct kevent changes[2]; EV_SET(&changes[0], fd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, nullptr); EV_SET(&changes[1], fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, nullptr); kevent(c.ownerPollFd, changes, 2, nullptr, 0, nullptr); } } // namespace netplus Loading
src/CMakeLists.txt +1 −0 Changes for src/CMakeLists.txt: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -76,6 +76,7 @@ set(headers crypto/tls.h crypto/curve25519.h utils/certgen.h threadpool.h ) add_library(netplus SHARED ${netplussrc} ${headers}) Loading
src/connection.h +37 −0 Changes for src/connection.h: 37 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -191,6 +191,14 @@ namespace netplus { time_t lasteventime; // Epoll (or equivalent) fd this connection's primary socket is // registered under. Set once at accept time (ConnectEventHandler). // Lets code outside the event loop (e.g. a background worker // finishing async work for this connection) force a re-poll via // requestWritablePoll() below without needing its own reference // to the owning poll instance. int ownerPollFd = -1; // Proactively write out whatever is currently queued in SendData, // instead of waiting for the event loop's post-RequestEvent drain. // Safe to call from within a RequestEvent handler on the same Loading @@ -203,4 +211,33 @@ namespace netplus { con(); friend class poll; }; // Look up a live connection by its primary socket fd. Safe to call // from any thread (guarded internally by the same lock the event // loop uses to track connections). Returns nullptr if the fd isn't // (or is no longer) a tracked connection. std::shared_ptr<con> lookupConnection(int fd); // Force a connection's fd to be re-polled for EPOLLOUT (or the // platform equivalent), even if it is otherwise fully idle. For use // by code running outside the event loop (e.g. a background worker // that just finished async work for this connection and queued data // into its SendData) that needs the connection's socket to become // writable-pollable again without waiting for unrelated inbound // traffic. // // PRECONDITION: caller holds c.event_mutex. That's what makes this // safe to call from an arbitrary thread: whoever holds event_mutex // is already the sole authority over this fd's next poll mask (the // same invariant the event loop's own rearm-on-scope-exit guards // rely on), so there is no other thread that could be concurrently // deciding a different mask for the same fd. // // Implemented per event backend (epoll/kqueue). On backends where // this isn't yet supported (select-based poll.cpp, iocp.cpp) this // is a documented no-op — no regression for those, since the // feature that relies on it is new and opt-in, but a stream parked // waiting on this wakeup on those backends only resumes once some // other, unrelated event touches the connection. void requestWritablePoll(con &c); };
src/event/epoll.cpp +22 −0 Changes for src/event/epoll.cpp: 22 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -260,6 +260,7 @@ namespace netplus { std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(fd, ccon); } ccon->ownerPollFd = _pollFD; // Serialize connection setup with I/O for this fd { Loading Loading @@ -998,4 +999,25 @@ namespace netplus { return r > 0 && (out.events & (EPOLLHUP | EPOLLERR)); } std::shared_ptr<con> lookupConnection(int fd) { std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return nullptr; return it->second; } void requestWritablePoll(con &c) { // Caller must hold c.event_mutex (see declaration in connection.h) — // that's what makes calling epoll_ctl from an arbitrary thread safe // here, same invariant EpollArmGuard's own rearm relies on. if (!c.slots[0].csock || c.ownerPollFd < 0) return; int fd = c.slots[0].csock->fd(); if (fd < 0) return; epoll_event ev{}; ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT; ev.data.fd = fd; if (epoll_ctl(c.ownerPollFd, EPOLL_CTL_MOD, fd, &ev) < 0 && errno == ENOENT) epoll_ctl(c.ownerPollFd, EPOLL_CTL_ADD, fd, &ev); } } // namespace netplus
src/event/iocp.cpp +11 −0 Changes for src/event/iocp.cpp: 11 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -936,4 +936,15 @@ namespace netplus { return false; } // IOCP tracks connections per-EventState (sockToCon above), keyed by // SOCKET, with no global fd-keyed registry across instances the way // epoll/kqueue's CONNECTIONS map provides. Not implemented for this // backend yet: callers relying on this (the H2 background-dispatch // offload feature, new and opt-in) will simply see a null connection // and drop the response as if the connection had already been torn // down — not a crash, not silent corruption, just the wakeup this // feature relies on not being available on IOCP yet. std::shared_ptr<con> lookupConnection(int /*fd*/) { return nullptr; } void requestWritablePoll(con &) {} } // namespace netplus
src/event/kqueue.cpp +26 −0 Changes for src/event/kqueue.cpp: 26 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -391,6 +391,7 @@ namespace netplus { std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(fd, ccon); } ccon->ownerPollFd = _kqfd; // Add to kqueue (EV_ONESHOT with batched re-arming) _batch.add_change(fd, EVFILT_READ, EV_ADD | EV_ONESHOT); Loading Loading @@ -1066,3 +1067,28 @@ bool netplus::socketwait::isDisconnected(socket &sock) { if (r > 0 && (out.flags & EV_EOF)) return true; return false; } namespace netplus { std::shared_ptr<con> lookupConnection(int fd) { std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return nullptr; return it->second; } void requestWritablePoll(con &c) { // Caller must hold c.event_mutex (see declaration in connection.h). // Bypasses the per-poll-instance change batch deliberately — this // may run from a thread that has no such instance in scope; a // direct one-shot kevent() call is safe to issue from any thread. if (!c.slots[0].csock || c.ownerPollFd < 0) return; int fd = c.slots[0].csock->fd(); if (fd < 0) return; struct kevent changes[2]; EV_SET(&changes[0], fd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, nullptr); EV_SET(&changes[1], fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, nullptr); kevent(c.ownerPollFd, changes, 2, nullptr, 0, nullptr); } } // namespace netplus