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; > } > > > But I'm trying to figure out how I'd do it with POE::Wheel::SocketFactory + > POE::Wheel::ReadWrite. It seems like I'm only going to get an event on the > initial connection. How would I get it to sit and listen on the socket for > messages? Furthermore, how would I get it to read a specific number of > fields based on the message ID?
SocketFactory just creates sockets. In the client context, it performs the socket creation and connection, returning a socket to you when it's established (or an error event if there was a problem). At that point you delete the SocketFactory wheel and create a ReadWrite wheel to perform buffered I/O on the socket. 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/