NAME

AnyEvent::Mojo::Server::Connection - An active TCP connection to AnyEvent::Mojo::Server

VERSION

version 0.8003

SYNOPSIS

    use AnyEvent::Mojo::Server::Connection;

    ...

DESCRIPTION

Foreach connection to a " AnyEvent::Mojo::Server ", a AnyEvent::Mojo::Server::Connection object is created.

This object keeps track of the current " Mojo::Transaction ".

If an error or EOF condition is detected while reading or writting to the client socket, or in case of a timeout, the socket is disconnected.

METHODS

new

The constructor accepts the following parameters:

sock

The client socket.

peer_host

The IP of the client.

peer_port

The TCP port number of the client.

server

The " AnyEvent::Mojo::Server " to whom this connection belongs to.

timeout

Seconds (can be fractional) that the connection can be idle (waiting for a request or unable to write more data out).

If the connection is paused, the timeout is ignored.

If the timeout fires, the connection is closed.

It returns the AnyEvent::Mojo::Server::Connection object.

run

The run() method starts all the " AnyEvent::Handle " processing to read the next request, process it and write the response.

It returns nothing.

close

The close() method clears the current transaction, destroys the " AnyEvent::Handle " associated with this connection and closes the client socket.

request_count

Returns the total request count for the connection. In case of keep-alive requests, the request count grows beyond 1.

peer_host

Returns the IP address of the peer host.

peer_port

Returns the TCP port number of the peer host.

ASYNCHRONOUS PROCESSING

While in the middle of a request, an application can pause the current transaction, do something else (including dealing with other requests) and then resume the processing.

To do that, you application must call the $tx-connection->pause()> method.

When you are ready to send back the response, call $tx-connection->resume()>.

For example:

    # inside your response handler of you Mojo::App
    $tx->connection->pause();
    
    # Call webservice and deal with result
    http_get 'http://my.webservice.endpoint/api', sub {
      my ($data) = @_;
      
      $tx->response->body("Webservice returned this: '$data'");
      $tx->connection->resume();
    };

To make it easier to resume later, the pause() method returns a coderef that will resume the transaction when called. So the code above could be written like this:

    # inside your response handler of you Mojo::App
    my $resume_cb = $tx->connection->pause();
    
    # Call webservice and deal with result
    http_get 'http://my.webservice.endpoint/api', sub {
      my ($data) = @_;
      
      $tx->response->body("Webservice returned this: '$data'");
      $resume_cb->();
    };

pause()

Pauses the current transaction.

The transaction state must be write, that is, before sending any status or header responses.

While the connection is paused, inactivity timeouts are ignored.

Returns a coderef that, when called, will resume the transaction.

resume()

Resumes a paused transaction.

The response must be complete and we will immediatly start sending the data to the client.

Returns nothing.

AUTHOR

Pedro Melo, <melo at cpan.org>

COPYRIGHT & LICENSE

Copyright 2008-2009 Pedro Melo.

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

5 POD Errors

The following errors were encountered while parsing the POD:

Around line 243:

L<> starts or ends with whitespace

Around line 246:

L<> starts or ends with whitespace

Around line 278:

L<> starts or ends with whitespace

Around line 300:

L<> starts or ends with whitespace

Around line 309:

L<> starts or ends with whitespace