📄 socket.pm
字号:
# # /*# * *********** WARNING **************# * This file generated by ModPerl::WrapXS/0.01# * Any changes made here will be lost# * ***********************************# * 01: lib/ModPerl/Code.pm:708# * 02: lib/ModPerl/WrapXS.pm:624# * 03: lib/ModPerl/WrapXS.pm:1173# * 04: Makefile.PL:423# * 05: Makefile.PL:325# * 06: Makefile.PL:56# */# package APR::Socket;use strict;use warnings FATAL => 'all';use APR ();use APR::XSLoader ();our $VERSION = '0.009000';APR::XSLoader::load __PACKAGE__;1;__END__=head1 NAMEAPR::Socket - Perl API for APR sockets=head1 Synopsis use APR::Socket (); ### set the socket to the blocking mode if it isn't already ### and read in the loop and echo it back use APR::Const -compile => qw(SO_NONBLOCK); if ($sock->opt_get(APR::Const::SO_NONBLOCK)) { $sock->opt_set(APR::Const::SO_NONBLOCK => 0); } # read from/write to the socket (w/o handling possible failures) my $wanted = 1024; while ($sock->recv(my $buff, $wanted)) { $sock->send($buff); } ### get/set IO timeout and try to read some data use APR::Const -compile => qw(TIMEUP); # timeout is in usecs! my $timeout = $sock->timeout_get(); if ($timeout < 10_000_000) { $sock->timeout_set(20_000_000); # 20 secs } # now read, while handling timeouts my $wanted = 1024; my $buff; my $rlen = eval { $sock->recv($buff, $wanted) }; if ($@ && ref $@ && $@ == APR::Const::TIMEUP) { # timeout, do something, e.g. warn "timed out, will try again later"; } else { warn "asked for $wanted bytes, read $rlen bytes\n"; # do something with the data } # non-blocking io poll $sock->opt_set(APR::Const::SO_NONBLOCK => 1); my $rc = $sock->poll($c->pool, 1_000_000, APR::Const::POLLIN); if ($rc == APR::Const::SUCCESS) { # read the data } else { # handle the condition }=head1 DescriptionC<APR::Socket> provides the Perl interface to APR sockets.=head1 APIC<APR::Socket> provides the following methods:=head2 C<opt_get>Query socket options for the specified socket $val = $sock->opt_get($opt);=over 4=item obj: C<$sock>( C<L<APR::Socket object|docs::2.0::api::APR::Socket>> )the socket object to query=item arg1: C<$opt>( C<L<APR::Const constant|docs::2.0::api::APR::Const/C__socket_>> )the socket option we would like to configure. Here are theL<available socket options|docs::2.0::api::APR::Const/C__socket_>.=item ret: C<$val> ( integer )the currently set value for L<the socketoption|docs::2.0::api::APR::Const/C__socket_> you've queried for=item excpt: C<L<APR::Error|docs::2.0::api::APR::Error>>=item since: 2.0.00=backExamples can be found in L<the socket options constantssection|docs::2.0::api::APR::Const/C__socket_>. For example L<setting the IO to the blockingmode|docs::2.0::api::APR::Const/C_APR__Const__SO_NONBLOCK_>.=head2 C<opt_set>Setup socket options for the specified socket $sock->opt_set($opt, $val);=over 4=item obj: C<$sock>( C<L<APR::Socket object|docs::2.0::api::APR::Socket>> object )the socket object to set up.=item arg1: C<$opt>( C<L<APR::Const|docs::2.0::api::APR::Const/C__socket_>> constant )the socket option we would like to configure. Here are theL<available socket options|docs::2.0::api::APR::Const/C__socket_>.=item arg2: C<$val> ( integer )value for the option. Refer to the L<socketoptions|docs::2.0::api::APR::Const/C__socket_> section to learn aboutthe expected values.=item ret: no return value=item excpt: C<L<APR::Error|docs::2.0::api::APR::Error>>=item since: 2.0.00=backExamples can be found in L<the socket options constantssection|docs::2.0::api::APR::Const/C__socket_>. For example L<setting the IO to the blockingmode|docs::2.0::api::APR::Const/C_APR__Const__SO_NONBLOCK_>.=head2 C<poll>Poll the socket for events: $rc = $sock->poll($pool, $timeout, $events);=over 4=item obj: C<$sock>( C<L<APR::Socket object|docs::2.0::api::APR::Socket>> )The socket to poll=item arg1: C<$pool>( C<L<APR::Pool object|docs::2.0::api::APR::Pool>> )usually C<L<$c-E<gt>pool|docs::2.0::api::Apache2::Connection/C_pool_>>.=item arg2: C<$timeout> ( integer )The amount of time to wait (in milliseconds) for the specified eventsto occur.=item arg3: C<$events> ( C<L<APR::Const :pollconstants|docs::2.0::api::APR::Const/C__poll_>> )The events for which to wait.For example useC<L<APR::Const::POLLIN|docs::2.0::api::APR::Const/C_APR__Const__POLLIN_>> to waitfor incoming data to be available,C<L<APR::Const::POLLOUT|docs::2.0::api::APR::Const/C_APR__Const__POLLOUT_>> to waituntil it's possible to write data to the socket andC<L<APR::Const::POLLPRI|docs::2.0::api::APR::Const/C_APR__Const__POLLPRI_>> to waitfor priority data to become available.=item ret: C<$rc>( C<L<APR::Const constant|docs::2.0::api::APR::Const>> )If C<APR::Const::SUCCESS> is received than the polling was successful. If not-- the error code is returned, which can be converted to the errorstring with help ofC<L<APR::Error::strerror|docs::2.0::api::APR::Error/C_strerror_>>.=item since: 2.0.00=backFor example poll a non-blocking socket up to 1 second when readingdata from the client: use APR::Socket (); use APR::Connection (); use APR::Error (); use APR::Const -compile => qw(SO_NONBLOCK POLLIN SUCCESS TIMEUP); $sock->opt_set(APR::Const::SO_NONBLOCK => 1); my $rc = $sock->poll($c->pool, 1_000_000, APR::Const::POLLIN); if ($rc == APR::Const::SUCCESS) { # Data is waiting on the socket to be read. # $sock->recv(my $buf, BUFF_LEN) } elsif ($rc == APR::Const::TIMEUP) { # One second elapsed and still there is no data waiting to be # read. for example could try again. } else { die "poll error: " . APR::Error::strerror($rc); }=head2 C<recv>Read incoming data from the socket $len = $sock->recv($buffer, $wanted);=over 4=item obj: C<$sock>( C<L<APR::SockAddr object|docs::2.0::api::APR::SockAddr>> object )The socket to read from=item arg1: C<$buffer> ( SCALAR )The buffer to fill. All previous data will be lost.=item arg2: C<$wanted> ( int )How many bytes to attempt to read.=item ret: C<$len> ( number )How many bytes were actually read.C<$buffer> gets populated with the string that is read. It willcontain an empty string if there was nothing to read.=item excpt: C<L<APR::Error|docs::2.0::api::APR::Error>>If you get the C<'(11) Resource temporarily unavailable'> error(exceptionC<L<APR::Const::EAGAIN|docs::2.0::api::APR::Const/C_APR__Const__EAGAIN_>>)(or another equivalent, which might be different on non-POSIXsystems), then you didn't ensure that the socket is in L<a blocking IOmode|/C_opt_set_> before using it. Note that you should use C<L<APR::Status::is_EAGAIN|docs::2.0::api::APR::Status/C_is_EAGAIN_>>to perform this check (since different error codes may be returned forthe same event on different OSes). For example if the socket is set tothe non-blocking mode and there is no data right away, you may getthis exception thrown. So here is how to check for it and retry a fewtimes after short delays: use APR::Status (); $sock->opt_set(APR::Const::SO_NONBLOCK, 1); # .... my $tries = 0; my $buffer; RETRY: my $rlen = eval { $socket->recv($buffer, SIZE) }; if ($@) die $@ unless ref $@ && APR::Status::is_EAGAIN($@); if ($tries++ < 3) { # sleep 250msec select undef, undef, undef, 0.25; goto RETRY; } else { # do something else } } warn "read $rlen bytes\n"If timeout was set via C<timeout_set|/C_timeout_set_>, you may need tocatch theC<L<APR::Const::TIMEUP|docs::2.0::api::APR::Const/C_APR__Const__TIMEUP_>>exception. For example: use APR::Const -compile => qw(TIMEUP); $sock->timeout_set(1_000_000); # 1 sec my $buffer; eval { $sock->recv($buffer, $wanted) }; if ($@ && $@ == APR::Const::TIMEUP) { # timeout, do something, e.g. }If not handled -- you may get the error C<'70007: The timeoutspecified has expired'>.Another error condition that may occur is the C<'(104) Connectionreset by peer'> error, which is up to your application logic to decidewhether it's an error or not. This error usually happens when theclient aborts the connection. use APR::Const -compile => qw(ECONNABORTED); my $buffer; eval { $sock->recv($buffer, $wanted) }; if ($@ == APR::Const::ECONNABORTED) { # ignore it or deal with it }=item since: 2.0.00=backHere is the quick prototype example, which doesn't handle any errors(mod_perl will do that for you): use APR::Socket (); # set the socket to the blocking mode if it isn't already use APR::Const -compile => qw(SO_NONBLOCK); if ($sock->opt_get(APR::Const::SO_NONBLOCK)) { $sock->opt_set(APR::Const::SO_NONBLOCK => 0); } # read from/write to the socket (w/o handling possible failures) my $wanted = 1024; while ($sock->recv(my $buffer, $wanted)) { $sock->send($buffer); }If you want to handle errors by yourself, the loop may look like: use APR::Const -compile => qw(ECONNABORTED); # ... while (1) { my $buf; my $len = eval { $sock->recv($buf, $wanted) }; if ($@) { # handle the error, e.g. to ignore aborted connections but # rethrow any other errors: if ($@ == APR::Const::ECONNABORTED) { # ignore last; } else { die $@; # retrow } } if ($len) { $sock->send($buffer); } else { last; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -