APR::Socket - Perl API for APR sockets |
APR::Socket - Perl API for APR sockets
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::SO_NONBLOCK)) { $sock->opt_set(APR::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::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 }
APR::Socket
provides the Perl interface to APR sockets.
APR::Socket
provides the following methods:
opt_get
Query socket options for the specified socket
$val = $sock->opt_get($opt);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$opt
( APR::Const constant|docs::2.0::api::APR::Const/C__socket_
)$val
( integer )APR::Error|docs::2.0::api::APR::Error
Examples can be found in the socket options constants section. For example setting the IO to the blocking mode.
opt_set
Setup socket options for the specified socket
$sock->opt_set($opt, $val);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
object )$opt
( APR::Const|docs::2.0::api::APR::Const/C__socket_
constant )$val
( integer )APR::Error|docs::2.0::api::APR::Error
Examples can be found in the socket options constants section. For example setting the IO to the blocking mode.
recv
Read incoming data from the socket
$len = $sock->recv($buffer, $wanted);
$sock
( APR::SockAddr object|docs::2.0::api::APR::SockAddr
object )$buffer
( SCALAR )$wanted
( int )$len
( number )$buffer
gets populated with the string that is read. It will
contain an empty string if there was nothing to read.
APR::Error|docs::2.0::api::APR::Error
'(11) Resource temporarily unavailable'
error
(exception
APR::EAGAIN|docs::2.0::api::APR::Const/C_APR__EAGAIN_
), then you
didn't ensure that the socket is in a blocking IO mode
before using it.
If timeout was set via timeout_set|/C_timeout_set_
, you may need to
catch the APR::TIMEUP|docs::2.0::api::APR::Const/C_APR__TIMEUP_
exception. For example:
use APR::Const -compile => qw(TIMEUP); my $buffer; eval { $sock->recv($buffer, $wanted) }; if ($@ && $@ == APR::TIMEUP) { # timeout, do something, e.g. }
Examples:
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::SO_NONBLOCK)) { $sock->opt_set(APR::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); }
send
Write data to the socket
$wlen = $sock->send($buf, $opt_len);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$buf
( scalar )$opt_len
( int )$buf
.
$wlen
( integer )For examples see the recv|/C_recv_
item.
timeout_get
Get socket timeout settings
$usecs = $sock->timeout_get();
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$usecs
( number)APR::timeout_set|/C__timeout_set_
) for possible
values and their meaning.
APR::Error|docs::2.0::api::APR::Error
timeout_set
Setup socket timeout.
$sock->timeout_set($usecs);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$usecs
( number )The possible values are:
send()|/C__send_
and recv()|/C__recv_)
throw
(APR::TIMEUP|docs::2.0::api::APR::Const/C__APR__TIMEUP_
exception) if specified time elapses with no data sent or received.
Notice that the positive value is in micro seconds. So if you want to set the timeout for 5 seconds, the value should be: 5_000_000.
This mode sets the socket into a non-blocking IO mode.
send()|/C__send_
and recv()|/C__recv_)
calls never block.
send()|/C__send_
and recv()|/C__recv_)
calls block.
Usually just -1 is used for this case, but any negative value will do.
This mode sets the socket into a blocking IO mode.
APR::Error|docs::2.0::api::APR::Error
APR::Socket
also provides auto-generated Perl interface for a few
other methods which aren't tested at the moment and therefore their
API is a subject to change. These methods will be finalized later as a
need arises. If you want to rely on any of the following methods
please contact the the mod_perl development mailing list so we can help each other take the steps necessary
to shift the method to an officially supported API.
bind
META: Autogenerated - needs to be reviewed/completed
Bind the socket to its associated port
$ret = $sock->bind($sa);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$sa
( APR::SockAddr object|docs::2.0::api::APR::SockAddr
)$ret
( integer )This may be where we will find out if there is any other process using the selected port.
close
META: Autogenerated - needs to be reviewed/completed
Close a socket.
$ret = $sock->close();
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$ret
( integer )
connect
META: Autogenerated - needs to be reviewed/completed
Issue a connection request to a socket either on the same machine or a different one.
$ret = $sock->connect($sa);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$sa
( APR::SockAddr object|docs::2.0::api::APR::SockAdrr
)$ret
( integer )
listen
META: Autogenerated - needs to be reviewed/completed
Listen to a bound socket for connections.
$ret = $sock->listen($backlog);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$backlog
( integer )$ret
( integer )
recvfrom
META: Autogenerated - needs to be reviewed/completed
$ret = $from->recvfrom($sock, $flags, $buf, $len);
$from
( APR::SockAddr object|docs::2.0::api::APR::SockAddr
)$sock
( APR::SockAddr object|docs::2.0::api::APR::SockAddr
)$flags
( integer )$buf
( integer )$len
( string )$ret
( integer )
sendto
META: Autogenerated - needs to be reviewed/completed
$ret = $sock->sendto($where, $flags, $buf, $len);
$sock
( APR::Socket object|docs::2.0::api::APR::Socket
)$where
( APR::Socket object|docs::2.0::api::APR::Socket
)$flags
( integer )$buf
( scalar )$len
( string )$ret
( integer )
mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.
The mod_perl development team and numerous contributors.
APR::Socket - Perl API for APR sockets |