Name
SPVM::Sys::Socket::Util - Socket Utilities
Description
Sys::Socket::Util class in SPVM has methods for socket utilities.
Usage
use Sys::Socket::Util;
Class Methods
inet_aton
static method inet_aton : Sys::Socket::In_addr ($address : string);
Creates a new Sys::Socket::In_addr object and calls Sys::Socket#inet_aton method with it, and returns it.
inet_ntoa
static method inet_ntoa : string ($in_addr : Sys::Socket::In_addr);
Calls Sys::Socket#inet_ntoa method, and returns its return value.
inet_pton
static method inet_pton : Sys::Socket::In_addr_base ($family : int, $address : string);
Creates a new Sys::Socket::In_addr object or a new Sys::Socket::In6_addr according to the address family $family.
And calls Sys::Socket#inet_aton method with it, and returns it.
Excetpions:
If the address family $family is not available, an excetpion is thrown.
inet_ntop
static method inet_ntop : string ($family : int, $in_addr : Sys::Socket::In_addr_base);
Calls Sys::Socket#inet_ntop method given enough address buffer $dst.
And the got address $dst is truncated to the length the address and returns it.
sockaddr_in
static method sockaddr_in : Sys::Socket::Sockaddr::In ($port : int, $in_addr : Sys::Socket::In_addr);
Creates a new Sys::Socket::Sockaddr::In object given the port $port and the address $in_addr, and returns it.
The address family is set to AF_INET.
Exceptions:
$in_addr must be defined. Otherwise an exception is thrown.
sockaddr_in6
static method sockaddr_in6 : Sys::Socket::Sockaddr::In6 ($port : int, $in6_addr : Sys::Socket::In6_addr);
Creates a new Sys::Socket::Sockaddr::In6 object given the port $port and the address $in_addr, and returns it.
The address family is set to AF_INET6.
Exceptions:
$in6_addr must be defined. Otherwise an exception is thrown.
sockaddr_un
static method sockaddr_un : Sys::Socket::Sockaddr::Un ($path : string);
Creates a new Sys::Socket::Sockaddr::Un object given the path $path, and returns it.
The address family is set to AF_UNIX.
Exceptions:
$path must be defined. Otherwise an exception is thrown.
set_tcp_keepalive
static method set_tcp_keepalive : void ($socket_fd : int, $onoff : int, $keepidle_sec : int, $keepintvl_sec : int = -1);
Sets the TCP keep-alive settings (SO_KEEPALIVE, TCP_KEEPIDLE/TCP_KEEPALIVE, and TCP_KEEPINTVL) in a portable way.
Parameters:
$socket_fd: The socket file descriptor.$onoff: Set to 1 to enable TCP keep-alive, or 0 to disable it.$keepidle_sec: The time (in seconds) the connection remains idle before TCP starts sending keep-alive probes.$keepintvl_sec: The time (in seconds) between individual keep-alive probes. If this value is negative (default is -1),$keepidle_secis used for this value for maximum portability.
Notes:
This method ensures consistent keep-alive behavior across different platforms. When $keepintvl_sec is not specified, it applies the same duration ($keepidle_sec) to both the initial idle time and the retransmission interval.
On Windows, this method calls Sys::Socket#win_set_tcp_keepalive via WSAIoctl with the SIO_KEEPALIVE_VALS control code. The time values are converted to milliseconds.
On macOS, it uses the TCP_KEEPALIVE constant for the idle time, while on other systems like Linux, it uses TCP_KEEPIDLE. It also sets TCP_KEEPINTVL and SO_KEEPALIVE via Sys#setsockopt.
Note that Windows does not support retrieving these time settings via getsockopt, and the number of retries (TCP_KEEPCNT) is fixed by the system (typically 10 on Windows).
See also Sys::Socket#win_set_tcp_keepalive.
set_blocking
static method set_blocking : void ($fd : int, $blocking : int);
Sets the socket file descriptor $fd to blocking or non-blocking mode in a portable way.
Arguments:
$fd: The socket file descriptor.$blocking: Set to a non-zero value to enable blocking mode, or 0 to enable non-blocking mode.
inet_socketpair
static method inet_socketpair : void ($socket_fd1_ref : int*, $socket_fd2_ref : int*, $domain : int, $type : int, $protocol : int);
Creates a pair of connected internet sockets (client and server-accepted sockets) using the loopback address (127.0.0.1).
Parameters:
$socket_fd1_ref: Reference to the integer where the first socket file descriptor will be stored.$socket_fd2_ref: Reference to the integer where the second socket file descriptor will be stored.$domain: The communication domain (e.g.,AF_INET).$type: The communication type (e.g.,SOCK_STREAM).$protocol: The particular protocol to be used with the socket.
Notes:
This method provides a portable way to create connected socket pairs over the network loopback interface, which is particularly useful on platforms or environments where standard socketpair(2) with UNIX domain sockets is restricted or unavailable.
It internally creates two sockets, binds the server side to a temporary port on 127.0.0.1, listens for connections, connects the client side to it, accepts the connection, and then closes the listening server socket.
See Also
Copyright & License
Copyright (c) 2023 Yuki Kimoto
MIT License