Commit 832f5b7f authored by jan.koester's avatar jan.koester
Browse files

test

parent d2ff9094
Loading
Loading
Loading
Loading
+49 −50
Changes for src/event/kqueue.cpp: 49 added lines, 50 removed lines.
Original line number Diff line number Diff line
@@ -264,103 +264,102 @@ 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; }

            buffer buf(BLOCKSIZE);
            std::lock_guard<std::mutex> lk(rcon->event_mutex);
            if (!rcon->csock) return;

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

                size_t rcvsize = rcon->csock->recvData(buf, BLOCKSIZE);
                    // Your recvData should throw NetException::Note on EAGAIN/EWOULDBLOCK
                    const size_t rcv = rcon->csock->recvData(buf, 0);

                rcon->lasteventime = time(nullptr);

                if (rcvsize <= 0) {
                    if (rcv == 0) {
                        NetException e;
                    if (rcvsize == 0) {
                        e[NetException::Note] << "ReadEventHandler: EOF detected. Closing connection.";
                    } else {
                        e[NetException::Note] << "ReadEventHandler: Fatal socket error or eof detected. Closing connection.";
                    }
                        e[NetException::Close] << "ReadEventHandler: peer closed connection (EOF)";
                        throw e;
                    }

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

                    // Let protocol consume/parse and possibly queue SendData
                    evconnection->RequestEvent(*rcon, tid, args);

                    // If we now have output, switch to write immediately
                    if (!rcon->SendData.empty()) {
                        setpollEvents(rcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                        return;
                    }

                setpollEvents(rcon,EVFILT_READ,EV_ADD | EV_ONESHOT);
                    // otherwise 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()){
                    // would-block: arm read unless output pending
                    if (!rcon->SendData.empty())
                        setpollEvents(rcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                    }else{
                    else
                        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 {
                evconnection->ResponseEvent(*wcon,tid,args);
                // Do NOT call ResponseEvent repeatedly inside the send loop.
                // Only call it when we *need more plaintext* (queue empty) OR after fully flushing.
                while (!wcon->SendData.empty()) {
                    const size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size());
                    buffer out(wcon->SendData.data(), toSend);

                if(wcon->SendData.empty()){
                    setpollEvents(wcon,EVFILT_READ,EV_ADD | EV_ONESHOT);
                    return;
                }
                    // IMPORTANT: sendData returns PLAINTEXT CONSUMED
                    const size_t consumed = wcon->csock->sendData(out, 0);

                size_t ssize = BLOCKSIZE < wcon->SendData.size() ? BLOCKSIZE : wcon->SendData.size();

                buffer buf(wcon->SendData.data(),ssize);
                size_t sended = wcon->csock->sendData(buf);

                if (sended > ssize) {
                    NetException e;
                    e[NetException::Note] << "WriteEventHandler: Fatal socket error detected. Closing connection.";
                    throw e;
                    if (consumed == 0) {
                        // would-block OR SSL is still flushing its internal pending record
                        setpollEvents(wcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                        return;
                    }

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

                    wcon->lasteventime = time(nullptr);
                }

                // Queue fully flushed. NOW let protocol layer append more (e.g. streaming file),
                // but only ONCE.
                evconnection->ResponseEvent(*wcon, tid, args);

                // If ResponseEvent queued more, stay in write. Otherwise go back to read.
                if (!wcon->SendData.empty()) {
                    setpollEvents(wcon, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                } else {
                    setpollEvents(wcon, EVFILT_READ, EV_ADD | EV_ONESHOT);
                }
                return;

            } 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);