dAmn Interoperability Project — READ-ONLY ARCHIVE

 [FrontPage]

1. Perl

These are taken from my Perl client, ../ClientPOE. --moeffju

When using a line-based socket, set the line delimiter to `\0' (NUL, ascii 0). Remember that you probably won't need to add another \0 to the end of your packet, or parse it out! (For example, when using POE::Filter::Line.)

sub dAmn_build_packet {
  my ($cmd, $param, $arg, @body) = @_;
  
  my $pkt = '';
  
  $pkt .= $cmd;
  $pkt .= " $param" if $param;
  $pkt .= "\n";
  $pkt .= "$arg\n" if $arg;
  $pkt .= "\n" . join("\n", @body) if @body;
  # you might not need this.
  $pkt .= "\000";
  
  return $pkt;
}
sub dAmn_parse_packet {
  my ($packet) = @_;

  my ($cmd, $param, $arg, @body, $channel);
  my @lines = split(/\n/, $packet);

  ($cmd, $param) = split(/ /, $lines[0]);

  $arg = $lines[1] if ($#lines >= 1);
  @body = @lines[2..$#lines] if ($#lines >= 2);

  # $packet->{channel} is only set for send and recv events, currently
  # You might want to move this somewhere else.
  if ($cmd =~ m/^(send|recv)$/) {
    (undef, $channel) = split(/:/, $param);
  }

  # This is just for debugging to stop perl from complaining about undefs.
  # You probably want to remove this.
  $param = '' unless $param;
  $arg = '' unless $arg;
  @body = () unless @body;
  $channel = '' unless $channel;
  
  return (cmd => $cmd, param => $param, arg => $arg, body => \@body, channel => $channel);
}

2013-03-28 12:59