15declare(strict_types=1);
17namespace raklib\generic;
19use
function socket_close;
20use
function socket_create;
21use
function socket_last_error;
22use
function socket_set_block;
23use
function socket_set_nonblock;
24use
function socket_set_option;
25use
function socket_strerror;
37 protected \Socket $socket;
43 $socket = @socket_create($ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, SOL_UDP);
44 if($socket ===
false){
45 throw new \RuntimeException(
"Failed to create socket: " . trim(socket_strerror(socket_last_error())));
47 $this->socket = $socket;
50 socket_set_option($this->socket, IPPROTO_IPV6, IPV6_V6ONLY, 1);
54 public function getSocket() : \
Socket{
58 public function close() : void{
59 socket_close($this->socket);
62 public function getLastError() : int{
63 return socket_last_error($this->socket);
70 @socket_set_option($this->socket, SOL_SOCKET, SO_SNDBUF, $size);
79 @socket_set_option($this->socket, SOL_SOCKET, SO_RCVBUF, $size);
88 @socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, [
"sec" => $seconds,
"usec" => $microseconds]);
93 public function setBlocking(
bool $blocking) : void{
95 socket_set_block($this->socket);
97 socket_set_nonblock($this->socket);
setRecvTimeout(int $seconds, int $microseconds=0)