- Send the "goodbye" message.
- Set the wheel's FlushedEvent handler.
- Destroy the wheel in the FlushedEvent handler.
$heap->{wheel_client}->put("goodbye"); $heap->{wheel_client}->event(FlushedEvent => 'close_wheel_client'); sub close_wheel_client { my $heap = $_[HEAP]; delete $heap->{wheel_client}; }
If your protocol does not include a "goodbye" message:
- Check the return value of the wheel's get_driver_out_octets() method when requesting a shutdown.
- If it's zero, go ahead and destroy the wheel.
- If it's nonzero, set the wheel's FlushedEvent handler.
- Destroy the wheel in the FlushedEvent handler.
if ($heap->{wheel_client}->get_driver_out_octets() > 0) { $heap->{wheel_client}->event(FlushedEvent => 'close_wheel_client'); } else { delete $heap->{wheel_client}; } sub close_wheel_client { my $heap = $_[HEAP]; delete $heap->{wheel_client}; }
The second method will also work if you have goodbye messages, but get_driver_out_octets() will always be nonzero.
On the other hand, if put() ever flushes right away, the second method may be the only one that works reliably.