| 1 | #----------------------------------------------------------------------- | ||
| 2 | # Copyright (C) 2005-2015 by Jörn Reder <joern AT zyn.de>. | ||
| 3 | # All Rights Reserved. See file COPYRIGHT for details. | ||
| 4 | # | ||
| 5 | # This module is part of Event::RPC, which is free software; you can | ||
| 6 | # redistribute it and/or modify it under the same terms as Perl itself. | ||
| 7 | #----------------------------------------------------------------------- | ||
| 8 | |||
| 9 | package Event::RPC::Loop::Event; | ||
| 10 | |||
| 11 | use base qw( Event::RPC::Loop ); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use utf8; | ||
| 15 | |||
| 16 | use Event; | ||
| 17 | |||
| 18 | sub add_io_watcher { | ||
| 19 | my $self = shift; | ||
| 20 | my %par = @_; | ||
| 21 | my ($fh, $cb, $desc, $poll) = @par{'fh','cb','desc','poll'}; | ||
| 22 | |||
| 23 | return Event->io ( | ||
| 24 | fd => $fh, | ||
| 25 | poll => $poll, | ||
| 26 | cb => $cb, | ||
| 27 | desc => $desc, | ||
| 28 | reentrant => 0, | ||
| 29 | parked => 0, | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub del_io_watcher { | ||
| 34 | my $self = shift; | ||
| 35 | my ($watcher) = @_; | ||
| 36 | |||
| 37 | $watcher->cancel; | ||
| 38 | |||
| 39 | 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub add_timer { | ||
| 43 | my $self = shift; | ||
| 44 | my %par = @_; | ||
| 45 | my ($interval, $after, $cb, $desc) = | ||
| 46 | @par{'interval','after','cb','desc'}; | ||
| 47 | |||
| 48 | die "interval and after can't be used together" | ||
| 49 | if $interval && $after; | ||
| 50 | |||
| 51 | return Event->timer ( | ||
| 52 | interval => $interval, | ||
| 53 | after => $after, | ||
| 54 | cb => $cb, | ||
| 55 | desc => $desc, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub del_timer { | ||
| 60 | my $self = shift; | ||
| 61 | my ($timer) = @_; | ||
| 62 | |||
| 63 | $timer->cancel; | ||
| 64 | |||
| 65 | 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub enter { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | Event::loop(); | ||
| 72 | |||
| 73 | 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub leave { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | Event::unloop_all("ok"); | ||
| 80 | |||
| 81 | 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; | ||
| 85 | |||
| 86 | __END__ |