[tpm] Fwd: delimiting/reading YAML documents across sockets
uri at stemsystems.com
Mon Jul 8 16:32:22 PDT 2013
On 07/08/2013 07:15 PM, Mike Stok wrote:
> Forgot to reply all. Might as well be ignorant in public
>> I'm trying to use YAML modules to send structures across
>> pipes from children to parent processes (or sockets).
>> Although the YAML docs talk about Dump() and Load()
>> I haven't been able to find (or figure out) an adequate
>> example of 'exchanging' the data, only encoding and decoding.
>> Although I can add the wrapper manually during transmission...
>> And, can all variables in a single exchange be sent within
>> the same document (example?) or do I have to send them
>> as individual documents within some kind of 'document of
>> document' wrapper? Surely this can't be as hard as it seems?
>> Suggestions, examples or references ?
>> [PS. The same questions would apply to anyone who suggests
>> JSON as an alternative.]
you should separate encoding from packeting. you can choose any encoding
and then wrap it in a common packet format for sending on the wire. what
i did years ago was to create a trivial packet header/trailer. the
header is one line with a size of the packet (only the packet, not
including the header/trailer) in ascii digits. the trailer isn't really
needed but i added a fixed line there. so generating this packet is
easy. reading isn't much harder. you read until you have at least one
line which will be the header. parse out the size and delete the header
or similar. then keep reading until you have size bytes read. read the
trailer if you have one. then decode the packet. it isn't much code on
either side. i did it with async (event loop) i/o so i kept a read
packet buffer and expected size around. when the buffer was larger than
the expected size, i knew i had a packet to decode. i kept trailing data
as it could be from the next packet.
if you wrap all this into a nice layer it becomes a simple send packet
or callback when a packet is read and decoded. assuming your
encode/decode can handle nested hashes/arrays, you can send most any
normal perl data. stay away from code and blessed objects as they can be
annoying and IMO shouldn't need to be send over the wire.
Uri Guttman - The Perl Hunter
The Best Perl Jobs, The Best Perl Hackers
More information about the toronto-pm