|
Fray Engine
|
Owns the TCP servers and the threads around them. More...
#include <ipc_manager.hpp>
Public Member Functions | |
| IPCManager (FrayManager &application) | |
| Creates an IPCManager with its own ThreadManager, which is convenient for tests. | |
| IPCManager (threads::ThreadManager &threads, FrayManager &application) | |
| Creates an IPCManager whose threads register with a ThreadManager owned by someone else. | |
| ~IPCManager () | |
| Finishes IPC and application shutdown if not already finished. | |
| F_void | finish () |
| Stops IPC, releases servers, and shuts the application down exactly once. Call on the controlling thread; waits for local workers to finish. | |
| TCPServerSP | createTCPServer (F_ushort port) |
| Creates a TCP server and starts it accepting. Must be called before start(). Not thread-safe; call from the thread that owns the IPCManager. | |
| F_void | removeTCPServer (F_ushort port) |
| Forgets the server on the port (called by TCPServer::stop on the network thread). When no servers remain, cancels the monitor timer and stops the io_context so the network thread ends. | |
| F_void | start () |
| Starts the network thread and the engine message thread, then returns. Does nothing if already started. Also starts monitoring Electron Main and the once-a-second monitor timer. | |
| F_void | sendLoadingBuildInfo (const std::string &name, const std::string &version) |
| Queues build information for the loading Renderer without waiting for it to start. | |
| F_void | sendWindowCommand (F_ulong operation, std::uint32_t windowId) |
| Queues a window operation for Electron Main. | |
| F_void | startLoading (const std::string &name, const std::string &version) |
| Queues the initial hidden loading window and its application information. | |
| F_void | setFontReader (std::function< std::vector< std::string >()> reader) |
| Sets how installed font families are listed during startup. Call before startLoading(). | |
| F_void | readSystemFonts () |
| Reads the installed font families on a worker thread, then hands them to the message thread, which records the startup milestone and delivers them to the application Renderer. Nothing waits for the scan. | |
| F_void | sendSystemFonts () |
| Message thread only. Sends the installed font families to the application Renderer, which keeps them for the session. | |
| F_void | loadingReadyToShow () |
| Records loading readiness and independently commands both startup windows. | |
| F_void | scheduleLoadingIntro () |
| Starts the Engine-owned, nonblocking loading logo intro timer. | |
| F_void | sendStartupProgress (const std::string &status="") |
| Publishes the Engine's startup snapshot to the loading Renderer. | |
| F_void | recordStartupMilestone (unsigned milestone) |
| Records an independent startup event and advances the Engine-owned transition. | |
| F_void | finishStartupIfReady () |
| Transitions only after startup milestones and the loading intro timer have both finished. | |
| F_void | startupFailed (const std::string &error) |
| Records Main's independent report that application-window startup failed. | |
| F_void | mainFrameIsReady () |
| Records Main's announcement that the application window has a first frame. | |
| F_void | scheduleStartupWarning () |
| Warns about slow startup without completing it or blocking any process. | |
| bool | loadingWindowActive () const |
| Whether startup logs should include the loading window. | |
| F_void | networkFinished () |
| Marks the network thread as finished and wakes wait(). Takes _networkMutex briefly. Called by the network thread as its last act, including after an exception. | |
| F_void | scheduleMonitor () |
| Arms the one-second monitor timer; each firing queues a router sweep and neighbor-monitor tick on the message thread, then re-arms. Runs on the network thread (timer handler); stops re-arming if the timer is cancelled or errors. | |
| F_void | pollShutdown () |
| Starts a short-interval local poll that advances shutdown until its terminal command is queued. Message thread only; repeated calls while polling do nothing. | |
| F_void | armShutdownPoll () |
| Advances shutdown now and re-arms the poll on the network thread while it is still pending. | |
| F_void | advanceShutdown () |
| Advances Engine-local shutdown and sends Main an independent terminal command when ready. | |
| F_void | wait () |
| Blocks until every server has stopped and the queued messages have been handled. While waiting it posts a heartbeat task to the message thread each second, then joins both threads. Subsequent calls on the controlling thread do nothing. Takes _networkMutex only while checking/waiting (released during the wait). Call from the owning (main) thread, not from the managed threads. | |
| F_void | stop () |
| Asks every server to stop (run on the network thread). Safe from any thread; wait() returns once the last one is gone. | |
| bool | send (Frame frame) |
| Hands a message to the router and returns at once. Safe from any thread. The router delivers it now if its destination is ready, otherwise it waits in that destination's queue for as long as the header's ttl_ms allows (0 = until the destination is ready). Nothing is sent back to the caller either way. | |
| F_void | sendBuilt (const message::FrameOptions &options) |
| Builds an Engine frame from the options and sends it with send(). Safe from any thread. Failures are logged, not returned. | |
| F_void | sendReady (ProcessKind destination, F_ulong destinationId) |
| Sends a readiness notification without waiting for the destination. | |
| Frame | identify (const Frame &frame, message::Prefix &prefix) |
| Assigns a routing UID without preventing delivery if UID generation fails. | |
| F_void | routeMulticast (const message::InData &incoming, bool record) |
| Records one multicast command, handles the Engine recipient, and queues one frame to Main. | |
| F_void | route (const Frame &frame) |
| Router thread only. Delivers an outgoing frame, or queues it if its destination is not ready. Readiness messages and broadcasts always go out at once, because they are how readiness is learned and who is reachable is not known yet. Everything else goes through the outgoing queue. Non-readiness messages also count toward the health progress counters. Assigns a UID when needed before handing the frame to its destination. | |
| F_void | transmit (Frame frame) |
| Writes a frame to every connected client. Safe from any thread (posts to the network thread). | |
| F_void | handleReadiness (const message::InData &incoming) |
| Message thread only. Handles readiness and health traffic from a neighbor. A READY marks the sender ready and delivers whatever was waiting for it. A REQUEST_READY is answered with a READY sent back to the sender only, so no peer ever has to announce to everyone because someone else asked. A PING is answered with a PONG carrying a health report; a PONG's report is passed to the neighbor monitor. Any message except READY also counts as the sender being heard. Messages from the Engine or broadcast senders are ignored. | |
| F_void | queueMessage (Frame frame) |
| Called by the network thread: queues a received message for the engine message thread. On the message thread the frame is validated and routed. Messages for the Engine are logged, readiness is handled, commands go to FrayManager::handleApplicationMessage and receipts to FrayManager::handleReceipt. Messages for Electron Main or Renderer are recorded (disk log and Console) unless their flags opt out, then relayed with their UID. Assigns a UID when needed. Errors are logged. | |
Private Types | |
| enum | StartupMilestone : unsigned { engineStarted = 1 , mainConnected = 2 , loadingReady = 4 , rendererConnected = 8 , mainFrameReady = 16 , systemFontsReady = 32 } |
| using | Neighbor = std::pair<ProcessKind, F_ulong> |
Private Attributes | |
| boost::asio::io_context | _io |
| boost::asio::steady_timer | _monitorTimer {_io} |
| boost::asio::steady_timer | _startupTimer {_io} |
| boost::asio::steady_timer | _loadingIntroTimer {_io} |
| std::atomic< bool > | _loadingWindowActive {true} |
| bool | _startupActive = false |
| bool | _startupFailed = false |
| bool | _loadingIntroElapsed = false |
| unsigned | _startupMilestones = 0 |
| unsigned | _startupPercent = 0 |
| unsigned | _startupWarningGeneration = 0 |
| threads::ThreadManager::Id | _fontsThread = 0 |
| std::function< std::vector< std::string >()> | _fontReader = [] { return std::vector<std::string>(); } |
| std::vector< std::string > | _systemFonts |
| bool | _systemFontsLoaded = false |
| std::atomic< bool > | _startupMainPending {false} |
| Keeps the initial startup queue until Main first announces readiness. | |
| std::vector< TCPServerSP > | _servers |
| threads::TaskQueue | _messages |
| bool | _started = false |
| bool | _waited = false |
| bool | _finished = false |
| bool | _shutdownCommandSent = false |
| boost::asio::steady_timer | _shutdownTimer {_io} |
| bool | _shutdownPolling = false |
| bool | _shutdownConsoleStarted = false |
| threads::ThreadManager::Id | _networkThread = 0 |
| threads::ThreadManager::Id | _messageThread = 0 |
| std::mutex | _networkMutex |
| std::condition_variable | _networkWake |
| bool | _networkStopped = false |
| uint64_t | _heartbeat = 0 |
| uint64_t | _processedCount = 0 |
| std::chrono::steady_clock::time_point | _lastProgress = std::chrono::steady_clock::now() |
| FrayManager & | _application |
| routing::OutgoingRouter< Neighbor, Frame > | _outgoing |
| routing::NeighborMonitor< Neighbor > | _monitor |
| std::unique_ptr< threads::ThreadManager > | _ownedThreads |
| threads::ThreadManager * | _threads |
Static Private Attributes | |
| static constexpr auto | loadingIntroDuration = std::chrono::milliseconds(2000) |
| static constexpr unsigned | allStartupMilestones |
| static constexpr unsigned | startupMilestoneCount = 6 |
Owns the TCP servers and the threads around them.
Definition at line 185 of file ipc_manager.hpp.
|
private |
A neighbor is identified by its process kind and instance ID.
Definition at line 264 of file ipc_manager.hpp.
|
private |
| Enumerator | |
|---|---|
| engineStarted | |
| mainConnected | |
| loadingReady | |
| rendererConnected | |
| mainFrameReady | |
| systemFontsReady | |
Definition at line 208 of file ipc_manager.hpp.
|
inlineexplicit |
Creates an IPCManager with its own ThreadManager, which is convenient for tests.
| application | Receives engine messages and is shut down by the destructor; must outlive this object. |
Definition at line 323 of file ipc_manager.hpp.
|
inline |
Creates an IPCManager whose threads register with a ThreadManager owned by someone else.
| threads | Shared ThreadManager; must outlive this object. |
| application | Receives engine messages and is shut down by the destructor; must outlive this object. |
Definition at line 336 of file ipc_manager.hpp.
|
inline |
Finishes IPC and application shutdown if not already finished.
Definition at line 347 of file ipc_manager.hpp.
|
inline |
Advances Engine-local shutdown and sends Main an independent terminal command when ready.
Definition at line 782 of file ipc_manager.hpp.
|
inline |
Advances shutdown now and re-arms the poll on the network thread while it is still pending.
Definition at line 762 of file ipc_manager.hpp.
|
inline |
Creates a TCP server and starts it accepting. Must be called before start(). Not thread-safe; call from the thread that owns the IPCManager.
| port | TCP port to listen on; 0 lets the OS pick a free port (read it from the result's port()). |
Definition at line 385 of file ipc_manager.hpp.
|
inline |
Stops IPC, releases servers, and shuts the application down exactly once. Call on the controlling thread; waits for local workers to finish.
Definition at line 353 of file ipc_manager.hpp.
|
inline |
Transitions only after startup milestones and the loading intro timer have both finished.
Definition at line 652 of file ipc_manager.hpp.
|
inline |
Message thread only. Handles readiness and health traffic from a neighbor. A READY marks the sender ready and delivers whatever was waiting for it. A REQUEST_READY is answered with a READY sent back to the sender only, so no peer ever has to announce to everyone because someone else asked. A PING is answered with a PONG carrying a health report; a PONG's report is passed to the neighbor monitor. Any message except READY also counts as the sender being heard. Messages from the Engine or broadcast senders are ignored.
| incoming | Parsed incoming message. |
Definition at line 1105 of file ipc_manager.hpp.
|
inline |
Assigns a routing UID without preventing delivery if UID generation fails.
| frame | Validated frame to identify. |
| prefix | Decoded frame prefix, updated when an identity is assigned. |
Definition at line 932 of file ipc_manager.hpp.
|
inline |
Records loading readiness and independently commands both startup windows.
Definition at line 564 of file ipc_manager.hpp.
|
inline |
Whether startup logs should include the loading window.
Definition at line 714 of file ipc_manager.hpp.
|
inline |
Records Main's announcement that the application window has a first frame.
Definition at line 681 of file ipc_manager.hpp.
|
inline |
Marks the network thread as finished and wakes wait(). Takes _networkMutex briefly. Called by the network thread as its last act, including after an exception.
Definition at line 720 of file ipc_manager.hpp.
|
inline |
Starts a short-interval local poll that advances shutdown until its terminal command is queued. Message thread only; repeated calls while polling do nothing.
Definition at line 753 of file ipc_manager.hpp.
Called by the network thread: queues a received message for the engine message thread. On the message thread the frame is validated and routed. Messages for the Engine are logged, readiness is handled, commands go to FrayManager::handleApplicationMessage and receipts to FrayManager::handleReceipt. Messages for Electron Main or Renderer are recorded (disk log and Console) unless their flags opt out, then relayed with their UID. Assigns a UID when needed. Errors are logged.
| frame | Complete received frame. |
Definition at line 1194 of file ipc_manager.hpp.
|
inline |
Reads the installed font families on a worker thread, then hands them to the message thread, which records the startup milestone and delivers them to the application Renderer. Nothing waits for the scan.
Definition at line 529 of file ipc_manager.hpp.
|
inline |
Records an independent startup event and advances the Engine-owned transition.
| milestone | Bit identifying the completed milestone. |
Definition at line 635 of file ipc_manager.hpp.
Forgets the server on the port (called by TCPServer::stop on the network thread). When no servers remain, cancels the monitor timer and stops the io_context so the network thread ends.
| port | Port of the server to remove; nothing is removed if none matches. |
Definition at line 401 of file ipc_manager.hpp.
Router thread only. Delivers an outgoing frame, or queues it if its destination is not ready. Readiness messages and broadcasts always go out at once, because they are how readiness is learned and who is reachable is not known yet. Everything else goes through the outgoing queue. Non-readiness messages also count toward the health progress counters. Assigns a UID when needed before handing the frame to its destination.
| frame | Frame to route; an unparsable frame is logged and dropped. |
Definition at line 1043 of file ipc_manager.hpp.
|
inline |
Records one multicast command, handles the Engine recipient, and queues one frame to Main.
| incoming | Parsed, UID-stamped multicast frame. |
| record | Whether this arrival needs its command record. |
Definition at line 957 of file ipc_manager.hpp.
|
inline |
Starts the Engine-owned, nonblocking loading logo intro timer.
Definition at line 578 of file ipc_manager.hpp.
|
inline |
Arms the one-second monitor timer; each firing queues a router sweep and neighbor-monitor tick on the message thread, then re-arms. Runs on the network thread (timer handler); stops re-arming if the timer is cancelled or errors.
Definition at line 733 of file ipc_manager.hpp.
|
inline |
Warns about slow startup without completing it or blocking any process.
Definition at line 687 of file ipc_manager.hpp.
|
inline |
Hands a message to the router and returns at once. Safe from any thread. The router delivers it now if its destination is ready, otherwise it waits in that destination's queue for as long as the header's ttl_ms allows (0 = until the destination is ready). Nothing is sent back to the caller either way.
| frame | Complete frame to send; must not be null. |
Definition at line 877 of file ipc_manager.hpp.
|
inline |
Builds an Engine frame from the options and sends it with send(). Safe from any thread. Failures are logged, not returned.
| options | Destination, command, payload and header fields for the message. |
Definition at line 898 of file ipc_manager.hpp.
|
inline |
Queues build information for the loading Renderer without waiting for it to start.
| name | Public build name. |
| version | Application version string. |
Definition at line 452 of file ipc_manager.hpp.
Sends a readiness notification without waiting for the destination.
| destination | Process kind receiving the notification. |
| destinationId | Recipient instance identifier. |
Definition at line 916 of file ipc_manager.hpp.
|
inline |
Publishes the Engine's startup snapshot to the loading Renderer.
| status | Optional warning or error displayed separately from the latest log line. |
Definition at line 607 of file ipc_manager.hpp.
|
inline |
Message thread only. Sends the installed font families to the application Renderer, which keeps them for the session.
Definition at line 548 of file ipc_manager.hpp.
|
inline |
Queues a window operation for Electron Main.
| operation | Window command to send. |
| windowId | Renderer window instance to operate on. |
Definition at line 472 of file ipc_manager.hpp.
|
inline |
Sets how installed font families are listed during startup. Call before startLoading().
| reader | Function returning the family names; it runs on a worker thread. |
Definition at line 520 of file ipc_manager.hpp.
|
inline |
Starts the network thread and the engine message thread, then returns. Does nothing if already started. Also starts monitoring Electron Main and the once-a-second monitor timer.
Definition at line 425 of file ipc_manager.hpp.
|
inline |
Queues the initial hidden loading window and its application information.
| name | Public application build name. |
| version | Application version string. |
Definition at line 490 of file ipc_manager.hpp.
|
inline |
Records Main's independent report that application-window startup failed.
| error | Human-readable failure shown on the loading window. |
Definition at line 670 of file ipc_manager.hpp.
|
inline |
Asks every server to stop (run on the network thread). Safe from any thread; wait() returns once the last one is gone.
Definition at line 859 of file ipc_manager.hpp.
Writes a frame to every connected client. Safe from any thread (posts to the network thread).
| frame | Frame to broadcast. |
Definition at line 1087 of file ipc_manager.hpp.
|
inline |
Blocks until every server has stopped and the queued messages have been handled. While waiting it posts a heartbeat task to the message thread each second, then joins both threads. Subsequent calls on the controlling thread do nothing. Takes _networkMutex only while checking/waiting (released during the wait). Call from the owning (main) thread, not from the managed threads.
Definition at line 838 of file ipc_manager.hpp.
|
private |
The application that handles engine messages; must outlive this object.
Definition at line 262 of file ipc_manager.hpp.
|
private |
True after the controlling thread has finished IPC and application shutdown.
Definition at line 236 of file ipc_manager.hpp.
|
private |
Lists the installed font families; replaced before startLoading(). Reports none by default so tests need no platform font libraries.
Definition at line 220 of file ipc_manager.hpp.
|
private |
Reads the installed font families in the background during startup.
Definition at line 218 of file ipc_manager.hpp.
|
private |
Counter bumped on the message thread by wait(); lets a health report show that thread is alive.
Definition at line 256 of file ipc_manager.hpp.
|
private |
Boost io_context run by the network thread.
Definition at line 189 of file ipc_manager.hpp.
|
private |
When _processedCount last changed; message thread only.
Definition at line 260 of file ipc_manager.hpp.
|
private |
Definition at line 203 of file ipc_manager.hpp.
|
private |
Tracks the loading logo intro independently of startup milestones.
Definition at line 195 of file ipc_manager.hpp.
|
private |
Whether startup logs still include the loading window.
Definition at line 199 of file ipc_manager.hpp.
|
private |
Queue drained in order by the engine message thread.
Definition at line 230 of file ipc_manager.hpp.
|
private |
ThreadManager id of the engine message thread.
Definition at line 248 of file ipc_manager.hpp.
|
private |
Health monitor for neighbors: its callbacks send probes/pings, read the router's state, and apply state changes (marking ready, discarding queues of down neighbors, cancelling prompts if Electron Main is lost). Message thread only.
Definition at line 273 of file ipc_manager.hpp.
|
private |
Fires once a second to queue a router sweep and a neighbor-monitor tick.
Definition at line 191 of file ipc_manager.hpp.
|
private |
Guards _networkStopped; paired with _networkWake.
Definition at line 250 of file ipc_manager.hpp.
|
private |
True once the network thread has finished (guarded by _networkMutex).
Definition at line 254 of file ipc_manager.hpp.
|
private |
ThreadManager id of the network thread.
Definition at line 246 of file ipc_manager.hpp.
|
private |
Notified when the network thread finishes so wait() can continue.
Definition at line 252 of file ipc_manager.hpp.
|
private |
Readiness table and waiting-message queues per neighbor instance; delivery writes to every client via transmit().
Definition at line 267 of file ipc_manager.hpp.
|
private |
ThreadManager created by the single-argument constructor; null when an external one is used.
Definition at line 314 of file ipc_manager.hpp.
|
private |
Number of non-readiness messages processed (sent or received); message thread only.
Definition at line 258 of file ipc_manager.hpp.
|
private |
Servers owned here; touched on the creating thread before start() and on the network thread afterwards.
Definition at line 228 of file ipc_manager.hpp.
|
private |
True once the terminal Engine command has been scheduled; message thread only.
Definition at line 238 of file ipc_manager.hpp.
|
private |
True after Console delivery has been asked to drain; message thread only.
Definition at line 244 of file ipc_manager.hpp.
|
private |
True while the fast shutdown poll is armed; message thread only.
Definition at line 242 of file ipc_manager.hpp.
|
private |
Fires while shutdown is pending so progress is noticed within milliseconds.
Definition at line 240 of file ipc_manager.hpp.
|
private |
True once start() has run.
Definition at line 232 of file ipc_manager.hpp.
|
private |
Startup milestone state, owned by the message thread.
Definition at line 201 of file ipc_manager.hpp.
|
private |
Definition at line 202 of file ipc_manager.hpp.
|
private |
Keeps the initial startup queue until Main first announces readiness.
Definition at line 226 of file ipc_manager.hpp.
|
private |
Definition at line 204 of file ipc_manager.hpp.
|
private |
Definition at line 205 of file ipc_manager.hpp.
|
private |
Reports startup that has taken longer than expected.
Definition at line 193 of file ipc_manager.hpp.
|
private |
Distinguishes the current slow-start warning from cancelled older timers.
Definition at line 207 of file ipc_manager.hpp.
|
private |
The installed font families once read; message thread only.
Definition at line 222 of file ipc_manager.hpp.
|
private |
Whether _systemFonts has been read, even if the computer has none; message thread only.
Definition at line 224 of file ipc_manager.hpp.
|
private |
The ThreadManager that registers the network and message threads (owned or external).
Definition at line 316 of file ipc_manager.hpp.
|
private |
True once wait() has joined the IPC workers on the controlling thread.
Definition at line 234 of file ipc_manager.hpp.
|
staticconstexprprivate |
Every milestone that must be recorded before the loading window gives way to the application window.
Definition at line 214 of file ipc_manager.hpp.
|
staticconstexprprivate |
Must be at least as long as the loading Renderer logo intro's total GSAP duration.
Definition at line 197 of file ipc_manager.hpp.
|
staticconstexprprivate |
Definition at line 216 of file ipc_manager.hpp.