diff --git a/engine.php b/engine.php index a17fae3..56e6107 100644 --- a/engine.php +++ b/engine.php @@ -138,7 +138,7 @@ function server_list($address = null) $request = "{$this->header}getserversExt $game $protocol $extra_flags"; $socket = new EngineSocket(); - $response = $socket->write($address, $request); + $socket->write($address, $request); $packet_index = 0; $packet_count = 1; @@ -232,10 +232,10 @@ private function parse_master_response($data, &$index, &$count, &$servers) $high = sprintf('%02x', ord($nextbyte())); $low = sprintf('%02x', ord($nextbyte())); $ip[] = "$high$low"; - $port = ord($nextbyte()) << 8; - $port |= ord($nextbyte()); - $servers[] = new Engine_Address($this, "[".implode(":", $ip)."]", $port); } + $port = ord($nextbyte()) << 8; + $port |= ord($nextbyte()); + $servers[] = new Engine_Address($this, "[".implode(":", $ip)."]", $port); } } } @@ -302,42 +302,58 @@ class EngineSocket { static $default_write_timeout = 1000; // 1 millisecond static $default_read_timeout = 500000; // 500 milliseconds - private $socket = null; + private $socket6 = null; + private $socket4 = null; - function socket() + function socket( $address ) { - if ( $this->socket == null ) - { - $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - $this->set_timeout( + if ( str_starts_with( $address->host, "[" ) ) + { + if ( $this->socket6 == null ) + { + $this->socket6 = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP); + $this->set_timeout($this->socket6, EngineSocket::$default_write_timeout, EngineSocket::$default_read_timeout - ); + ); + } + return $this->socket6; + } + else + { + if ( $this->socket4 == null ) + { + $this->socket4 = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + $this->set_timeout($this->socket4, + EngineSocket::$default_write_timeout, + EngineSocket::$default_read_timeout + ); + } + return $this->socket4; } - return $this->socket; } - function set_timeout($send_microseconds, $receive_microseconds = -1) + function set_timeout($socket, $send_microseconds, $receive_microseconds = -1) { - if ( !$this->socket ) + if ( !$socket ) return; if ( $receive_microseconds < 0 ) $receive_microseconds = $send_microseconds; if ( $send_microseconds > 0 ) - socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, + socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 0, 'usec' => $send_microseconds)); if ( $receive_microseconds > 0 ) - socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, + socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 0, 'usec' => $receive_microseconds)); } function request(Engine_Address $address, $request) { - if ( !$this->socket() ) + if ( !$this->socket($address) ) return false; $request_command = strtok($request, " "); @@ -357,15 +373,18 @@ function request(Engine_Address $address, $request) function write($address, $data) { - socket_sendto($this->socket(), $data, strlen($data), - 0, $address->host, $address->port); + $host = trim($address->host, "[]"); + socket_sendto($this->socket($address), $data, strlen($data), + 0, $host, $address->port); } function read($address, $read_size) { $received = ""; - socket_recvfrom($this->socket(), $received, $read_size, - 0, $address->host, $address->port); + $from = ''; + $port = 0; + socket_recvfrom($this->socket($address), $received, $read_size, + 0, $from, $port); return $received; } @@ -678,9 +697,14 @@ function server_list_html($addresses, $css_prefix="dptable_") if ( isset($status["sv_statsURL"]) ) $link .= " " . new HTML_Link("Stats", $status["sv_statsURL"], true, array("target" => "_blank")); + $server_name = $address->protocol->string->to_html($status["server.name"]); + + if ( str_starts_with( $address->host, "[" ) ) + $server_name = $server_name . " [IPv6]"; + $table->data_row( [ - $address->protocol->string->to_html($status["server.name"]), + $server_name, // htmlspecialchars($status["server.game"]), htmlspecialchars($status["server.version"]), htmlspecialchars($status["mapname"]),