NAME

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

VERSION

Version 0.1

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

Number of seconds the connection will wait for data while reading.

If no data is sent, 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.

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 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 298:

L<> starts or ends with whitespace

Around line 301:

L<> starts or ends with whitespace

Around line 333:

L<> starts or ends with whitespace

Around line 352:

L<> starts or ends with whitespace

Around line 361:

L<> starts or ends with whitespace