Commit 6b90f4ba authored by jan.koester's avatar jan.koester
Browse files

fixes fixes

parent c95542d6
Loading
Loading
Loading
Loading
+3 −0
Changes for src/connection.h: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -166,6 +166,9 @@ namespace netplus {
            condata<char> RecvData;
            condata<char> SendData;

            /*states*/
            size_t SendOff = 0;

            std::mutex event_mutex;
        protected:
            con();
+38 −21
Changes for src/event/epoll.cpp: 38 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -236,92 +236,109 @@ namespace netplus {
            if (!rcon->csock) return;

            try {
                while (true) {
                for (;;) {
                    buffer buf(BLOCKSIZE);

                    // recvData throws NetException::Note on EAGAIN/EWOULDBLOCK
                    size_t rcv = rcon->csock->recvData(buf);

                    if (rcv == 0) {
                        // In *your* API this might mean EAGAIN OR EOF depending on tcp::recvData implementation.
                        // If tcp::recvData is fixed to return 0 on EOF and throw Note on EAGAIN -> treat 0 as EOF.
                        // recv()==0 => peer closed
                        NetException e;
                        e[NetException::Close] << "ReadEventHandler: EOF detected. Closing connection.";
                        e[NetException::Close] << "ReadEventHandler: peer closed connection (EOF)";
                        throw e;
                    }

                    rcon->RecvData.append(buf.data.buf, rcv);
                    rcon->lasteventime = time(nullptr);

                    // Let protocol layer consume as much as it can (HTTP parser / TLS state machine etc.)
                    evconnection->RequestEvent(*rcon, tid, args);

                    if (!rcon->SendData.empty()) {
                    if (rcon->SendOff < rcon->SendData.size()) {
                        setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                        return;
                    }

                    // continue loop: keep draining recv until would-block
                }
            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                    // would-block -> re-arm read (or write if you have pending output)
                    if (!rcon->SendData.empty())
                    // would-block: re-arm read unless we have pending output
                    if (rcon->SendOff < rcon->SendData.size())
                        setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                    else
                        setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                    return;
                }

                // Close/error: you probably want to cleanup connection here instead of throw
                // but keep your current behavior:
                throw;
            }
        }



    void WriteEventHandler(int pos, const int tid, ULONG_PTR args) {
        con* wcon = reinterpret_cast<con*>(_Events[pos].data.ptr);
        if (!wcon || !wcon->csock) return;

        std::lock_guard<std::mutex> lk(wcon->event_mutex);
            if (!_Events[pos].data.ptr || !wcon->csock) return;
        if (!wcon->csock) return;

        try {
            evconnection->ResponseEvent(*wcon, tid, args);

            // keep writing as long as:
            // - app SendData has bytes OR
            // - ssl still has pending encrypted bytes
            for (;;) {
                // 1) If no app bytes left, flush SSL pending record by calling sendData with empty buffer
                if (wcon->SendData.empty()) {
                    setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                    netplus::buffer empty((char*)nullptr, 0); // or however your buffer can represent empty
                    size_t n = wcon->csock->sendData(empty);

                    // If SSL still pending, we must stay in EPOLLOUT
                    if (auto* ss = dynamic_cast<netplus::ssl*>(wcon->csock.get())) {
                        if (ss->hasPendingWrite()) {
                            setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                            return;
                        }
                    }

                while (!wcon->SendData.empty()) {
                    size_t toSend = std::min((size_t)BLOCKSIZE, wcon->SendData.size());
                    // nothing pending anymore -> switch to read
                    setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                    return;
                }

                    buffer out(wcon->SendData.data(), toSend);
                // 2) Normal path: send some app bytes
                size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size());
                netplus::buffer out(wcon->SendData.data(), toSend);

                    size_t sent = wcon->csock->sendData(out);
                size_t consumed = wcon->csock->sendData(out);

                    if (sent == 0) {
                if (consumed == 0) {
                    // would-block or SSL is flushing an internal record
                    setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                    return;
                }

                wcon->SendData.erase(wcon->SendData.begin(),
                                        wcon->SendData.begin() + sent);
                                    wcon->SendData.begin() + consumed);

                wcon->lasteventime = time(nullptr);

                if (wcon->SendData.empty()) {
                    evconnection->ResponseEvent(*wcon, tid, args);
                    // loop continues and will flush SSL pending record
                }
            }

                setpollEvents(pos, EPOLLIN | EPOLLONESHOT);
                return;

        } catch (NetException& e) {
            if (e.getErrorType() == NetException::Note) {
                setpollEvents(pos, EPOLLOUT | EPOLLONESHOT);
                return;
            }
                throw e;
            throw;
        }
    }

+72 −43
Changes for src/event/kqueue.cpp: 72 added lines, 43 removed lines.
Original line number Diff line number Diff line
@@ -264,103 +264,132 @@ void ConnectEventHandler(int pos, const int tid, ULONG_PTR args) {
        }

        void ReadEventHandler(int pos, const int tid, ULONG_PTR args) {
            // Get pointer directly from udata.
            con* rcon = reinterpret_cast<con*>(_Events[pos].udata);
            if (!rcon) return;

            if (!rcon) { return; }

            // Acquire connection-specific lock immediately
            std::lock_guard<std::mutex> lock_guard(rcon->event_mutex);

            // Double check pointer validity under lock
            if (!rcon->csock) { return; }
            std::lock_guard<std::mutex> lk(rcon->event_mutex);
            if (!rcon->csock) return;

            try {
                for (;;) {
                    buffer buf(BLOCKSIZE);

                    size_t rcvsize = 0;
                    try {

                size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE);

                rcon->lasteventime = time(nullptr);

                if (rcvsize <= 0) {
                    NetException e;
                    if (rcvsize == 0) {
                        e[NetException::Note] << "ReadEventHandler: EOF detected. Closing connection.";
                        rcvsize = rcon->csock->recvData(buf, 0);   // FLAGS MUST BE 0 (or real flags), NOT BLOCKSIZE
                    } catch (NetException& e) {
                        if (e.getErrorType() == NetException::Note) {
                            // would-block -> re-arm based on whether we need to write
                            if (!rcon->SendData.empty() || rcon->csock->hasPendingWrite()) {
                                setpollEvents(rcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                            } else {
                        e[NetException::Note] << "ReadEventHandler: Fatal socket error or eof detected. Closing connection.";
                                setpollEvents(rcon, EVFILT_READ, EV_ADD | EV_ONESHOT);
                            }
                            return;
                        }
                        throw;
                    }

                    if (rcvsize == 0) {
                        // Real EOF (tcp close) or TLS close_notify mapped to 0
                        NetException e;
                        e[NetException::Close] << "ReadEventHandler: peer closed connection (EOF)";
                        throw e;
                    }

                    rcon->lasteventime = time(nullptr);
                    rcon->RecvData.append(buf.data.buf, rcvsize);

                    evconnection->RequestEvent(*rcon, tid, args);

                if(!rcon->SendData.empty()){
                    // If parser produced output OR ssl has pending ciphertext, go write
                    if (!rcon->SendData.empty() || rcon->csock->hasPendingWrite()) {
                        setpollEvents(rcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                        return;
                    }

                setpollEvents(rcon,EVFILT_READ,EV_ADD | EV_ONESHOT);

                    // else: keep draining reads until would-block
                }
            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                    // Re-register for read or write if it was a non-fatal temporary error
                    if(!rcon->SendData.empty()){
                        setpollEvents(rcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT);
                    }else{
                    // already handled, but keep safe
                    setpollEvents(rcon, EVFILT_READ, EV_ADD | EV_ONESHOT);
                    }
                    return;
                }
                throw e;
                throw;
            }
        }
        };

        void WriteEventHandler(int pos, const int tid, ULONG_PTR args) {
            con* wcon = reinterpret_cast<con*>(_Events[pos].udata);
            if (!wcon) return;

            if (!wcon) { return; }

            std::lock_guard<std::mutex> lock_guard(wcon->event_mutex);

            if (!wcon->csock) { return; }
            std::lock_guard<std::mutex> lk(wcon->event_mutex);
            if (!wcon->csock) return;

            try {
                // allow protocol to queue bytes
                evconnection->ResponseEvent(*wcon, tid, args);

                for (;;) {
                    // If no app bytes left, we might still need to flush TLS internal ciphertext
                    if (wcon->SendData.empty()) {
                    setpollEvents(wcon,EVFILT_READ,EV_ADD | EV_ONESHOT);
                        if (wcon->csock->hasPendingWrite()) {
                            setpollEvents(wcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                            return;
                        }

                size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size();
                        // truly nothing left to send
                        setpollEvents(wcon, EVFILT_READ, EV_ADD | EV_ONESHOT);
                        return;
                    }

                    const size_t ssize = std::min<size_t>(BLOCKSIZE, wcon->SendData.size());
                    buffer buf(wcon->SendData.data(), ssize);
                size_t sended = wcon->csock->sendData(buf);

                if (sended > ssize) {
                    size_t consumed = 0;
                    try {
                        consumed = wcon->csock->sendData(buf, 0);
                    } catch (NetException& e) {
                        if (e.getErrorType() == NetException::Note) {
                            setpollEvents(wcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                            return;
                        }
                        throw;
                    }

                    if (consumed == 0) {
                        // would-block or ssl flushing internal record
                        setpollEvents(wcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                        return;
                    }

                    if (consumed > ssize) {
                        NetException e;
                    e[NetException::Note] << "WriteEventHandler: Fatal socket error detected. Closing connection.";
                        e[NetException::Error] << "WriteEventHandler: sendData consumed > provided bytes";
                        throw e;
                    }

                    wcon->SendData.erase(wcon->SendData.begin(),
                                        wcon->SendData.begin() + sended);
                                        wcon->SendData.begin() + consumed);

                    wcon->lasteventime = time(nullptr);

                setpollEvents(wcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT);
                    // If we emptied app queue, let protocol enqueue more (chunking, pipelining, etc.)
                    if (wcon->SendData.empty()) {
                        evconnection->ResponseEvent(*wcon, tid, args);
                    }

                    // loop continues until would-block
                }
            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                    setpollEvents(wcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                    return;
                }else{
                    throw e;
                }
                throw;
            }
        }
        };

        void CloseEventHandler(int pos, const int tid, ULONG_PTR args) {
            std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX);
+84 −37
Changes for src/event/poll.cpp: 84 added lines, 37 removed lines.
Original line number Diff line number Diff line
@@ -357,104 +357,151 @@ namespace netplus {
            int fd = _ReadyEvents[pos].first;
            con* rcon = nullptr;

            // Get connection object from global map
            {
                std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX);
                auto it = CONNECTIONS.find(fd);
                if (it != CONNECTIONS.end()) {
                    rcon = it->second;
                }
                if (it != CONNECTIONS.end()) rcon = it->second;
            }

            if (!rcon || !rcon->csock) {
                return;
            }
            if (!rcon || !rcon->csock) return;

            std::lock_guard<std::mutex> lock_guard(rcon->event_mutex);
            std::lock_guard<std::mutex> g(rcon->event_mutex);

            try {
                for (;;) {
                    buffer buf(BLOCKSIZE);

                    size_t rcvsize = 0;
                    try {
                size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE);
                        rcvsize = rcon->csock->recvData(buf, 0);  // FLAGS, not BLOCKSIZE
                    } catch (NetException& e) {
                        if (e.getErrorType() == NetException::Note) {
                            // would-block -> decide what to wait for next
                            if (!rcon->SendData.empty() || rcon->csock->hasPendingWrite()) {
                                FD_CLR(fd, &_masterReadSet);
                                FD_SET(fd, &_masterWriteSet);
                            } else {
                                FD_CLR(fd, &_masterWriteSet);
                                FD_SET(fd, &_masterReadSet);
                            }
                            return;
                        }
                        throw;
                    }

                rcon->lasteventime = time(nullptr);
                    if (rcvsize == 0) {
                        // real EOF (tcp close) or TLS close_notify mapped to 0 by ssl::recvData
                        NetException e;
                        e[NetException::Close] << "ReadEventHandler: peer closed connection (EOF)";
                        throw e;
                    }

                if(rcvsize!=0){
                    rcon->lasteventime = time(nullptr);
                    rcon->RecvData.append(buf.data.buf, rcvsize);
                }

                    // Let protocol consume input and maybe queue output
                    evconnection->RequestEvent(*rcon, tid, args);

                if(!rcon->SendData.empty()){
                    // Switch to Write mode for this FD in the master sets
                    // If protocol produced output or ssl still needs flush -> switch to write mode
                    if (!rcon->SendData.empty() || rcon->csock->hasPendingWrite()) {
                        FD_CLR(fd, &_masterReadSet);
                        FD_SET(fd, &_masterWriteSet);
                        return;
                    }

                // If nothing to send, stay in Read mode.
                    // else: continue draining recv() until would-block
                }
            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                     // Note suggests temporary issue, stay in read mode
                    // already handled above, but keep safe
                    return;
                }
                throw e;
                throw;
            }
        }
        };

        void WriteEventHandler(int pos, const int tid, ULONG_PTR args) {
            int fd = _ReadyEvents[pos].first;
            con* wcon = nullptr;

            // Get connection object from global map
            {
                std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX);
                auto it = CONNECTIONS.find(fd);
                if (it != CONNECTIONS.end()) {
                    wcon = it->second;
                }
                if (it != CONNECTIONS.end()) wcon = it->second;
            }

            if(!wcon ||  !wcon->csock){
                return;
            }
            if (!wcon || !wcon->csock) return;

            std::lock_guard<std::mutex> lock_guard(wcon->event_mutex);
            std::lock_guard<std::mutex> g(wcon->event_mutex);

            try {
                // Let protocol enqueue response bytes (headers/body/etc.)
                evconnection->ResponseEvent(*wcon, tid, args);

                for (;;) {
                    // If app send queue is empty, we might still need to flush TLS internal record.
                    if (wcon->SendData.empty()) {
                    // Switch back to Read mode for this FD in the master sets
                        if (wcon->csock->hasPendingWrite()) {
                            // Stay in write mode until ssl flush is done
                            FD_CLR(fd, &_masterReadSet);
                            FD_SET(fd, &_masterWriteSet);
                            return;
                        }

                        // Nothing to send and nothing pending in ssl -> go read
                        FD_CLR(fd, &_masterWriteSet);
                        FD_SET(fd, &_masterReadSet);
                        return;
                    }

                size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size();
                    const size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size());
                    buffer buf(wcon->SendData.data(), toSend);

                    size_t consumed = 0;
                    try {
                        consumed = wcon->csock->sendData(buf, 0);
                    } catch (NetException& e) {
                        if (e.getErrorType() == NetException::Note) {
                            // would-block -> keep write interest
                            FD_CLR(fd, &_masterReadSet);
                            FD_SET(fd, &_masterWriteSet);
                            return;
                        }
                        throw;
                    }

                buffer buf(wcon->SendData.data(),ssize);
                size_t sended = wcon->csock->sendData(buf);
                    if (consumed == 0) {
                        // ssl may be flushing an internal record or would-block
                        FD_CLR(fd, &_masterReadSet);
                        FD_SET(fd, &_masterWriteSet);
                        return;
                    }

                if (sended > ssize) {
                    if (consumed > toSend) {
                        NetException e;
                    e[NetException::Note] << "WriteEventHandler: Fatal socket error detected. Closing connection.";
                        e[NetException::Error] << "WriteEventHandler: sendData consumed > provided bytes";
                        throw e;
                    }

                wcon->SendData.erase(wcon->SendData.begin(),wcon->SendData.begin() + sended);
                    wcon->SendData.erase(wcon->SendData.begin(),
                                        wcon->SendData.begin() + consumed);

                    wcon->lasteventime = time(nullptr);

                // Stay in Write mode.
                    // If we just emptied app queue, let protocol enqueue more (chunked etc.)
                    if (wcon->SendData.empty()) {
                        evconnection->ResponseEvent(*wcon, tid, args);
                    }

                    // loop continues: keep writing until would-block
                }
            } catch (NetException& e) {
                if (e.getErrorType() == NetException::Note) {
                    return;
                }else{
                    throw e;
                }
                throw;
            }
        }
        };

        void CloseEventHandler(int pos,const int tid,ULONG_PTR args) {
            int fd = _ReadyEvents[pos].first;
+0 −3
Changes for src/posix/tcp.cpp: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -181,7 +181,6 @@ void netplus::tcp::bind(){
}

size_t netplus::tcp::sendData(buffer &data,int flags){

    NetException exception;

    int rval=0;
@@ -210,8 +209,6 @@ size_t netplus::tcp::sendData(buffer &data,int flags){
    return (size_t)rval;
}



size_t netplus::tcp::recvData(buffer &data,int flags){
    NetException exception;

Loading