CBOR messages.
This commit is contained in:
parent
62d69388ee
commit
21334a4d0a
@ -1,3 +1,4 @@
|
|||||||
|
require "cbor"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
# JSON is currently used for messages over websockets
|
# JSON is currently used for messages over websockets
|
||||||
@ -9,6 +10,8 @@ class IPC::Message
|
|||||||
property utype : UInt8 # libipc user message type
|
property utype : UInt8 # libipc user message type
|
||||||
property payload : Bytes
|
property payload : Bytes
|
||||||
|
|
||||||
|
|
||||||
|
# Clients send and receive JSON (or CBOR) payloads.
|
||||||
struct JSONMessage
|
struct JSONMessage
|
||||||
include JSON::Serializable
|
include JSON::Serializable
|
||||||
|
|
||||||
@ -20,16 +23,37 @@ class IPC::Message
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_json (str : String)
|
def self.from_json (str : String) : IPC::Message
|
||||||
jsonmessage = JSONMessage.from_json str
|
jsonmessage = JSONMessage.from_json str
|
||||||
|
|
||||||
IPC::Message.new 0, jsonmessage.mtype, jsonmessage.utype, jsonmessage.payload
|
IPC::Message.new 0, jsonmessage.mtype, jsonmessage.utype, jsonmessage.payload
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json
|
def to_json : String
|
||||||
JSONMessage.new(@utype, String.new(@payload), @mtype).to_json
|
JSONMessage.new(@utype, String.new(@payload), @mtype).to_json
|
||||||
end
|
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))
|
def initialize(message : Pointer(LibIPC::Message))
|
||||||
if message.null?
|
if message.null?
|
||||||
@mtype = LibIPC::MessageType::Error.to_u8
|
@mtype = LibIPC::MessageType::Error.to_u8
|
||||||
|
Reference in New Issue
Block a user