clj-commons / gloss

speaks in bytes, so you don't have to
Eclipse Public License 1.0
483 stars 57 forks source link

Type depending on more fields. #38

Open Nutelac opened 9 years ago

Nutelac commented 9 years ago

I implementing protocol, which has multiple byte codes for one type. For example digital message may have codes in range of 0x90 - 0x9F. Naive function for generating that type can looks like this:

(defn digital-message
  [port]
  (bit-or 0x90 port))

I would like to create encoders and decoders which looks like that: decoder:

(decode-all decoder (to-byte-buffer [0x91 0x05 0x98 0x06])) ; [{:type :digital-message :port 1 :some-data 5}
                                                            ;  {:type :digital-message :port 8 :some-data 6}]

encoder:

(encode encoder {:type :digital-message :port 2 :some-data 5}) ; [0x92 0x05]

Unfortunaly, I don't know how to implement it and combine with gloss/header.

Thanks for any response.