Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:tokuhirom/perl-echo-servers
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Sep 27, 2009
2 parents 66db46e + 48ae7c6 commit 8f14208
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
8 changes: 5 additions & 3 deletions dump-env.pl
@@ -1,7 +1,9 @@
use Class::MOP;
use Config;

print "perl: $]\n";
print $^O, "\n";
print join(' ', $Config{osname}, $Config{osvers}, $Config{archname}), "\n";
print "useithreads: ", $Config{useithreads} ? "yes" : "no", "\n";
print "\n";

d($_) for qw/
Expand All @@ -20,7 +22,7 @@

sub d {
my $c = shift;
Class::MOP::load_class($c);
print "$c: " . ${"$c\::VERSION"}, "\n";
eval { Class::MOP::load_class($c); };
printf "%-22s: %s\n", $c, ($@ ? "MISSING" : ${"$c\::VERSION"});
}

1 change: 1 addition & 0 deletions echo-ae+mp.pl
@@ -1,5 +1,6 @@
use strict;
use warnings;
use AnyEvent::Impl::EV;
use AE;
use Getopt::Long;
use AnyEvent::Socket;
Expand Down
60 changes: 49 additions & 11 deletions echo-epoll.pl
Expand Up @@ -4,6 +4,7 @@
use IO::Socket::INET;
use IO::Epoll;
use Fcntl;
use POSIX qw(:errno_h);

my $concurrent = 10; # max event
my $port = 9010;
Expand Down Expand Up @@ -31,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 @@ -48,17 +95,8 @@
|| die "epoll_ctl: $!\n";
} else {
### >client: $ev->[0], $$
open my $sock, "+<&=".$ev->[0] or die "fdopen: $!";
my $line = <$sock>;
if ($line) {
print $sock $line;
} 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;
}
process_connection($ev);
}
}
}

0 comments on commit 8f14208

Please sign in to comment.