Permalink
Browse files

switch using PerlIO or not by env var

  • Loading branch information...
1 parent 13fb339 commit 48ae7c66388451132000b8768d594e60d09fdc05 @hirose31 hirose31 committed Sep 25, 2009
Showing with 48 additions and 16 deletions.
  1. +48 −16 echo-epoll.pl
View
@@ -32,6 +32,52 @@
epoll_ctl($epfd, EPOLL_CTL_ADD, $listener_fd, EPOLLIN) >= 0
|| die "epoll_ctl: $!\n";
+sub with_sysread {
+ my($ev) = @_;
+ open my $sock, "+<&=".$ev->[0] or die "fdopen: $!";
+
+ my $buf = "";
+ my $r = sysread $sock, $buf, 24;
+ #warn "[$r] <$buf> $ev->[0]\n";
+ if ($r) {
+ syswrite $sock, $buf, $r;
+ } else {
+ ### no data: $ev->[0], $$
+ if (!defined $r && ($! == EINTR || $! == EAGAIN)) {
+ next;
+ }
+ epoll_ctl($epfd, EPOLL_CTL_DEL, $ev->[0], 0) >= 0
+ || die "epoll_ctl: $!\n";
+ $Sock_Holder[$ev->[0]] = undef;
+ close $sock;
+ }
+}
+
+sub with_perlio {
+ my($ev) = @_;
+ open my $sock, "+<&=".$ev->[0] or die "fdopen: $!";
+
+ my $buf = <$sock>;
+ #warn "<$buf> $ev->[0]\n";
+ if ($buf) {
+ print $sock $buf;
+ } else {
+ ### no data: $ev->[0], $$
+ epoll_ctl($epfd, EPOLL_CTL_DEL, $ev->[0], 0) >= 0
+ || die "epoll_ctl: $!\n";
+ $Sock_Holder[$ev->[0]] = undef;
+ close $sock;
+ }
+}
+
+if ($ENV{USE_PERLIO}) {
+ print "use Perl IO\n";
+ *process_connection = \&with_perlio;
+} else {
+ print "use sysread, syswrite\n";
+ *process_connection = \&with_sysread;
+}
+
while (1) {
my $events = epoll_wait($epfd, $concurrent, -1); # Max 10 events returned, 1s timeout
@@ -49,22 +95,8 @@
|| die "epoll_ctl: $!\n";
} else {
### >client: $ev->[0], $$
- open my $sock, "+<&=".$ev->[0] or die "fdopen: $!";
- my $buf = "";
- my $r = sysread $sock, $buf, 24;
- #warn "[$r] <$buf> $ev->[0]\n";
- if ($r) {
- syswrite $sock, $buf, $r;
- } else {
- ### no data: $ev->[0], $$
- if (!defined $r && ($! == EINTR || $! == EAGAIN)) {
- next;
- }
- epoll_ctl($epfd, EPOLL_CTL_DEL, $ev->[0], 0) >= 0
- || die "epoll_ctl: $!\n";
- $Sock_Holder[$ev->[0]] = undef;
- close $sock;
- }
+ process_connection($ev);
}
}
}
+

0 comments on commit 48ae7c6

Please sign in to comment.