ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Handle.pm
Revision: 1.1
Committed: Thu Aug 16 21:55:35 2001 UTC (22 years, 10 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Coro::Handle - non-blocking io with a blocking interface.
4    
5     =head1 SYNOPSIS
6    
7     use Coro::Handle;
8    
9     =head1 DESCRIPTION
10    
11     This module implements io-handles in a coroutine-compatible way, that is,
12     other coroutines can run while reads or writes block on the handle. It
13     does NOT inherit from IO::Handle but uses tied objects.
14    
15     =over 4
16    
17     =cut
18    
19     package Coro::Handle;
20    
21     use Errno ();
22     use base 'Exporter';
23    
24     $VERSION = 0.45;
25    
26     @EXPORT = qw(unblock);
27    
28     =item $fh = new_from_fh Coro::Handle $fhandle [, arg => value...]
29    
30     Create a new non-blocking io-handle using the given
31     perl-filehandle. Returns undef if no fhandle is given. The only other
32     supported argument is "timeout", which sets a timeout for each operation.
33    
34     =cut
35    
36     sub new_from_fh {
37     my $class = shift;
38     my $fh = shift or return;
39     my $self = do { local *Coro::Handle };
40    
41     my ($package, $filename, $line) = caller;
42     $filename =~ s/^.*[\/\\]//;
43    
44     tie $self, Coro::Handle::FH, fh => $fh, desc => "$filename:$line", @_;
45    
46     my $_fh = select bless \$self, $class; $| = 1; select $_fh;
47     }
48    
49     =item $fh = unblock $fh
50    
51     This is a convinience function that just calls C<new_from_fh> on the given
52     filehandle. Use it to replace a normal perl filehandle by a non-blocking
53     equivalent.
54    
55     =cut
56    
57     sub unblock($) {
58     new_from_fh Coro::Handle $_[0];
59     }
60    
61     sub read { read $_[0], $_[1], $_[2], $_[3] }
62     sub sysread { sysread $_[0], $_[1], $_[2], $_[3] }
63     sub syswrite { syswrite $_[0], $_[1], $_[2], $_[3] }
64    
65     =item $fh->writable, $fh->readable
66    
67     Wait until the filehandle is readable or writable (and return true) or
68     until an error condition happens (and return false).
69    
70     =cut
71    
72     sub readable { tied(${$_[0]})->readable }
73     sub writable { tied(${$_[0]})->writable }
74    
75     =item $fh->readline([$terminator])
76    
77     Like the builtin of the same name, but allows you to specify the input
78     record separator in a coroutine-safe manner (i.e. not using a global
79     variable).
80    
81     =cut
82    
83     sub readline { tied(${+shift})->READLINE(@_) }
84    
85     =item $fh->autoflush([...])
86    
87     Always returns true, arguments are being ignored (exists for compatibility
88     only). Might change in the future.
89    
90     =cut
91    
92     sub autoflush { !0 }
93    
94     =item $fh->fileno, $fh->close
95    
96     Work like their function equivalents.
97    
98     =cut
99    
100     sub fileno { tied(${+shift})->FILENO }
101     sub close { tied(${+shift})->CLOSE }
102    
103     =item $fh->timeout([...])
104    
105     The optional agrument sets the new timeout (in seconds) for this
106     handle. Returns the current (new) value.
107    
108     C<0> is a valid timeout, use C<undef> to disable the timeout.
109    
110     =cut
111    
112     sub timeout {
113     my $self = tied(${+shift});
114     if (@_) {
115     $self->{timeout} = $_[0];
116     $self->{rw}->timeout($_[0]) if $self->{rw};
117     $self->{ww}->timeout($_[0]) if $self->{ww};
118     }
119     $self->{timeout};
120     }
121    
122     package Coro::Handle::FH;
123    
124     use Fcntl ();
125     use Errno ();
126     use Carp 'croak';
127    
128     use Coro::Event;
129     use Event::Watcher qw(R W E);
130    
131     use base 'Tie::Handle';
132    
133     sub TIEHANDLE {
134     my $class = shift;
135    
136     my $self = bless {
137     rb => "",
138     wb => "",
139     @_,
140     }, $class;
141    
142     fcntl $self->{fh}, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK
143     or croak "fcntl(O_NONBLOCK): $!";
144    
145     $self;
146     }
147    
148     sub OPEN {
149     my $self = shift;
150     $self->CLOSE;
151     my $r = @_ == 2 ? open $self->{fh}, $_[0], $_[1]
152     : open $self->{fh}, $_[0], $_[1], $_[2];
153     if ($r) {
154     fcntl $self->{fh}, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK
155     or croak "fcntl(O_NONBLOCK): $!";
156     }
157     $r;
158     }
159    
160     sub CLOSE {
161     my $self = shift;
162     $self->{rb} =
163     $self->{wb} = "";
164     (delete $self->{rw})->cancel if exists $self->{rw};
165     (delete $self->{ww})->cancel if exists $self->{ww};
166     close $self->{fh};
167     }
168    
169     sub FILENO {
170     fileno $_[0]->{fh};
171     }
172    
173     sub writable {
174     ($_[0]->{ww} ||= Coro::Event->io(
175     fd => $_[0]->{fh},
176     desc => "$_[0]->{desc} WW",
177     timeout => $_[0]->{timeout},
178     poll => W+E,
179     ))->next->got & W;
180     }
181    
182     sub readable {
183     ($_[0]->{rw} ||= Coro::Event->io(
184     fd => $_[0]->{fh},
185     desc => "$_[0]->{desc} RW",
186     timeout => $_[0]->{timeout},
187     poll => R+E,
188     ))->next->got & R;
189     }
190    
191     sub WRITE {
192     my $self = $_[0];
193     my $len = defined $_[2] ? $_[2] : length $_[1];
194     my $ofs = $_[3];
195     my $res = 0;
196    
197     while() {
198     my $r = syswrite $self->{fh}, $_[1], $len, $ofs;
199     if (defined $r) {
200     $len -= $r;
201     $ofs += $r;
202     $res += $r;
203     last unless $len;
204     } elsif ($! != Errno::EAGAIN) {
205     last;
206     }
207     last unless $self->writable;
208     }
209    
210     return $res;
211     }
212    
213     sub READ {
214     my $self = $_[0];
215     my $len = $_[2];
216     my $ofs = $_[3];
217     my $res = 0;
218    
219     # first deplete the read buffer
220     if (exists $self->{rb}) {
221     my $l = length $self->{rb};
222     if ($l <= $len) {
223     substr($_[1], $ofs) = delete $self->{rb};
224     $len -= $l;
225     $res += $l;
226     return $res unless $len;
227     } else {
228     substr($_[1], $ofs) = substr($self->{rb}, 0, $len);
229     substr($self->{rb}, 0, $len) = "";
230     return $len;
231     }
232     }
233    
234     while() {
235     my $r = sysread $self->{fh}, $_[1], $len, $ofs;
236     if (defined $r) {
237     $len -= $r;
238     $ofs += $r;
239     $res += $r;
240     last unless $len && $r;
241     } elsif ($! != Errno::EAGAIN) {
242     last;
243     }
244     last unless $self->readable;
245     }
246    
247     return $res;
248     }
249    
250     sub READLINE {
251     my $self = shift;
252     my $irs = @_ ? shift : $/;
253    
254     while() {
255     my $pos = index $self->{rb}, $irs;
256     if ($pos >= 0) {
257     $pos += length $irs;
258     my $res = substr $self->{rb}, 0, $pos;
259     substr ($self->{rb}, 0, $pos) = "";
260     return $res;
261     }
262     my $r = sysread $self->{fh}, $self->{rb}, 8192, length $self->{rb};
263     if (defined $r) {
264     return undef unless $r;
265     } elsif ($! != Errno::EAGAIN || !$self->readable) {
266     return undef;
267     }
268     }
269     }
270    
271     sub DESTROY {
272     &CLOSE;
273     }
274    
275     1;
276    
277     =head1 BUGS
278    
279     - Perl's IO-Handle model is THE bug.
280    
281     =head1 AUTHOR
282    
283     Marc Lehmann <pcg@goof.com>
284     http://www.goof.com/pcg/marc/
285    
286     =cut
287