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
|
# Use case: client connect to tr/x/y/ server and send/recv a lot of data.
# Use case: client connect to tr/x/y/ server using tr/y/z/ proxy and
# send/recv a lot of data.
use warnings;
use strict;
use lib 't';
use share;
my $SIZE = 204800;
use constant ACCEPTED => 123;
@CheckPoint = (
# first test: client connect to server without proxy
['listener_server', ACCEPTED ], 'server: new client',
((
['server', IN ], 'server: IN',
['server', ('x' x BUFSIZE) ], ' received "xxx..."',
) x ($SIZE/BUFSIZE)),
['server', SENT ], 'server: SENT',
['client', EOF ], 'client: EOF',
['client', ('y' x $SIZE) ], ' received "yyyyyyyyy..."',
# second test: client connect to server using proxy
['listener_proxy', ACCEPTED ], 'proxy: new client',
{
win32_somefreebsd => [
['listener_server', ACCEPTED ], 'server: new client',
['proxy2server', CONNECTED ], 'proxy2server: CONNECTED',
],
other => [
['proxy2server', CONNECTED ], 'proxy2server: CONNECTED',
['listener_server', ACCEPTED ], 'server: new client',
],
},
((
['server', IN ], 'server: IN',
['server', ('x' x BUFSIZE) ], ' received "xxx..."',
) x ($SIZE/BUFSIZE)),
['server', SENT ], 'server: SENT',
['proxy2server', EOF ], 'proxy2server: EOF',
['client2', EOF ], 'client2: EOF',
['client2', ('z' x $SIZE) ], ' received "zzzzzzzzz..."',
);
plan tests =>
2 # {is_eof} tests in client() and client2()
+ checkpoint_count();
my $srv_sock = tcp_server('127.0.0.1', 0);
my $srv_w = EV::io($srv_sock, EV::READ, \&listener_server);
my $prx_sock = tcp_server('127.0.0.1', 0);
my $prx_w = EV::io($prx_sock, EV::READ, \&listener_proxy);
sub new_client {
my ($port, $cb) = @_;
IO::Stream->new({
host => '127.0.0.1',
port => $port,
cb => $cb,
wait_for => EOF,
out_buf => ('x' x $SIZE),
in_buf_limit=> 0,
});
}
new_client(sockport($srv_sock), \&client);
EV::loop;
sub listener_server {
if (accept my $sock, $srv_sock) {
checkpoint(ACCEPTED);
IO::Stream->new({
fh => $sock,
cb => \&server,
wait_for => IN,
});
}
elsif ($! != EAGAIN) {
die "accept: $!\n";
}
}
sub server {
my ($io, $e, $err) = @_;
die "server error: $err\n" if $err;
checkpoint($e);
if ($e & IN) {
checkpoint($io->{in_buf});
if ($io->{in_bytes} == $SIZE) {
$io->{wait_for} = SENT;
}
$io->write('y' x length $io->{in_buf});
$io->{in_buf} = q{};
}
if ($e & SENT) {
$io->close();
}
}
sub listener_proxy {
if (accept my $sock, $prx_sock) {
checkpoint(ACCEPTED);
IO::Stream->new({
host => '127.0.0.1',
port => sockport($srv_sock),
cb => \&proxy2server,
wait_for => CONNECTED|IN|EOF,
Client => undef,
ClientSock => $sock,
});
}
elsif ($! != EAGAIN) {
die "accept: $!\n";
}
}
sub proxy2server {
my ($io, $e, $err) = @_;
die "proxy2server error: $err\n" if $err;
checkpoint($e)
if $e != IN && $e != SENT;
if ($e & CONNECTED) {
$io->{Client} = IO::Stream->new({
fh => $io->{ClientSock},
cb => \&proxy2client,
wait_for => IN,
Server => $io,
});
weaken($io->{Client}->{Server});
weaken($io->{Client});
}
if ($e & SENT) {
shutdown $io->{fh}, 1;
}
if ($e & IN) {
$io->{in_buf} =~ s/y/z/g;
if ($io->{in_bytes} == $SIZE) {
$io->{Client}->{wait_for} |= SENT;
}
$io->{Client}->write($io->{in_buf});
$io->{in_buf} = q{};
}
if ($e & EOF) {
$io->close();
}
}
sub proxy2client {
my ($io, $e, $err) = @_;
die "proxy2client error: $err\n" if $err;
checkpoint($e)
if $e != IN && $e != SENT;
if ($e & IN) {
if ($io->{in_bytes} == $SIZE) {
$io->{Server}->{wait_for} |= SENT;
}
$io->{Server}->write($io->{in_buf});
$io->{in_buf} = q{};
}
if ($e & SENT) {
$io->close();
}
}
sub client {
my ($io, $e, $err) = @_;
die "client error: $err\n" if $err;
checkpoint($e);
checkpoint($io->{in_buf});
ok($io->{is_eof}, ' {is_eof} set');
$io->close();
new_client(sockport($prx_sock), \&client2);
}
sub client2 {
my ($io, $e, $err) = @_;
die "client2 error: $err\n" if $err;
checkpoint($e);
checkpoint($io->{in_buf});
ok($io->{is_eof}, ' {is_eof} set');
$io->close();
EV::unloop;
}
|