|
Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
|
The event buffer and the event_wait waiters. More...
#include <server_p.h>
Classes | |
| struct | Waiter |
| A pending event_wait call. More... | |
| struct | Match |
| A waiter that matched, with the events to answer it with. More... | |
| struct | Pending |
| Work for the sessions' thread, taken by takePending(). More... | |
Public Member Functions | |
| void | record (const Event &incoming) |
Buffers incoming with the next sequence number, matches waiters, and posts the flush if none is pending. | |
| QList< Event > | query (const EventFilter &filter, int limit, bool newest) const |
| Matching events (see ServerPrivate::queryEvents()). | |
| QList< Event > | queryOrWait (const std::shared_ptr< Waiter > &waiter) |
The matching events after waiter's filter, or, if there are none, registers the waiter, in one step, so no event is missed in between. | |
| bool | removeWaiter (const std::shared_ptr< Waiter > &waiter) |
Removes waiter. | |
| Pending | takePending () |
| Takes the pending work, and allows the next flush to be posted. | |
| void | setFlush (std::shared_ptr< HomeLink > link, std::function< void()> flush) |
Where and what the flush is: link is the sessions' thread (nullptr: none). | |
| quint64 | lastSequence () const |
| Sequence number of the newest event so far (0 if none). | |
| quint64 | firstSequence () const |
| Sequence number of the oldest buffered event (0 if none). | |
| int | capacity () const |
| Maximum buffered events. | |
| void | setCapacity (int capacity) |
| Sets the capacity (at least 1), dropping the oldest events if needed. | |
Private Member Functions | |
| QList< Event > | queryLocked (const EventFilter &filter, int limit, bool newest) const |
| query() without the lock. | |
| void | trimLocked () |
| Drops the oldest events beyond the capacity (lock held). | |
Private Attributes | |
| std::mutex | m_mutex |
| Guards everything below. | |
| std::deque< Event > | m_events |
| Ring buffer, oldest first. | |
| int | m_capacity = 5000 |
| Maximum buffered events. | |
| quint64 | m_nextSequence = 1 |
| Sequence number of the next event. | |
| quint64 | m_dropped = 0 |
| Events evicted from the buffer. | |
| QList< std::shared_ptr< Waiter > > | m_waiters |
| Pending event_wait calls. | |
| Pending | m_pending |
| Not yet flushed. | |
| bool | m_flushPosted = false |
| Whether a flush is on its way. | |
| std::shared_ptr< HomeLink > | m_flushLink |
| Where the flush is posted. | |
| std::function< void()> | m_flush |
| The flush. | |
The event buffer and the event_wait waiters.
Thread-safe.
Events are recorded on the thread that emits them, so they reach the buffer, and the events_* tools that read it on the worker, even while the server's thread is blocked (design 0015, phase 5). record() may run deep inside arbitrary code, a message handler say, so it only does bookkeeping under its mutex. What touches sessions, the log notifications and the answers to matched waiters, is queued as pending work, and a flush that drains it is posted (never run inline) to the sessions' thread.
| void Sonde::EventBuffer::record | ( | const Event & | incoming | ) |
Buffers incoming with the next sequence number, matches waiters, and posts the flush if none is pending.
Safe from any thread.
| bool Sonde::EventBuffer::removeWaiter | ( | const std::shared_ptr< Waiter > & | waiter | ) |
Removes waiter.