Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have added 'connected' method to Feersum::Connection::Handle #11

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions Feersum.xs
Expand Up @@ -2510,6 +2510,24 @@ seek (feer_conn_handle *hdl, ssize_t offset, ...)
OUTPUT:
RETVAL

int
connected (feer_conn_handle *hdl)
PROTOTYPE: $
CODE:
{
assert(ix);
socklen_t addrlen;
int res;
res = getpeername(hdl->fd, hdl->sa, &addrlen);
if (res == 0) {
RETVAL = 1;
} else {
RETVAL = 0;
}
}
OUTPUT:
RETVAL

int
close (feer_conn_handle *hdl)
PROTOTYPE: $
Expand Down
9 changes: 9 additions & 0 deletions lib/Feersum/Connection/Handle.pm
Expand Up @@ -57,6 +57,8 @@ For write handles:
$w->poll_cb(sub {
# use $_[0] instead of $w to avoid a closure
$_[0]->write(\"some data");
# check if the socket is connected
$_[0]->connected();
# can close() or unregister the poll_cb in here
$_[0]->close();
});
Expand Down Expand Up @@ -138,6 +140,13 @@ Pass in an array-ref and it works much like the two C<write()> calls above,
except it's way more efficient than calling C<write()> over and over.
Undefined elements of the array are ignored.

=item C<< $w->connected() >>

Uses getpeername in order to determine if the underlying socket is connected.
It will return true even if the socket is in CLOSE_WAIT state. If the TCP
socket is closed (after TCP timeout) by the system it will return false
value. The implementations is consistent with IO::Handle.

=item C<< $w->close() >>

Close the HTTP response (which triggers the "T-E: chunked" terminating chunk
Expand Down
3 changes: 2 additions & 1 deletion t/05-streaming.t
Expand Up @@ -5,7 +5,7 @@ use constant HARDER => $ENV{RELEASE_TESTING} ? 10 : 1;
use constant CLIENTS_11 => HARDER * 2;
use constant CLIENTS_10 => HARDER * 2;
use constant CLIENTS => CLIENTS_11 + CLIENTS_10;
use Test::More tests => 7 + 21 * CLIENTS_11 + 22 * CLIENTS_10;
use Test::More tests => 7 + 22 * CLIENTS_11 + 23 * CLIENTS_10;
use Test::Fatal;
use lib 't'; use Utils;

Expand Down Expand Up @@ -45,6 +45,7 @@ $evh->request_handler(sub {
$started++;
isa_ok($w, 'Feersum::Connection::Writer', "got a writer $cnum");
isa_ok($w, 'Feersum::Connection::Handle', "... it's a handle $cnum");
ok $w->connected, "connected method works on write handle";
my $n = 0;
my $wrote_third = 0;
my $t; $t = AE::timer rand()/5,rand()/5, sub {
Expand Down