1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::Impl::POE - AnyEvent adaptor for POE |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use AnyEvent; |
8 |
use EV; |
9 |
|
10 |
# this module gets loaded automatically as required |
11 |
|
12 |
=head1 DESCRIPTION |
13 |
|
14 |
This module provides transparent support for AnyEvent. You don't have to |
15 |
do anything to make POE work with AnyEvent except by loading POE before |
16 |
creating the first AnyEvent watcher. |
17 |
|
18 |
- one session per event |
19 |
- messages output |
20 |
- time vs. gettimeofday |
21 |
- bad documentation, missing etc. |
22 |
- timeout order |
23 |
- no support for child watchers |
24 |
|
25 |
Terminal signals will kill sessions if they are not handled by a "sig_handled"() call. The OS signals that usually kill or dump a process are con‐ |
26 |
sidered terminal in POE, but they never trigger a coredump. These are: HUP, INT, QUIT and TERM. |
27 |
|
28 |
|
29 |
=cut |
30 |
|
31 |
package AnyEvent::Impl::POE; |
32 |
|
33 |
no warnings; |
34 |
use strict; |
35 |
|
36 |
use POE; |
37 |
|
38 |
# have to do this to keep POE from spilling ugly messages |
39 |
POE::Session->create (inline_states => { _start => sub { @_[KERNEL]->stop } }); |
40 |
POE::Kernel->run; |
41 |
|
42 |
sub io { |
43 |
my ($class, %arg) = @_; |
44 |
my $poll = delete $arg{poll}; |
45 |
my $cb = delete $arg{cb}; |
46 |
my $fh = delete $arg{fh}; |
47 |
my $id; |
48 |
my $session = POE::Session->create ( |
49 |
inline_states => { |
50 |
_start => sub { |
51 |
$poll eq "r" ? $_[KERNEL]->select_read ($fh => "ready") |
52 |
: $_[KERNEL]->select_write ($fh => "ready") |
53 |
}, |
54 |
ready => sub { |
55 |
$cb->(); |
56 |
}, |
57 |
stop => sub { |
58 |
$poll eq "r" ? $_[KERNEL]->select_read ($fh) |
59 |
: $_[KERNEL]->select_read ($fh) |
60 |
}, |
61 |
}, |
62 |
); |
63 |
bless \\$session, AnyEvent::Impl::POE:: |
64 |
} |
65 |
|
66 |
sub timer { |
67 |
my ($class, %arg) = @_; |
68 |
my $after = delete $arg{after}; |
69 |
my $cb = delete $arg{cb}; |
70 |
my $session = POE::Session->create ( |
71 |
inline_states => { |
72 |
_start => sub { |
73 |
$_[KERNEL]->delay_set (timeout => $after); |
74 |
}, |
75 |
timeout => sub { |
76 |
$cb->(); |
77 |
}, |
78 |
stop => sub { |
79 |
$_[KERNEL]->alarm_remove_all; |
80 |
}, |
81 |
}, |
82 |
); |
83 |
bless \\$session, AnyEvent::Impl::POE:: |
84 |
} |
85 |
|
86 |
sub signal { |
87 |
my ($class, %arg) = @_; |
88 |
my $signal = delete $arg{signal}; |
89 |
my $cb = delete $arg{cb}; |
90 |
my $session = POE::Session->create ( |
91 |
inline_states => { |
92 |
_start => sub { |
93 |
$_[KERNEL]->sig ($signal => "catch"); |
94 |
$_[KERNEL]->refcount_increment ($_[SESSION]->ID => "poe"); |
95 |
}, |
96 |
catch => sub { |
97 |
$cb->(); |
98 |
$_[KERNEL]->sig_handled; |
99 |
}, |
100 |
stop => sub { |
101 |
$_[KERNEL]->refcount_decrement ($_[SESSION]->ID => "poe"); |
102 |
$_[KERNEL]->sig ($signal); |
103 |
}, |
104 |
}, |
105 |
); |
106 |
bless \\$session, AnyEvent::Impl::POE:: |
107 |
} |
108 |
|
109 |
sub DESTROY { |
110 |
POE::Kernel->post (${${$_[0]}}, "stop"); |
111 |
} |
112 |
|
113 |
sub one_event { |
114 |
POE::Kernel->loop_do_timeslice; |
115 |
} |
116 |
|
117 |
1; |
118 |
|
119 |
=head1 SEE ALSO |
120 |
|
121 |
L<AnyEvent>, L<POE>. |
122 |
|
123 |
=head1 AUTHOR |
124 |
|
125 |
Marc Lehmann <schmorp@schmorp.de> |
126 |
http://home.schmorp.de/ |
127 |
|
128 |
=cut |
129 |
|