| 1 | package Ion; | ||||||||||
| 2 | # ABSTRACT: A clear and concise API for writing TCP servers and clients | ||||||||||
| 3 | 3 | $Ion::VERSION = '0.06'; | |||||||||
| 4 | 3 | ▶ | use common::sense; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Coro; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use AnyEvent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Ion::Server; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Ion::Conn; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use parent 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | our @EXPORT = qw( | |||||||||
| 15 | Connect | ||||||||||
| 16 | Listen | ||||||||||
| 17 | Service | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | sub Connect (;$$) { | ||||||||||
| 21 | 3 | ▶ | my ($host, $port) = @_; | ||||||||
|
Subroutine
Connect: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 22 | 3 | ▶ | return Ion::Conn->new(handle => $host) if ref $host; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 3 | return Ion::Conn->new(host => $host, port => $port); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | sub Listen (;$$) { | ||||||||||
| 27 | 3 | ▶ | my ($port, $host) = @_; | ||||||||
|
Subroutine
Listen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 28 | 3 | Ion::Server->new(host => $host, port => $port); | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | sub Service (&;$$) { | ||||||||||
| 32 | 2 | ▶ | my $callback = shift; | ||||||||
|
Subroutine
Service: called CC=2 SCAR=12.5
Pod
documented
| |||||||||||
| 33 | 2 | my $server = shift; | |||||||||
| 34 | 2 | ▶ | $server = Listen($server, shift) unless ref $server; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | $server->start; | |||||||||
| 36 | |||||||||||
| 37 | async_pool { | ||||||||||
| 38 | 0 | ▶ | my ($callback, $server) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | while (defined(my $conn = <$server>)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | async_pool { | ||||||||||
| 42 | 0 | ▶ | my ($callback, $conn, $server) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | while (defined(my $line = <$conn>)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | my $reply = $callback->($line, $conn, $server); | |||||||||
| 46 | 0 | ▶ | last unless defined $reply; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | ▶ | $conn->($reply) if $reply; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | $conn->close; | |||||||||
| 51 | 0 | } $callback, $conn, $server; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 2 | } $callback, $server; | |||||||||
| 55 | |||||||||||
| 56 | 2 | return $server; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 3 | 1; | |||||||||
| 60 | |||||||||||
| 61 | __END__ | ||||||||||