Permalink
Browse files

Merge branch 'master' of git@github.com:tokuhirom/perl-echo-servers

  • Loading branch information...
2 parents 66db46e + 48ae7c6 commit 8f14208d5e8facf1f7125d7e23eda17eebe7ad32 @tokuhirom committed Sep 27, 2009
Showing with 55 additions and 14 deletions.
  1. +5 −3 dump-env.pl
  2. +1 −0 echo-ae+mp.pl
  3. +49 −11 echo-epoll.pl
View
@@ -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/
@@ -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"});
}
View
@@ -1,5 +1,6 @@
use strict;
use warnings;
+use AnyEvent::Impl::EV;
use AE;
use Getopt::Long;
use AnyEvent::Socket;
View
@@ -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;
@@ -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
@@ -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.