Skip to content

Commit

Permalink
switch using PerlIO or not by env var
Browse files Browse the repository at this point in the history
  • Loading branch information
hirose31 committed Sep 25, 2009
1 parent 13fb339 commit 48ae7c6
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions echo-epoll.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.