3#ifndef _FRAY_MESSAGE_HPP_
4#define _FRAY_MESSAGE_HPP_
72 MessageCategory
category = MESSAGE_CATEGORY_COMMAND;
109 return isNotice(prefix, MSG_READY) ||
isCommand(prefix, COMMAND_REQUEST_READY) ||
137 return (flags & MESSAGE_FLAG_NO_LOG) == 0;
148 return (flags & MESSAGE_FLAG_NO_CONSOLE) == 0;
175 std::shared_ptr<const std::vector<F_uchar>>
frame;
195 out[0] =
static_cast<F_uchar>(v >> 24);
196 out[1] =
static_cast<F_uchar>(v >> 16);
197 out[2] =
static_cast<F_uchar>(v >> 8);
198 out[3] =
static_cast<F_uchar>(v);
209 return (
static_cast<F_ulong>(in[0]) << 24) | (
static_cast<F_ulong>(in[1]) << 16) |
231 std::copy(p.
uid.begin(), p.
uid.end(), out + 23);
249 return errors::INVALID_MESSAGE;
251 return errors::UNSUPPORTED_MESSAGE_VERSION;
257 p.
senderKind =
static_cast<ProcessKind
>(data[6]);
262 std::copy(data + 23, data + 39, p.
uid.begin());
263 if (!MessageCategory_IsValid(data[39]))
264 return errors::INVALID_MESSAGE;
265 p.
category =
static_cast<MessageCategory
>(data[39]);
267 return errors::INVALID_MESSAGE;
269 return errors::MESSAGE_TOO_LARGE;
282 inline std::shared_ptr<const std::vector<F_uchar>>
assignUid(
283 const std::shared_ptr<
const std::vector<F_uchar>>& frame,
286 if (std::any_of(prefix.
uid.begin(), prefix.
uid.end(), [](
F_uchar byte) { return byte != 0; }))
289 auto assigned = std::make_shared<std::vector<F_uchar>>(*frame);
290 std::copy(uid.begin(), uid.end(), prefix.
uid.begin());
291 std::copy(prefix.
uid.begin(), prefix.
uid.end(), assigned->begin() + 23);
308 const Header &header,
311 std::vector<F_uchar> &out)
313 const size_t headerSize = header.ByteSizeLong();
315 return errors::UNSUPPORTED_MESSAGE_VERSION;
316 if (!MessageCategory_IsValid(prefix.
category) ||
317 headerSize >
static_cast<size_t>(std::numeric_limits<int>::max()) ||
318 payloadSize > UINT32_MAX - headerSize || (payloadSize && !payload))
319 return errors::INVALID_MESSAGE;
321 return errors::MESSAGE_TOO_LARGE;
323 prefix.
length =
static_cast<F_ulong>(headerSize + payloadSize);
326 std::vector<F_uchar> frame(
PREFIX_SIZE + headerSize + payloadSize);
328 if (!header.SerializeToArray(frame.data() +
PREFIX_SIZE,
static_cast<int>(headerSize)))
329 return errors::INVALID_MESSAGE;
331 std::copy(payload, payload + payloadSize, frame.begin() +
PREFIX_SIZE + headerSize);
333 out = std::move(frame);
347 case PROCESS_KIND_ENGINE:
return "Engine";
348 case PROCESS_KIND_ENGINE_PLUGIN:
return "Engine Plugin";
349 case PROCESS_KIND_ELECTRON_MAIN:
return "Electron Main";
350 case PROCESS_KIND_ELECTRON_RENDERER:
return "Electron Renderer";
351 case PROCESS_KIND_ELECTRON_PLUGIN:
return "Electron Plugin";
352 case PROCESS_KIND_BROADCAST:
return "Everyone";
353 case PROCESS_KIND_MULTICAST:
return "Multiple recipients";
354 default:
return "Unknown";
369 if (kind == PROCESS_KIND_ELECTRON_RENDERER)
371 name +=
" " + std::to_string(
id);
375 else if (kind == PROCESS_KIND_ENGINE_PLUGIN || kind == PROCESS_KIND_ELECTRON_PLUGIN)
376 name +=
" " + std::to_string(
id);
387 static std::atomic<uint64_t> next{1};
388 return next.fetch_add(1);
392 constexpr std::uint32_t
SILENT_FLAGS = MESSAGE_FLAG_NO_LOG | MESSAGE_FLAG_NO_CONSOLE;
416 MessageCategory
category = MESSAGE_CATEGORY_COMMAND;
432 const Header& header)
434 if (header.recipients().empty())
436 std::set<std::pair<ProcessKind, F_ulong>> seen;
437 for (
const auto& recipient : header.recipients())
439 if (recipient.kind() < PROCESS_KIND_ENGINE || recipient.kind() > PROCESS_KIND_ELECTRON_PLUGIN ||
440 !seen.emplace(recipient.kind(), recipient.id()).second)
462 std::shared_ptr<
const std::vector<F_uchar>> &out)
const
465 return errors::INVALID_MESSAGE;
475 header.set_timestamp(std::chrono::duration_cast<std::chrono::milliseconds>(
476 std::chrono::system_clock::now().time_since_epoch()).count());
477 header.set_sender_name(
processName(PROCESS_KIND_ENGINE));
482 header.set_ttl_ms(options.
ttlMs);
483 header.set_flags(options.
flags);
484 for (
const auto& recipient : options.
recipients)
486 auto* target = header.add_recipients();
487 target->set_kind(recipient.first);
488 target->set_id(recipient.second);
493 return errors::INVALID_MESSAGE;
494 auto frame = std::make_shared<std::vector<F_uchar>>();
496 options.
payload.size(), *frame);
497 if (err != errors::NONE)
499 out = std::move(frame);
512 std::shared_ptr<
const std::vector<F_uchar>> frame,
516 return errors::INVALID_MESSAGE;
522 return errors::INVALID_MESSAGE;
524 return errors::INVALID_MESSAGE;
526 return errors::INVALID_MESSAGE;
528 return errors::UNSUPPORTED_MESSAGE_VERSION;
530 in.
frame = std::move(frame);
Builds outgoing frames sent by the Engine (sender is always the Engine). Stateless; safe to use from ...
F_err build(const FrameOptions &options, std::shared_ptr< const std::vector< F_uchar > > &out) const
Builds an Engine frame from the options, stamping the protocol version, a new message id and the curr...
F_long F_err
Error return type used across the Engine; the value is a fray::errors::code (0 / NONE means success).
std::array< std::uint8_t, 16 > Uid
A 128-bit identifier shared by routing and stored entries.
Uid generate()
Generates a random, globally unique identifier.
Internal helpers for big-endian encoding.
void put32(F_uchar *out, F_ulong v)
Writes a 32-bit value as 4 big-endian bytes.
F_ulong get32(const F_uchar *in)
Reads 4 big-endian bytes as a 32-bit value.
bool isCommand(const Prefix &prefix, F_ulong id)
Tests whether a prefix identifies a command.
F_err decodePrefix(const F_uchar *data, size_t size, Prefix &out)
Parses and validates a prefix from received bytes.
const char * processName(ProcessKind kind)
Human-readable name of a process kind, also used as the header sender/destination name.
bool wantsLog(std::uint32_t flags)
Tests whether a message's header flags leave it in the disk log.
F_err parseFrame(std::shared_ptr< const std::vector< F_uchar > > frame, InData &out)
Parses the header of a complete frame. The payload is not read or copied.
uint64_t nextMessageId()
Returns the next message ID. Message IDs are unique per sender, so every engine message takes its ID ...
constexpr F_ulong MAX_FRAME_LENGTH
Largest allowed value of the prefix length field (header + payload). Checked before any buffer is all...
std::shared_ptr< const std::vector< F_uchar > > assignUid(const std::shared_ptr< const std::vector< F_uchar > > &frame, Prefix &prefix)
Assigns a random 128-bit UID to a frame whose prefix does not have one. Existing UIDs are preserved; ...
bool wantsConsole(std::uint32_t flags)
Tests whether a message's header flags leave it in the Console.
F_err buildFrame(Prefix prefix, const Header &header, const F_uchar *payload, size_t payloadSize, std::vector< F_uchar > &out)
Builds a complete frame (prefix + protobuf header + payload) into a byte vector. The prefix's length ...
constexpr std::uint32_t SILENT_FLAGS
bool validMulticastRecipients(const Header &header)
Validates the explicit destinations of a multicast header.
constexpr std::uint32_t PROTOCOL_VERSION
Version written to the protobuf message header.
bool isControl(const Prefix &prefix)
Identifies readiness and health control frames.
constexpr std::uint32_t MAIN_WINDOW_ID
Renderer instance ID assigned to the first application window.
bool isConsoleCommand(const Prefix &prefix)
Identifies commands the Console shows as entries. Control traffic and the Console's own log and entry...
constexpr size_t PREFIX_SIZE
Size in bytes of the fixed binary routing prefix.
constexpr std::uint32_t LOADING_WINDOW_ID
Renderer instance ID assigned to the loading window.
constexpr F_uchar PREFIX_VERSION
Wire format version written to, and required in, byte 0 of every prefix.
std::string processInstanceName(ProcessKind kind, std::uint32_t id)
Names a process instance for readiness diagnostics.
bool isTracked(const Prefix &prefix, std::uint32_t flags)
Identifies messages the Engine router logs, shows in the Console, or both.
bool isNotice(const Prefix &prefix, F_ulong id)
Tests whether a prefix identifies a protocol notification.
void encodePrefix(const Prefix &p, F_uchar *out)
Serializes a prefix into its PREFIX_SIZE-byte big-endian wire form.
Everything FrameBuilder needs to build one outgoing Engine message.
std::optional< std::string > targetProject
std::vector< std::pair< ProcessKind, F_ulong > > recipients
A parsed incoming message. The payload is a view into the frame, which this keeps alive via shared ow...
size_t payloadSize() const
Payload length in bytes (prefix.length minus headerLength).
std::shared_ptr< const std::vector< F_uchar > > frame
const F_uchar * payload() const
Pointer to the first payload byte inside frame (valid only while frame is held; frame must be non-nul...
Fixed-size routing prefix at the start of every frame; routers read only this. All integers are big-e...
ProcessKind destinationKind
unsigned char F_uchar
Alias for unsigned char (a byte).
uint32_t F_ulong
Unsigned 32-bit integer.