Networking

CMessenger

Exchange native Catalyst messages, observe callbacks, and make synchronous calls.

C++23 mc/CMessenger.h
#include <mc/CMessenger.h>

The pool is borrowed and must remain running until connections close and callbacks finish. Delegates are borrowed; keep them alive while registered.

Callbacks run on pool threads. A callback that returns false leaves the message available to receive(); true consumes it.

The legacy transport carries Catalyst values without TLS. The retained encryption/decryption delegate hooks are not invoked by this protocol.

Jump to a declaration · 21

CMessengerDelegate

Methods

encrypt

virtual char* encrypt(CMessenger* m, char* buf, uint32_t& size);

Reserved compatibility hook. The current legacy wire protocol does not invoke encryption; the default returns the input buffer.

decrypt

virtual char* decrypt(CMessenger* m, char* buf, uint32_t& size);

Reserved compatibility hook. The current legacy wire protocol does not invoke decryption; the default returns the input buffer.

didClose

virtual void didClose(CMessenger* m);

Called once when peer closure or an asynchronous connection error closes the messenger. Explicit close() is silent; the default callback does nothing.

handle

virtual bool handle(CMessenger* m, cvar& msg);

Return true to consume the message, or false to queue it for receive().

CMessenger

Methods

CMessenger

explicit CMessenger(CPool& pool, CSocket* socket = nullptr);
CMessenger(const CMessenger&) = delete;

Creates a messenger using a borrowed worker pool, which must keep running through connection cleanup and callbacks. An optional socket transfers ownership, including if construction fails; without one, use connect() to establish a connection.

setDelegate

void setDelegate(CMessengerDelegate* delegate);

Borrows the delegate; nullptr selects the default. Replacement waits for an active callback to finish.

delegate

CMessengerDelegate* delegate();

Returns the currently installed borrowed delegate. Reading the pointer does not retain it; coordinate its lifetime with setDelegate() and active callbacks.

connect

bool connect(const cstr& host, int port);

Connects to the host and port, returning whether connection succeeded.

pool

CPool& pool();

Returns the borrowed worker pool used for message I/O and delegate dispatch. Keep it running until the messenger and its callbacks have shut down.

close

void close();

Closes the connection silently. Peer closure and asynchronous failures notify didClose() once.

connected

bool connected() const;

Reports the current connection state. Peer activity can change it immediately, so sends and receives must still handle connection failures.

send

void send(const cvar& msg);
void send(cvar&& msg);

Queues a value for transmission.

sendNow

void sendNow(cvar& msg);
void sendNow(cvar&& msg);

Waits for local transmission in FIFO order, not peer receipt. Both overloads consume the input value.

call

cvar call(const cvar& msg);
cvar call(cvar&& msg);

Makes a blocking legacy RPC using reserved #C and #R keys. Only one call may be outstanding; do not call from this messenger’s own callback.

reply

void reply(cvar&& msg);
void reply(const cvar& msg);

Sends a legacy #R reply corresponding to an incoming #C request. The rvalue overload may consume its message; it does not establish a separate RPC channel.

receive

bool receive(cvar& msg);
bool receive(cvar& msg, double dt);

Returns the next unhandled message. False means timeout or clean closure; asynchronous errors throw after queued messages drain.

session

cvar& session();

Returns application session state. Synchronize concurrent access externally.