--- AnyEvent/t/handle/04_listen.t 2008/05/15 13:32:19 1.7 +++ AnyEvent/t/handle/04_listen.t 2008/05/24 23:15:14 1.14 @@ -1,9 +1,11 @@ #!/opt/perl/bin/perl + use strict; + use AnyEvent::Impl::Perl; use AnyEvent::Handle; -use AnyEvent::Util; -use IO::Socket::INET; +use AnyEvent::Socket; +use AnyEvent; my $lbytes; my $rbytes; @@ -12,47 +14,41 @@ my $cv = AnyEvent->condvar; -my $sock = IO::Socket::INET->new ( - Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost', -) or die "Couldn't make socket: $!\n"; - my $hdl; +my $port; -my $w = AnyEvent::Util::listen ($sock, sub { - my ($cl, $claddr) = @_; - $hdl = AnyEvent::Handle->new (fh => $cl, on_eof => sub { $cv->broadcast }); - - $hdl->push_read_chunk (6, sub { - my ($hdl, $data) = @_; - - if ($data eq "TEST\015\012") { - print "ok 1 - server received client data\n"; - } else { - print "not ok 1 - server received bad client data\n"; - } +my $w = tcp_server undef, undef, + sub { + my ($fh, $host, $port) = @_; + + $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast }); + + $hdl->push_read (chunk => 6, sub { + my ($hdl, $data) = @_; + + if ($data eq "TEST\015\012") { + print "ok 1 - server received client data\n"; + } else { + print "not ok 1 - server received bad client data\n"; + } + + $hdl->push_write ("BLABLABLA\015\012"); + }); + }, sub { + ($port) = Socket::unpack_sockaddr_in getsockname $_[0]; - $hdl->push_write ("BLABLABLA\015\012"); - }); - -}, sub { - warn "error on accept: $!"; - $cv->broadcast; -}); - -my $clsock = - IO::Socket::INET->new ( - PeerHost => $sock->sockhost, - PeerPort => $sock->sockport, - Blocking => 0, - ); + 0 + }; my $clhdl; -my $wc = AnyEvent::Util::connect ($clsock, sub { - my ($clsock) = @_; - $clhdl = AnyEvent::Handle->new (fh => $clsock, on_eof => sub { $cv->broadcast }); +my $wc = tcp_connect localhost => $port, sub { + my ($fh) = @_ + or die "connect: $!"; + + $clhdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast }); $clhdl->push_write ("TEST\015\012"); - $clhdl->push_read_line (sub { + $clhdl->push_read (line => sub { my ($clhdl, $line) = @_; if ($line eq 'BLABLABLA') { @@ -63,9 +59,6 @@ $cv->broadcast; }); -}, sub { - warn "couldn't connect: $!"; - $cv->broadcast; -}, 10); +}; $cv->wait;