First I'd like to mention that you can have multiple alarms in POE, even in Windows. See POE::Kernel's documentation on the alarm() and delay() functions.
The server as it's written will create one ReadWrite wheel per client connection, and they're all stored in the same place: $heap->{readwrite}. In other words, each new connection will clobber the last. That's probably not what you want to do. :)
There are a few ways around this. First, each new ReadWrite wheel can be stored in a different $heap element. This stores each new wheel under its unique ID.
my $wheel = POE::Wheel::ReadWrite->new( ... );
$heap->{readwrite}->{$wheel->ID} = $wheel;
Every wheel event comes with a copy of the wheel's ID so you can tell which connection caused some activity. You might write your error handler like this:
sub agent_error {
my ($heap, $function, $error, $wheel_id) = @_[HEAP, ARG0, ARG2, ARG3
+];
print "SERVER: call to $function() failed: $error.\n";
print "Disconnecting agent!\n";
# Close the socket here...
delete $heap->{readwrite}->{$wheel_id};
}
Another way is to create a new Session instance for each client connection. This way is closest to fork() without really using fork: each connection has its own $heap to store things in.
If you're in the mood for a very high level approach, see the documentation for POE::Component::Server::TCP. You can find a very short Server::TCP example in this section of POE's cookbook.
-- Rocco Caputo / poe.perl.org / poe.sf.net |