1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 34;
use Net::EmptyPort;
use Danga::Socket;
use IO::Socket::INET;
use Time::HiRes qw(sleep); # to allow fractional sleep
use POSIX;
no warnings qw(deprecated);
use vars qw($done);
SKIP: {
my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
skip "not on linux 2.6", 1 if $^O ne "linux" || $release =~ /^2\.[01234]/;
ok(Danga::Socket->HaveEpoll(), "using epoll");
}
for my $mode ("auto", "poll") {
$done = 0;
my $iters = 0;
is(Danga::Socket->WatchedSockets, 0, "no watched sockets");
Danga::Socket->SetLoopTimeout(150);
Danga::Socket->SetPostLoopCallback(sub {
return 0 if $done;
$iters++;
ok(Server->new, "created server") if $iters == 1;
if ($iters == 3) {
ok(ClientOut->new, "created client outgoing");
sleep 0.2;
is(Danga::Socket->WatchedSockets, 2, "two watched sockets");
}
return 1;
});
if ($mode eq "poll") {
require IO::Poll;
Danga::Socket->PollEventLoop;
} else {
Danga::Socket->EventLoop;
}
ok($done, "$mode mode is done");
# check descriptor map status
my $map = Danga::Socket->DescriptorMap;
ok(ref $map eq "HASH", "map is hash");
is(scalar keys %$map, 3, "watching 3 connections");
Danga::Socket->Reset;
is(scalar keys %$map, 0, "watching 0 connections");
}
ok(1, "finish");
package Server;
use base 'Danga::Socket';
use vars qw($SERVER_PORT);
use Test::More;
BEGIN {
$SERVER_PORT = Net::EmptyPort::empty_port();
}
sub new {
my $class = shift;
diag("Starting server on port $SERVER_PORT");
my $ssock = IO::Socket::INET->new(Listen => 5,
LocalAddr => '127.0.0.1',
LocalPort => $SERVER_PORT,
Proto => 'tcp',
ReuseAddr => 1,
Blocking => 0,
);
die "couldn't create socket" unless $ssock;
my $self = $class->SUPER::new($ssock);
$self->watch_read(1);
return $self;
}
sub event_read {
my $self = shift;
while (my ($psock, $peeraddr) = $self->{sock}->accept) {
IO::Handle::blocking($psock, 0);
Test::More::ok($psock, "Server got incoming conn");
ClientIn->new($psock);
}
}
package ClientIn;
use base 'Danga::Socket';
use fields (
'got',
'state',
);
sub new {
my ($class, $sock) = @_;
my $self = fields::new($class);
$self->SUPER::new($sock); # init base fields
$self->watch_read(1);
my $peer_str = $self->peer_addr_string();
my $local_str = $self->local_addr_string();
Test::More::ok($peer_str, "New connection from host $peer_str");
Test::More::ok($local_str, "... on host $local_str");
$self->{state} = "init";
$self->{got} = "";
return $self;
}
sub event_read {
my $self = shift;
my $go = sub {
$self->{state} = $_[0];
return;
};
if ($self->{state} eq "init") {
my $bref = $self->read(5);
Test::More::ok($$bref eq "Hello", "state 1: ClientIn got Hello");
$self->push_back_read("lo");
return $go->("step2");
}
if ($self->{state} eq "step2") {
my $bref = $self->read(3);
Test::More::ok($$bref eq "lo", "ask for more than what's in push_back_read");
$self->push_back_read("Hello");
return $go->("step3");
}
if ($self->{state} eq "step3") {
my $bref = $self->read(3);
Test::More::ok($$bref eq "Hel", "ask for less than what's in push_back_read");
$self->{got} = $$bref;
return $go->("step4");
}
if ($self->{state} eq "step4") {
my $bref = $self->read(500);
$self->{got} .= $$bref;
if ($self->{got} eq "Hello!\n") {
Test::More::ok(1, "ClientIn got Hello!");
$self->watch_read(0);
$main::done = 1;
}
}
}
package ClientOut;
use base 'Danga::Socket';
use fields (
'connected', # 0 or 1
);
use Socket qw(PF_INET IPPROTO_TCP SOCK_STREAM);
use Test::More;
sub new {
my $class = shift;
my $sock;
socket $sock, PF_INET, SOCK_STREAM, IPPROTO_TCP;
die "can't create outgoing sock" unless $sock && defined fileno($sock);
IO::Handle::blocking($sock, 0);
diag("Connecting to 127.0.0.1:$Server::SERVER_PORT");
connect $sock, Socket::sockaddr_in($Server::SERVER_PORT, Socket::inet_aton('127.0.0.1'));
my $self = fields::new($class);
$self->SUPER::new($sock);
$self->{'connected'} = 0;
$self->watch_write(1);
return $self;
}
sub event_write {
my $self = shift;
if (! $self->{'connected'}) {
Test::More::ok(1, "ClientOut connected");
$self->{'connected'} = 1;
}
$self->write("Hello!\n");
$self->watch_write(0);
}
|