This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
2020-05-14 17:35:37 +02:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
require "./ipc.cr"
|
|
|
|
|
|
|
|
class IPC::JSON
|
|
|
|
include ::JSON::Serializable
|
|
|
|
|
|
|
|
@[::JSON::Field(ignored: true)]
|
|
|
|
getter type = -1
|
|
|
|
class_getter type = -1
|
|
|
|
|
|
|
|
property id : ::JSON::Any?
|
|
|
|
|
|
|
|
macro message(id, type, &block)
|
|
|
|
class {{id}} < ::IPC::JSON
|
|
|
|
include ::JSON::Serializable
|
|
|
|
|
|
|
|
@@type = {{type}}
|
|
|
|
def type
|
|
|
|
@@type
|
|
|
|
end
|
|
|
|
|
|
|
|
{{yield}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class IPC::Connection
|
|
|
|
def send(message : IPC::JSON)
|
|
|
|
send message.type.to_u8, message.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# CAUTION: Only use this method on an Array(IPC::JSON.class)
|
|
|
|
class Array(T)
|
2020-05-14 17:47:45 +02:00
|
|
|
def parse_ipc_json(message : IPC::Message) : IPC::JSON?
|
2020-05-14 17:35:37 +02:00
|
|
|
message_type = find &.type.==(message.utype)
|
|
|
|
|
|
|
|
payload = String.new message.payload
|
|
|
|
|
|
|
|
if message_type.nil?
|
|
|
|
raise "invalid message type (#{message.utype})"
|
|
|
|
end
|
|
|
|
|
|
|
|
message_type.from_json payload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|