http://www.perlmonks.org?node_id=177636


in reply to Re: Re: Re: Re: How do I close a POE SocketFactory Socket?
in thread How do I close a POE SocketFactory Socket?

If I understand correctly, you want the server to start talking first. That's easy, really. POE::Wheel::ReadWrite instances are ready to be used immediately.

sub agent_handler { # ... my $wheel = new POE::Wheel::ReadWrite(...); $heap->{wheel}->{$wheel->ID} = $wheel; $wheel->put("Greetings from MyServer version 1.0"); # ... }

POE can be used to write single-tasking programs that block, but network servers don't usually don't fall into this category. It's possible to block temporarily, though, and short blockages might go unnoticed. (Actually, a POE program is just a series of very short blocking operations. They usually occur quickly enough that people don't notice.)

The heap is reserved almost entirely for your storage. Its main purpose is to store persistent information between events. If I understand your question, this is exactly what you want to do.

If you decide to keep copies of socket handles in the heap, be sure to delete them when you're done with them. Otherwise your server will leak memory and file handles, and it will crash in short order.

sub agent_handler { # ... $heap->{wheel}->{$wheel->ID} = $wheel; $heap->{socket}->{$wheel->ID} = $socket; # ... } sub agent_error { # ... delete $heap->{wheel}->{$wheel_id}; delete $heap->{socket}->{$wheel_id}; }

If at all possible, however, you should use $wheel->put() to send data. Your $socket is in non-blocking mode, and all the standard caveats about print() and <> apply.

More notes.

If you're going to store a lot of things in the heap for each wheel, consider turning things inside out.

$heap->{$wheel->ID} = { wheel => $wheel, socket => $socket, foo => $foo, };

Then you can delete it all at once.

delete $heap->{$wheel_id};

Interview? I get this sort of Q&A all the time. :)

-- Rocco Caputo / poe.perl.org / poe.sf.net