ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
(Generate patch)

Comparing AnyEvent/t/handle/04_listen.t (file contents):
Revision 1.7 by elmex, Thu May 15 13:32:19 2008 UTC vs.
Revision 1.14 by root, Sat May 24 23:15:14 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2
2use strict; 3use strict;
4
3use AnyEvent::Impl::Perl; 5use AnyEvent::Impl::Perl;
4use AnyEvent::Handle; 6use AnyEvent::Handle;
7use AnyEvent::Socket;
5use AnyEvent::Util; 8use AnyEvent;
6use IO::Socket::INET;
7 9
8my $lbytes; 10my $lbytes;
9my $rbytes; 11my $rbytes;
10 12
11print "1..2\n"; 13print "1..2\n";
12 14
13my $cv = AnyEvent->condvar; 15my $cv = AnyEvent->condvar;
14 16
15my $sock = IO::Socket::INET->new ( 17my $hdl;
16 Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost', 18my $port;
17) or die "Couldn't make socket: $!\n";
18 19
19my $hdl; 20my $w = tcp_server undef, undef,
21 sub {
22 my ($fh, $host, $port) = @_;
20 23
21my $w = AnyEvent::Util::listen ($sock, sub {
22 my ($cl, $claddr) = @_;
23 $hdl = AnyEvent::Handle->new (fh => $cl, on_eof => sub { $cv->broadcast }); 24 $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
24 25
25 $hdl->push_read_chunk (6, sub { 26 $hdl->push_read (chunk => 6, sub {
26 my ($hdl, $data) = @_; 27 my ($hdl, $data) = @_;
27 28
28 if ($data eq "TEST\015\012") { 29 if ($data eq "TEST\015\012") {
29 print "ok 1 - server received client data\n"; 30 print "ok 1 - server received client data\n";
30 } else { 31 } else {
31 print "not ok 1 - server received bad client data\n"; 32 print "not ok 1 - server received bad client data\n";
32 } 33 }
33 34
34 $hdl->push_write ("BLABLABLA\015\012"); 35 $hdl->push_write ("BLABLABLA\015\012");
35 }); 36 });
37 }, sub {
38 ($port) = Socket::unpack_sockaddr_in getsockname $_[0];
36 39
37}, sub { 40 0
38 warn "error on accept: $!";
39 $cv->broadcast;
40});
41
42my $clsock =
43 IO::Socket::INET->new (
44 PeerHost => $sock->sockhost,
45 PeerPort => $sock->sockport,
46 Blocking => 0,
47 ); 41 };
48 42
49my $clhdl; 43my $clhdl;
50my $wc = AnyEvent::Util::connect ($clsock, sub { 44my $wc = tcp_connect localhost => $port, sub {
51 my ($clsock) = @_; 45 my ($fh) = @_
46 or die "connect: $!";
47
52 $clhdl = AnyEvent::Handle->new (fh => $clsock, on_eof => sub { $cv->broadcast }); 48 $clhdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
53 49
54 $clhdl->push_write ("TEST\015\012"); 50 $clhdl->push_write ("TEST\015\012");
55 $clhdl->push_read_line (sub { 51 $clhdl->push_read (line => sub {
56 my ($clhdl, $line) = @_; 52 my ($clhdl, $line) = @_;
57 53
58 if ($line eq 'BLABLABLA') { 54 if ($line eq 'BLABLABLA') {
59 print "ok 2 - client received response\n"; 55 print "ok 2 - client received response\n";
60 } else { 56 } else {
61 print "not ok 2 - client received bad response\n"; 57 print "not ok 2 - client received bad response\n";
62 } 58 }
63 59
64 $cv->broadcast; 60 $cv->broadcast;
65 }); 61 });
66}, sub { 62};
67 warn "couldn't connect: $!";
68 $cv->broadcast;
69}, 10);
70 63
71$cv->wait; 64$cv->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines