Compare commits
No commits in common. "master" and "cbor" have entirely different histories.
10
src/cbor.cr
10
src/cbor.cr
|
@ -5,20 +5,20 @@ require "./ipc.cr"
|
|||
class IPC::CBOR
|
||||
include ::CBOR::Serializable
|
||||
|
||||
#@[::CBOR::Field(ignored: true)]
|
||||
#getter type = -1
|
||||
@[::CBOR::Field(ignored: true)]
|
||||
getter type = -1
|
||||
class_getter type = -1
|
||||
|
||||
property id : ::CBOR::Any?
|
||||
def type
|
||||
@@type
|
||||
end
|
||||
|
||||
macro message(id, type, &block)
|
||||
class {{id}} < ::IPC::CBOR
|
||||
include ::CBOR::Serializable
|
||||
|
||||
@@type = {{type}}
|
||||
def type
|
||||
@@type
|
||||
end
|
||||
|
||||
{{yield}}
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
|
||||
class IPC::Context
|
||||
# TODO: FIXME: base timer default is 30 seconds, INFTIM was causing trouble.
|
||||
property base_timer : Int32 = 30_000 # LibIPC::INFTIM
|
||||
property timer : Int32 = 30_000 # LibIPC::INFTIM
|
||||
property base_timer : Int32 = LibIPC::INFTIM
|
||||
property timer : Int32 = LibIPC::INFTIM
|
||||
getter context : LibIPC::Ctx
|
||||
|
||||
# On message reception, the message is contained into the event structure.
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
require "cbor"
|
||||
require "json"
|
||||
|
||||
# JSON is currently used for messages over websockets
|
||||
|
@ -10,8 +9,6 @@ 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
|
||||
|
||||
|
@ -23,37 +20,16 @@ class IPC::Message
|
|||
end
|
||||
end
|
||||
|
||||
def self.from_json (str : String) : IPC::Message
|
||||
def self.from_json (str : String)
|
||||
jsonmessage = JSONMessage.from_json str
|
||||
|
||||
IPC::Message.new 0, jsonmessage.mtype, jsonmessage.utype, jsonmessage.payload
|
||||
end
|
||||
|
||||
def to_json : String
|
||||
def to_json
|
||||
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
|
||||
|
|
12
src/json.cr
12
src/json.cr
|
@ -5,20 +5,20 @@ require "./ipc.cr"
|
|||
class IPC::JSON
|
||||
include ::JSON::Serializable
|
||||
|
||||
#@[::JSON::Field(ignored: true)]
|
||||
class_property type = -1
|
||||
@[::JSON::Field(ignored: true)]
|
||||
getter type = -1
|
||||
class_getter type = -1
|
||||
|
||||
property id : ::JSON::Any?
|
||||
|
||||
def type
|
||||
@@type
|
||||
end
|
||||
|
||||
macro message(id, type, &block)
|
||||
class {{id}} < ::IPC::JSON
|
||||
include ::JSON::Serializable
|
||||
|
||||
@@type = {{type}}
|
||||
def type
|
||||
@@type
|
||||
end
|
||||
|
||||
{{yield}}
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue