Source code: https://gitlab.com/HiPhish/MsgPack.rkt
When two processes want to exchgange data they need to agree on a protocol for serialising and de-serialising said data. MessagePack is a protocol designed with speed and size in mind; this means that serialised data should be as small as possible and the recipient should be able to de-serialise the data as quickly as possible. The flipside of this is that the protocol is not easily readable to humans, but this is not a concern if the data is only meant to be exchanged beetween processes anyway.
The API of this implementation follows the naming conventions of MessagePack: we call the serialising process packing and the de-serialising process unpacking. Integers have different length and can be either signed (intN) or unsigned (uintN), where N is the length of the integer in bytes.
MessagePack objects are converted to Racket objects and vice-versa according to the following rules:
positive-fixint, negative-fixint, uint8, uint16, uint32, uint64, int8, int16, int32, int64
fixstr, str8, str16, str32
bin8, bin16, bin32
fixarray, array16, array32
fixmap, map16, map32
fixext1, fixext2, fixext4, fixext8, fixext16, ext8, ext16, ext32
When there is more than one Racket type listed unpack will return a value of the first type listed.
Here we have an object in Racket which we wish to pack. The object is a vector of various other packable Racket objects. Objects are packed to ports, usually these ports point to files or network connections, but here we will use byte strings as ports for the sake of simplicity.
; Import the library first > (require msgpack) ; Here is some data we want to pack: a vector of numbers, ; nothing, another vector and a string. > (define hodgepodge (vector 1 2 (void) '#(3 #t) "foo")) ; Use a byte string as the output port > (define packed (call-with-output-bytes (λ (out) (pack hodgepodge out)))) ; The entire hodgepodge has now been packed to binary data > packed
; If we want our original hodgepodge back we need to unpack it > (define unpacked (call-with-input-bytes packed (λ (in) (unpack in)))) > unpacked
'#(1 2 #<void> #(3 #t) "foo")
Packing and unpacking are the primitive operations associated with the MessagePack format, more complex tasks like sending and receiving RPC messages can then be implemented on top of this library.
|(require msgpack/packable)||package: msgpack|
|(require msgpack/ext)||package: msgpack|
|(require msgpack/pack)||package: msgpack|
|(require msgpack/unpack)||package: msgpack|