Obsolete
/
ipc.cr-old
Archived
3
0
Fork 0

CBOR messages.

master
Karchnu 2020-12-02 16:23:27 +01:00
parent 62d69388ee
commit 21334a4d0a
1 changed files with 27 additions and 3 deletions

View File

@ -1,3 +1,4 @@
require "cbor"
require "json"
# JSON is currently used for messages over websockets
@ -9,6 +10,8 @@ class IPC::Message
property utype : UInt8 # libipc user message type
property payload : Bytes
# Clients send and receive JSON (or CBOR) payloads.
struct JSONMessage
include JSON::Serializable
@ -20,16 +23,37 @@ class IPC::Message
end
end
def self.from_json (str : String)
def self.from_json (str : String) : IPC::Message
jsonmessage = JSONMessage.from_json str
IPC::Message.new 0, jsonmessage.mtype, jsonmessage.utype, jsonmessage.payload
end
def to_json
def to_json : String
JSONMessage.new(@utype, String.new(@payload), @mtype).to_json
end
struct CBORMessage
include CBOR::Serializable
property mtype : UInt8 = 1 # libipc message type
property utype : UInt8 # libipc user message type
property payload : Bytes
def initialize(@utype, @payload, @mtype = 1)
end
end
def self.from_cbor (m : Bytes) : IPC::Message
cbor_message = CBORMessage.from_cbor m
IPC::Message.new 0, cbor_message.mtype, cbor_message.utype, cbor_message.payload
end
def to_cbor : Bytes
CBORMessage.new(@utype, @payload, @mtype).to_cbor
end
def initialize(message : Pointer(LibIPC::Message))
if message.null?
@mtype = LibIPC::MessageType::Error.to_u8