… | |
… | |
15 | =over 4 |
15 | =over 4 |
16 | |
16 | |
17 | =cut |
17 | =cut |
18 | |
18 | |
19 | package Coro::Handle; |
19 | package Coro::Handle; |
|
|
20 | |
|
|
21 | no warnings; |
20 | |
22 | |
21 | use Errno (); |
23 | use Errno (); |
22 | use base 'Exporter'; |
24 | use base 'Exporter'; |
23 | |
25 | |
24 | $VERSION = 0.45; |
26 | $VERSION = 0.45; |
… | |
… | |
56 | |
58 | |
57 | sub unblock($) { |
59 | sub unblock($) { |
58 | new_from_fh Coro::Handle $_[0]; |
60 | new_from_fh Coro::Handle $_[0]; |
59 | } |
61 | } |
60 | |
62 | |
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 |
63 | =item $fh->writable, $fh->readable |
66 | |
64 | |
67 | Wait until the filehandle is readable or writable (and return true) or |
65 | Wait until the filehandle is readable or writable (and return true) or |
68 | until an error condition happens (and return false). |
66 | until an error condition happens (and return false). |
69 | |
67 | |
… | |
… | |
89 | |
87 | |
90 | =cut |
88 | =cut |
91 | |
89 | |
92 | sub autoflush { !0 } |
90 | sub autoflush { !0 } |
93 | |
91 | |
94 | =item $fh->fileno, $fh->close |
92 | =item $fh->fileno, $fh->close, |
|
|
93 | $fh->read, $fh->sysread, $fh->syswrite, |
|
|
94 | $fh->print, $fh->printf |
95 | |
95 | |
96 | Work like their function equivalents. |
96 | Work like their function equivalents (except read, which works like |
|
|
97 | sysread. You should not use the read function with Coro::Handles, it will |
|
|
98 | work but it's not efficient). |
97 | |
99 | |
98 | =cut |
100 | =cut |
99 | |
101 | |
100 | sub fileno { tied(${$_[0]})->FILENO } |
102 | sub read { Coro::Handle::FH::READ (tied ${$_[0]}, $_[1], $_[2], $_[3]) } |
101 | sub close { tied(${$_[0]})->CLOSE } |
103 | sub sysread { Coro::Handle::FH::READ (tied ${$_[0]}, $_[1], $_[2], $_[3]) } |
|
|
104 | sub syswrite { Coro::Handle::FH::WRITE (tied ${$_[0]}, $_[1], $_[2], $_[3]) } |
|
|
105 | sub print { Coro::Handle::FH::WRITE (tied ${+shift}, join "", @_) } |
|
|
106 | sub printf { Coro::Handle::FH::PRINTF(tied ${+shift}, @_) } |
|
|
107 | sub fileno { Coro::Handle::FH::FILENO(tied ${$_[0]}) } |
|
|
108 | sub close { Coro::Handle::FH::CLOSE (tied ${$_[0]}) } |
102 | |
109 | |
103 | =item $fh->timeout([...]) |
110 | =item $fh->timeout([...]) |
104 | |
111 | |
105 | The optional agrument sets the new timeout (in seconds) for this |
112 | The optional agrument sets the new timeout (in seconds) for this |
106 | handle. Returns the current (new) value. |
113 | handle. Returns the current (new) value. |
… | |
… | |
131 | tied(${$_[0]})->{fh}; |
138 | tied(${$_[0]})->{fh}; |
132 | } |
139 | } |
133 | |
140 | |
134 | package Coro::Handle::FH; |
141 | package Coro::Handle::FH; |
135 | |
142 | |
|
|
143 | no warnings; |
|
|
144 | |
136 | use Fcntl (); |
145 | use Fcntl (); |
137 | use Errno (); |
146 | use Errno (); |
138 | use Carp 'croak'; |
147 | use Carp 'croak'; |
139 | |
148 | |
140 | use Coro::Event; |
149 | use Coro::Event; |
141 | use Event::Watcher qw(R W E); |
150 | use Event::Watcher qw(R W E); |
142 | |
|
|
143 | use base 'Tie::Handle'; |
|
|
144 | |
151 | |
145 | # formerly a hash, but we are speed-critical, so try |
152 | # formerly a hash, but we are speed-critical, so try |
146 | # to be faster even if it hurts. |
153 | # to be faster even if it hurts. |
147 | # |
154 | # |
148 | # 0 FH |
155 | # 0 FH |
… | |
… | |
186 | if ($r) { |
193 | if ($r) { |
187 | fcntl $self->[0], &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK |
194 | fcntl $self->[0], &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK |
188 | or croak "fcntl(O_NONBLOCK): $!"; |
195 | or croak "fcntl(O_NONBLOCK): $!"; |
189 | } |
196 | } |
190 | $r; |
197 | $r; |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub PRINT { |
|
|
201 | WRITE($_[0], $_[1]); |
|
|
202 | } |
|
|
203 | |
|
|
204 | sub PRINTF { |
|
|
205 | WRITE(shift, sprintf(shift,@_)); |
|
|
206 | } |
|
|
207 | |
|
|
208 | sub GETC { |
|
|
209 | my $buf; |
|
|
210 | READ($_[0], $buf, 1); |
|
|
211 | $buf; |
|
|
212 | } |
|
|
213 | |
|
|
214 | sub BINMODE { |
|
|
215 | binmode $_[0][0]; |
|
|
216 | } |
|
|
217 | |
|
|
218 | sub TELL { |
|
|
219 | use Carp (); Carp::croak("Coro::Handle's don't support tell()"); |
|
|
220 | } |
|
|
221 | |
|
|
222 | sub SEEK { |
|
|
223 | use Carp (); Carp::croak("Coro::Handle's don't support seek()"); |
|
|
224 | } |
|
|
225 | |
|
|
226 | sub EOF { |
|
|
227 | use Carp (); Carp::croak("Coro::Handle's don't support eof()"); |
191 | } |
228 | } |
192 | |
229 | |
193 | sub CLOSE { |
230 | sub CLOSE { |
194 | &cleanup; |
231 | &cleanup; |
195 | close $_[0][0]; |
232 | close $_[0][0]; |