apr::socket.3

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

3
778
字号
\&      }\&      else {\&          # do something else\&      }\&  }\&  warn "read $rlen bytes\en".Ve.SpIf timeout was set via \f(CW\*(C`timeout_set|/C_timeout_set_\*(C'\fR, you may need tocatch the\&\f(CW\*(C`APR::Const::TIMEUP\*(C'\fRexception. For example:.Sp.Vb 7\&  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.\&  }.Ve.SpIf not handled \*(-- you may get the error \f(CW\*(Aq70007: The timeoutspecified has expired\*(Aq\fR..SpAnother error condition that may occur is the \f(CW\*(Aq(104) Connectionreset by peer\*(Aq\fR 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..Sp.Vb 6\&  use APR::Const \-compile => qw(ECONNABORTED);\&  my $buffer;\&  eval { $sock\->recv($buffer, $wanted) };\&  if ($@ == APR::Const::ECONNABORTED) {\&      # ignore it or deal with it\&  }.Ve.IP "since: 2.0.00" 4.IX Item "since: 2.0.00".PPHere is the quick prototype example, which doesn't handle any errors(mod_perl will do that for you):.PP.Vb 1\&  use APR::Socket ();\&  \&  # set the socket to the blocking mode if it isn\*(Aqt 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);\&  }.Ve.PPIf you want to handle errors by yourself, the loop may look like:.PP.Vb 10\&  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;\&      }\&  }.Ve.ie n .Sh """send""".el .Sh "\f(CWsend\fP".IX Subsection "send"Write data to the socket.PP.Vb 1\&  $wlen = $sock\->send($buf, $opt_len);.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 write to.ie n .IP "arg1: $buf ( scalar )" 4.el .IP "arg1: \f(CW$buf\fR ( scalar )" 4.IX Item "arg1: $buf ( scalar )"The data to send.ie n .IP "opt arg2: $opt_len ( int )" 4.el .IP "opt arg2: \f(CW$opt_len\fR ( int )" 4.IX Item "opt arg2: $opt_len ( int )"There is no need to pass this argument, unless you want to send lessdata than contained in \f(CW$buf\fR..ie n .IP "ret: $wlen ( integer )" 4.el .IP "ret: \f(CW$wlen\fR ( integer )" 4.IX Item "ret: $wlen ( integer )"How many bytes were sent.IP "since: 2.0.00" 4.IX Item "since: 2.0.00".PPFor examples see the \f(CW\*(C`recv\*(C'\fR item..ie n .Sh """timeout_get""".el .Sh "\f(CWtimeout_get\fP".IX Subsection "timeout_get"Get socket timeout settings.PP.Vb 1\&  $usecs = $sock\->timeout_get();.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 set up..ie n .IP "ret: $usecs ( number)" 4.el .IP "ret: \f(CW$usecs\fR ( number)" 4.IX Item "ret: $usecs ( number)"Currently set timeout in microseconds (and also the blocking \s-1IO\s0behavior). See (\f(CW\*(C`APR::timeout_set\*(C'\fR) for possiblevalues and their meaning..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.ie n .Sh """timeout_set""".el .Sh "\f(CWtimeout_set\fP".IX Subsection "timeout_set"Setup socket timeout..PP.Vb 1\&  $sock\->timeout_set($usecs);.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 set up..ie n .IP "arg1: $usecs ( number )" 4.el .IP "arg1: \f(CW$usecs\fR ( number )" 4.IX Item "arg1: $usecs ( number )"Value for the timeout in microseconds and also the blocking \s-1IO\s0behavior..SpThe possible values are:.RS 4.IP "t > 0" 4.IX Item "t > 0"\&\f(CW\*(C`send()\*(C'\fR and \f(CW\*(C`recv()\*(C'\fR throw(\f(CW\*(C`APR::Const::TIMEUP\*(C'\fRexception) if specified time elapses with no data sent or received..SpNotice that the positive value is in micro seconds. So if you want toset the timeout for 5 seconds, the value should be: 5_000_000..SpThis mode sets the socket into a non-blocking \s-1IO\s0 mode..IP "t == 0" 4.IX Item "t == 0"\&\f(CW\*(C`send()\*(C'\fR and \f(CW\*(C`recv()\*(C'\fR calls never block..IP "t < 0" 4.IX Item "t < 0"\&\f(CW\*(C`send()\*(C'\fR and \f(CW\*(C`recv()\*(C'\fR calls block..SpUsually just \-1 is used for this case, but any negative value will do..SpThis mode sets the socket into a blocking \s-1IO\s0 mode..IP "ret: no return value" 4.IX Item "ret: no return value".RE.RS 4.RE.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.SH "Unsupported API".IX Header "Unsupported API"\&\f(CW\*(C`APR::Socket\*(C'\fR also provides auto-generated Perl interface for a fewother methods which aren't tested at the moment and therefore their\&\s-1API\s0 is a subject to change. These methods will be finalized later as aneed arises. If you want to rely on any of the following methodsplease contact the the mod_perl development mailinglist so we can help each other take the steps necessaryto shift the method to an officially supported \s-1API\s0..ie n .Sh """bind""".el .Sh "\f(CWbind\fP".IX Subsection "bind"\&\s-1META:\s0 Autogenerated \- needs to be reviewed/completed.PPBind the socket to its associated port.PP.Vb 1\&  $ret = $sock\->bind($sa);.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 bind.ie n .IP "arg1: $sa\fR ( \f(CW""APR::SockAddr object"" )" 4.el .IP "arg1: \f(CW$sa\fR ( \f(CWAPR::SockAddr object\fR )" 4.IX Item "arg1: $sa ( APR::SockAddr object )"The socket address to bind to.ie n .IP "ret: $ret ( integer )" 4.el .IP "ret: \f(CW$ret\fR ( integer )" 4.IX Item "ret: $ret ( integer )".PD 0.IP "since: subject to change" 4.IX Item "since: subject to change".PD.PPThis may be where we will find out if there is any other processusing the selected port..ie n .Sh """close""".el .Sh "\f(CWclose\fP".IX Subsection "close"\&\s-1META:\s0 Autogenerated \- needs to be reviewed/completed.PPClose a socket..PP.Vb 1\&  $ret = $sock\->close();.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 close.ie n .IP "ret: $ret ( integer )" 4.el .IP "ret: \f(CW$ret\fR ( integer )" 4.IX Item "ret: $ret ( integer )".PD 0.IP "since: subject to change" 4.IX Item "since: subject to change".PD.ie n .Sh """connect""".el .Sh "\f(CWconnect\fP".IX Subsection "connect"\&\s-1META:\s0 Autogenerated \- needs to be reviewed/completed.PPIssue a connection request to a socket either on the same machineor a different one..PP.Vb 1\&  $ret = $sock\->connect($sa);.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 we wish to use for our side of the connection.ie n .IP "arg1: $sa\fR ( \f(CW""APR::SockAddr object"" )" 4.el .IP "arg1: \f(CW$sa\fR ( \f(CWAPR::SockAddr object\fR )" 4.IX Item "arg1: $sa ( APR::SockAddr object )"The address of the machine we wish to connect to.  If \s-1NULL\s0,\&\s-1APR\s0 assumes that the sockaddr_in in the apr_socket iscompletely filled out..ie n .IP "ret: $ret ( integer )" 4.el .IP "ret: \f(CW$ret\fR ( integer )" 4.IX Item "ret: $ret ( integer )".PD 0.IP "since: subject to change" 4.IX Item "since: subject to change".PD.ie n .Sh """listen""".el .Sh "\f(CWlisten\fP".IX Subsection "listen"\&\s-1META:\s0 Autogenerated \- needs to be reviewed/completed.PPListen to a bound socket for connections..PP.Vb 1\&  $ret = $sock\->listen($backlog);.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 listen on.ie n .IP "arg1: $backlog ( integer )" 4.el .IP "arg1: \f(CW$backlog\fR ( integer )" 4.IX Item "arg1: $backlog ( integer )"The number of outstanding connections allowed in the socketslisten queue.  If this value is less than zero, the listenqueue size is set to zero..ie n .IP "ret: $ret ( integer )" 4.el .IP "ret: \f(CW$ret\fR ( integer )" 4.IX Item "ret: $ret ( integer )".PD 0.IP "since: subject to change" 4.IX Item "since: subject to change".PD.ie n .Sh """recvfrom""".el .Sh "\f(CWrecvfrom\fP".IX Subsection "recvfrom"\&\s-1META:\s0 Autogenerated \- needs to be reviewed/completed.PP.Vb 1\&  $ret = $from\->recvfrom($sock, $flags, $buf, $len);.Ve.ie n .IP "obj: $from\fR ( \f(CW""APR::SockAddr object"" )" 4.el .IP "obj: \f(CW$from\fR ( \f(CWAPR::SockAddr object\fR )" 4.IX Item "obj: $from ( APR::SockAddr object )"The apr_sockaddr_t to fill in the recipient info.ie n .IP "arg1: $sock\fR ( \f(CW""APR::SockAddr object"" )" 4.el .IP "arg1: \f(CW$sock\fR ( \f(CWAPR::SockAddr object\fR )" 4.IX Item "arg1: $sock ( APR::SockAddr object )"The socket to use.ie n .IP "arg2: $flags ( integer )" 4.el .IP "arg2: \f(CW$flags\fR ( integer )" 4.IX Item "arg2: $flags ( integer )"The flags to use.ie n .IP "arg3: $buf ( integer )" 4.el .IP "arg3: \f(CW$buf\fR ( integer )" 4.IX Item "arg3: $buf ( integer )"The buffer to use.ie n .IP "arg4: $len ( string )" 4.el .IP "arg4: \f(CW$len\fR ( string )" 4.IX Item "arg4: $len ( string )"The length of the available buffer.ie n .IP "ret: $ret ( integer )" 4.el .IP "ret: \f(CW$ret\fR ( integer )" 4.IX Item "ret: $ret ( integer )".PD 0.IP "since: subject to change" 4.IX Item "since: subject to change".PD.ie n .Sh """sendto""".el .Sh "\f(CWsendto\fP".IX Subsection "sendto"\&\s-1META:\s0 Autogenerated \- needs to be reviewed/completed.PP.Vb 1\&  $ret = $sock\->sendto($where, $flags, $buf, $len);.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 send from.ie n .IP "arg1: $where\fR ( \f(CW""APR::Socket object"" )" 4.el .IP "arg1: \f(CW$where\fR ( \f(CWAPR::Socket object\fR )" 4.IX Item "arg1: $where ( APR::Socket object )"The apr_sockaddr_t describing where to send the data.ie n .IP "arg2: $flags ( integer )" 4.el .IP "arg2: \f(CW$flags\fR ( integer )" 4.IX Item "arg2: $flags ( integer )"The flags to use.ie n .IP "arg3: $buf ( scalar )" 4.el .IP "arg3: \f(CW$buf\fR ( scalar )" 4.IX Item "arg3: $buf ( scalar )"The data to send.ie n .IP "arg4: $len ( string )" 4.el .IP "arg4: \f(CW$len\fR ( string )" 4.IX Item "arg4: $len ( string )"The length of the data to send.ie n .IP "ret: $ret ( integer )" 4.el .IP "ret: \f(CW$ret\fR ( integer )" 4.IX Item "ret: $ret ( integer )".PD 0.IP "since: subject to change" 4.IX Item "since: subject to change".PD.SH "See Also".IX Header "See Also"mod_perl 2.0 documentation..SH "Copyright".IX Header "Copyright"mod_perl 2.0 and its core modules are copyrighted underThe Apache Software License, Version 2.0..SH "Authors".IX Header "Authors"The mod_perl development team and numerouscontributors.

⌨️ 快捷键说明

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