+my $packet_type = {
+ JOB_CREATED => 8,
+
+ WORK_COMPLETE => 13,
+
+ ECHO_REQ => 16,
+ ECHO_RES => 17,
+
+ ERROR => 19,
+};
+
+my $nr2type;
+$nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
+
+
+sub parse_packet {
+ my ($self,$data) = @_;
+ my ($magic, $type, $len) = unpack( "a4NN", $data );
+ die "wrong magic [$magic]" unless $magic eq "\0RES";
+ die "unsupported type [$type]" unless exists $nr2type->{$type};
+ die "ERROR" if $type == $packet_type->{ERROR};
+ return ( $type, split("\0", substr($data,12,$len)) );
+}
+
+sub res {
+ my ( $self, $value ) = @_;
+ if ( defined $value ) {
+ warn "# ++ res = ",dump $value;
+ $self->{_res} ||= $value;
+ }
+ return $self->{_res};
+}
+