NAME

Protocol::ControlChannel - simple binary protocol for exchanging key/value pairs

VERSION

version 0.003

SYNOPSIS

my $cc = Protocol::ControlChannel->new;
my $data = $cc->create_frame(key => 'value');
my $frame = $cc->extract_frame(\$data);
print 'Key: ' . $frame->{key} . ', value ' . $frame->{value} . "\n";

DESCRIPTION

This is the abstract implementation for a wire protocol which can be used to exchange data between two endpoints, as a series of key/value pairs.

Typical use-case is for passing events between remote processes/hosts.

The data packet looks like this:

  • packet_length - 32-bit network-order unsigned int - excludes the length field itself

  • type - 16-bit network-order unsigned int - defines the type of this message, typically describes the format of the value field

  • name_length - 16-bit network-order unsigned int - length of the name field

  • UTF-8 encoded name information - no null terminator

  • remaining bytes are value information, content depends on 'type'

Types are currently 0 for 'plain text', 1 for 'Storable::nfreeze'. It's quite possible that Sereal support will be added soon.

Usage is simple: instantiate, call methods, if anything returns undef then things have gone wrong so you're advised to terminate that session. If you're exchanging packets via UDP then this may not be so simple.

Note that content is either Perl data structures (i.e. a reference), or byte data. If you have a string, you'll need to pick a suitable encoding and decoding - probably UTF-8.

METHODS

new

Instantiate an object. Not technically necessary, since all the other methods could just as well be class methods for the moment, but in future this is likely to change.

extract_frame

Given a scalar ref to a byte buffer, will attempt to extract the next frame.

If a full, valid frame was found, it will be decoded, removed from the buffer, and returned as a hashref.

If not, you get undef.

If something went wrong, you'll probably get undef at the moment. In future this may raise an exception.

create_frame

Creates a frame. Takes a key => value pair, and returns them in packet form.

Key must be something that can be utf8-encoded - so 'a perl string', or an object that stringifies sanely.

SEE ALSO

Any of the other protocols that look very similar to this one...

AUTHOR

Tom Molesworth <cpan@entitymodel.com>

LICENSE

Copyright Tom Molesworth 2012-2013. Licensed under the same terms as Perl itself.