Author image Daisuke (yet another) Maki
and 1 contributors

NAME

JSON::RPC2::AnyEvent::Server::Handle - dispatch JSON-RPC requests comming from file-handle to JSON::RPC2::AnyEvent::Server

SYNOPSIS

    use AnyEvent::Socket;
    use JSON::RPC2::AnyEvent::Server::Handle;  # Add `dispatch_fh' method in JSON::RPC2::AnyEvent::Server
    
    my $srv = JSON::RPC2::AnyEvent::Server->(
        echo => sub{
            my ($cv, $args) = @_;
            $cv->send($args);
        }
    );
    
    my $w = tcp_server undef, 8080, sub {
        my ($fh, $host, $port) = @_;
        my $hdl = $srv->dispatch_fh($fh);  # equivalent to JSON::RPC2::AnyEvent::Server::Handle->new($srv, $fh)
        $hdl->on_end(sub{
            my $h = shift;  # JSON::RPC2::AnyEvent::Server::Handle
            # underlying fh is already closed here
            $h->destroy;
            undef $hdl;
        });
        $hdl->on_error(sub{
            my ($h, $fatal, $message) = @_;
            warn $message;
            $h->destroy  if $fatal;
            undef $hdl;
        });
    };

DESCRIPTION

JSON::RPC2::AnyEvent::Server::Handle is AnyEvent::Handle adapter for JSON::RPC2::AnyEvent::Server.

INTERFACE

CLASS->new($srv, $fh) -> $handle

$srv->dispatch_fh($fh) -> $handle

Connect $fh to $srv and returns a JSON::RPC2::AnyEvent::Handle object. The object dispatches coming requests to $srv and sends back returned response to $fh.

This module adds dispatch_fh method in JSON::RPC2::AnyEvent::Server, which can be used as a shortcut of new.

$srv: JSON::RPC2::AnyEvent::Server

JSON::RPC2::AnyEvent::Server object to connect.

$fh: AnyEvent::Handle or file-handle

File handle to be connected.

$handle: JSON::RPC2::AnyEvent::Server::Handle

New JSON::RPC2::AnyEvent::Server::Handle object.

$self->on_end(sub{ my($self) = @_; ... })

Registers callback called when the underlying file handle successfully reaches EOF.

$self->on_error(sub{ my($self, $fatal, $message) = @_; ... })

Registers callback called when an error occurs during comminication.

$self->destroy

Manually destroys this object.

LICENSE

Copyright (C) Daisuke (yet another) Maki.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Daisuke (yet another) Maki <maki.daisuke@gmail.com>