> On Mon, Feb 09, 2004 at 10:56:25AM -0600, Jay Strauss wrote:
> > Hi,
> >
> > I'd like rewrite some forking code to use POE instead.
> >
> > I must connect to a server process via a tcp socket. The messages I
> > receive, are not terminated, but the individual fields are terminated by
an
> > ASCII 0.
> >
> > The first field received (message type), then tells me how many more
fields
> > I must read to get the entire message.
> >
> > In my current code I do it sorta like:
> >
> > while (1) {
> > my $msgId = $self->receive;
> > if ($msgId == TICKPRICE) {$self->readTickPriceMessage}
> > ...
> > }
> >
> > sub readTickPriceMessage {
> > my $self = shift;
> > my @field = $self->receive() for (qw/tickerid tickType price/);
> > }
> >
> > sub receive {
> > my $self = shift;
> > my $s = $self->socket;
> >
> > my ($result,$byte);
> >
> > while (sysread($s, $byte, 1) == 1) {
> > last if ord($byte) == 0;
> > $result .= $byte;
> > }
> > return $result;
> > }
> >
> >
>
> I would use POE::Filter::Line with the literal line terminator "\0".
> That will return to you a stream of parsed fields. It's then up to you
> to decide how many constitute a complete record. You could build a list
> of fields until @record == $record_length{$record[0]} or something. At
> that point, you call whatever you've written to handle the record.
>
> --
> Roccco aputo - [EMAIL PROTECTED] - http://poe.perl.org/
Rocco,
I did it the way you said (e.g. with a Wheel::ReadWrite & POE::Filter::Line)
and that works. The initial part of the conversation goes:
connect
send clientVersion number
receive serverVersion number
send clientID
For grins (cause David suggested it) I tried with Component::Client::TCP,
but now it doesn't seem to call serverInput. I can't see my problem. Can
someone point to my error?
Thanks
Jay
#!/usr/bin/perl
use strict;
use warnings;
use POE qw(Component::Client::TCP);
$|++;
POE::Component::Client::TCP->new(
RemoteAddress => "localhost",
Filter => [ "POE::Filter::Line", InputLiteral => "\0" ],
RemotePort => 11001,
Connected => \&connected,
ConnectError => \&connectError,
Disconnected => \&disconnected,
ServerInput => sub {
my $input = $_[ARG0];
print "from server: $input\n";
},
ServerError => \&serverError,
);
$poe_kernel->run();
sub connected {
my ($heap) = @_[HEAP, ARG0];
print "connected\n";
my $clientVersion = 10;
$heap->{server}->put($clientVersion);
}