apr::socket.3

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 3 代码 · 共 778 行 · 第 1/2 页

3
778
字号
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings.  \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote.  \*(C+ will.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\.    ds -- \(*W-.    ds PI pi.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch.    ds L" "".    ds R" "".    ds C` "".    ds C' ""'br\}.el\{\.    ds -- \|\(em\|.    ds PI \(*p.    ds L" ``.    ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el       .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD.  Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\.    de IX.    tm Index:\\$1\t\\n%\t"\\$2"...    nr % 0.    rr F.\}.el \{\.    de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear.  Run.  Save yourself.  No user-serviceable parts..    \" fudge factors for nroff and troff.if n \{\.    ds #H 0.    ds #V .8m.    ds #F .3m.    ds #[ \f1.    ds #] \fP.\}.if t \{\.    ds #H ((1u-(\\\\n(.fu%2u))*.13m).    ds #V .6m.    ds #F 0.    ds #[ \&.    ds #] \&.\}.    \" simple accents for nroff and troff.if n \{\.    ds ' \&.    ds ` \&.    ds ^ \&.    ds , \&.    ds ~ ~.    ds /.\}.if t \{\.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u".    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}.    \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E.    \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'.    \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\.    ds : e.    ds 8 ss.    ds o a.    ds d- d\h'-1'\(ga.    ds D- D\h'-1'\(hy.    ds th \o'bp'.    ds Th \o'LP'.    ds ae ae.    ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "mod_perl-2.0.2::docs::api::APR::Socket 3".TH mod_perl-2.0.2::docs::api::APR::Socket 3 "2005-10-21" "perl v5.10.0" "User Contributed Perl Documentation".\" For nroff, turn off justification.  Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"APR::Socket \- Perl API for APR sockets.SH "Synopsis".IX Header "Synopsis".Vb 1\&  use APR::Socket ();\&  \&  ### set the socket to the blocking mode if it isn\*(Aqt 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\en";\&      # 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\&  }.Ve.SH "Description".IX Header "Description"\&\f(CW\*(C`APR::Socket\*(C'\fR provides the Perl interface to \s-1APR\s0 sockets..SH "API".IX Header "API"\&\f(CW\*(C`APR::Socket\*(C'\fR provides the following methods:.ie n .Sh """opt_get""".el .Sh "\f(CWopt_get\fP".IX Subsection "opt_get"Query socket options for the specified socket.PP.Vb 1\&  $val = $sock\->opt_get($opt);.Ve.ie n .IP "obj: $sock\fR ( \f(CW""APR::Socket object"" )" 4.el .IP "obj: \f(CW$sock\fR ( \f(CWAPR::Socket object\fR )" 4.IX Item "obj: $sock ( APR::Socket object )"the socket object to query.ie n .IP "arg1: $opt\fR ( \f(CW""APR::Const constant"" )" 4.el .IP "arg1: \f(CW$opt\fR ( \f(CWAPR::Const constant\fR )" 4.IX Item "arg1: $opt ( APR::Const constant )"the socket option we would like to configure.  Here are theavailable socket options..ie n .IP "ret: $val ( integer )" 4.el .IP "ret: \f(CW$val\fR ( integer )" 4.IX Item "ret: $val ( integer )"the currently set value for the socketoption you've queried for.ie n .IP "excpt: ""APR::Error""" 4.el .IP "excpt: \f(CWAPR::Error\fR" 4.IX Item "excpt: APR::Error".PD 0.IP "since: 2.0.00" 4.IX Item "since: 2.0.00".PD.PPExamples can be found in the socket options constantssection. For example setting the \s-1IO\s0 to the blockingmode..ie n .Sh """opt_set""".el .Sh "\f(CWopt_set\fP".IX Subsection "opt_set"Setup socket options for the specified socket.PP.Vb 1\&  $sock\->opt_set($opt, $val);.Ve.ie n .IP "obj: $sock\fR ( \f(CW""APR::Socket object"" object )" 4.el .IP "obj: \f(CW$sock\fR ( \f(CWAPR::Socket object\fR object )" 4.IX Item "obj: $sock ( APR::Socket object object )"the socket object to set up..ie n .IP "arg1: $opt\fR ( \f(CW""APR::Const"" constant )" 4.el .IP "arg1: \f(CW$opt\fR ( \f(CWAPR::Const\fR constant )" 4.IX Item "arg1: $opt ( APR::Const constant )"the socket option we would like to configure.  Here are theavailable socket options..ie n .IP "arg2: $val ( integer )" 4.el .IP "arg2: \f(CW$val\fR ( integer )" 4.IX Item "arg2: $val ( integer )"value for the option. Refer to the socketoptions section to learn aboutthe expected values..IP "ret: no return value" 4.IX Item "ret: no return value".PD 0.ie n .IP "excpt: ""APR::Error""" 4.el .IP "excpt: \f(CWAPR::Error\fR" 4.IX Item "excpt: APR::Error".IP "since: 2.0.00" 4.IX Item "since: 2.0.00".PD.PPExamples can be found in the socket options constantssection. For example setting the \s-1IO\s0 to the blockingmode..ie n .Sh """poll""".el .Sh "\f(CWpoll\fP".IX Subsection "poll"Poll the socket for events:.PP.Vb 1\&    $rc = $sock\->poll($pool, $timeout, $events);.Ve.ie n .IP "obj: $sock\fR ( \f(CW""APR::Socket object"" )" 4.el .IP "obj: \f(CW$sock\fR ( \f(CWAPR::Socket object\fR )" 4.IX Item "obj: $sock ( APR::Socket object )"The socket to poll.ie n .IP "arg1: $pool\fR ( \f(CW""APR::Pool object"" )" 4.el .IP "arg1: \f(CW$pool\fR ( \f(CWAPR::Pool object\fR )" 4.IX Item "arg1: $pool ( APR::Pool object )"usually \f(CW\*(C`$c\->pool\*(C'\fR..ie n .IP "arg2: $timeout ( integer )" 4.el .IP "arg2: \f(CW$timeout\fR ( integer )" 4.IX Item "arg2: $timeout ( integer )"The amount of time to wait (in milliseconds) for the specified eventsto occur..ie n .IP "arg3: $events\fR ( \f(CW""APR::Const :poll constants"" )" 4.el .IP "arg3: \f(CW$events\fR ( \f(CWAPR::Const :poll constants\fR )" 4.IX Item "arg3: $events ( APR::Const :poll constants )"The events for which to wait..SpFor example use\&\f(CW\*(C`APR::Const::POLLIN\*(C'\fR to waitfor incoming data to be available,\&\f(CW\*(C`APR::Const::POLLOUT\*(C'\fR to waituntil it's possible to write data to the socket and\&\f(CW\*(C`APR::Const::POLLPRI\*(C'\fR to waitfor priority data to become available..ie n .IP "ret: $rc\fR ( \f(CW""APR::Const constant"" )" 4.el .IP "ret: \f(CW$rc\fR ( \f(CWAPR::Const constant\fR )" 4.IX Item "ret: $rc ( APR::Const constant )"If \f(CW\*(C`APR::Const::SUCCESS\*(C'\fR is received than the polling was successful. If not\&\*(-- the error code is returned, which can be converted to the errorstring with help of\&\f(CW\*(C`APR::Error::strerror\*(C'\fR..IP "since: 2.0.00" 4.IX Item "since: 2.0.00".PPFor example poll a non-blocking socket up to 1 second when readingdata from the client:.PP.Vb 3\&  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);\&  }.Ve.ie n .Sh """recv""".el .Sh "\f(CWrecv\fP".IX Subsection "recv"Read incoming data from the socket.PP.Vb 1\&  $len = $sock\->recv($buffer, $wanted);.Ve.ie n .IP "obj: $sock\fR ( \f(CW""APR::SockAddr object"" object )" 4.el .IP "obj: \f(CW$sock\fR ( \f(CWAPR::SockAddr object\fR object )" 4.IX Item "obj: $sock ( APR::SockAddr object object )"The socket to read from.ie n .IP "arg1: $buffer ( \s-1SCALAR\s0 )" 4.el .IP "arg1: \f(CW$buffer\fR ( \s-1SCALAR\s0 )" 4.IX Item "arg1: $buffer ( SCALAR )"The buffer to fill. All previous data will be lost..ie n .IP "arg2: $wanted ( int )" 4.el .IP "arg2: \f(CW$wanted\fR ( int )" 4.IX Item "arg2: $wanted ( int )"How many bytes to attempt to read..ie n .IP "ret: $len ( number )" 4.el .IP "ret: \f(CW$len\fR ( number )" 4.IX Item "ret: $len ( number )"How many bytes were actually read..Sp\&\f(CW$buffer\fR gets populated with the string that is read. It willcontain an empty string if there was nothing to read..ie n .IP "excpt: ""APR::Error""" 4.el .IP "excpt: \f(CWAPR::Error\fR" 4.IX Item "excpt: APR::Error"If you get the \f(CW\*(Aq(11) Resource temporarily unavailable\*(Aq\fR error(exception\&\f(CW\*(C`APR::Const::EAGAIN\*(C'\fR)(or another equivalent, which might be different on non-POSIXsystems), then you didn't ensure that the socket is in a blocking \s-1IO\s0mode before using it. Note that you should use \&\f(CW\*(C`APR::Status::is_EAGAIN\*(C'\fRto 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:.Sp.Vb 10\&  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;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?