From 32fa98bc76cb555da4c67165b605843b177dc7a0 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 1 Feb 2023 11:09:06 +0100 Subject: [PATCH] Crystal bindings: IPC::JSON comments. --- zig-impl/crystal/some-crystal-app/src/json.cr | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/zig-impl/crystal/some-crystal-app/src/json.cr b/zig-impl/crystal/some-crystal-app/src/json.cr index f0582f4..5bbfcaa 100644 --- a/zig-impl/crystal/some-crystal-app/src/json.cr +++ b/zig-impl/crystal/some-crystal-app/src/json.cr @@ -1,5 +1,14 @@ require "json" +# IPC::JSON is the mother class for all exchanged messages (using JSON). +# IPC::JSON inherited classes have a common 'type' class attribute, +# which enables to find the right IPC::JSON+ class given a TypedMessage's type. + +# All transfered messages are typed messages. +# TypedMessage = u8 type (= IPC::JSON+ class type) + JSON content. +# 'JSON content' being a serialized IPC::JSON+ class. + +# Conventionally, IPC::JSON+ classes have a 'handle' method to process incoming messages. class IPC::JSON include ::JSON::Serializable @@ -22,12 +31,18 @@ class IPC::JSON end class IPC + # Schedule messages contained into IPC::JSON+. def schedule(fd : Int32, message : IPC::JSON) - schedule fd, message.type.to_u8, message.to_json + typed_msg = IPCMessage::TypedMessage.new message.type.to_u8, message.to_json + schedule fd, typed_msg + end + def write(fd : Int32, message : IPC::JSON) + typed_msg = IPCMessage::TypedMessage.new message.type.to_u8, message.to_json + write fd, typed_msg end end -# CAUTION: Only use this method on an Array(IPC::JSON.class) +# CAUTION: only use this method on an Array(IPC::JSON.class). class Array(T) def parse_ipc_json(message : IPCMessage::TypedMessage) : IPC::JSON? message_type = find &.type.==(message.type)