lgsl icon indicating copy to clipboard operation
lgsl copied to clipboard

query live function problem with fetching data in cycle

Open Pok4 opened this issue 2 years ago • 14 comments

Hi guys,

I have a little problem with query live function (lgsl_query_live), i added few servers and they are in while cycle, i have this:

$get_servers = $dbh->query("SELECT * FROM ".getenv('DB_PREFIX')."greyfish_servers WHERE `gtrs`='0'");
		while ($row = $get_servers->fetch(PDO::FETCH_ASSOC)) {
			$server_ip = $row['ip'];
			$server_port = $row['port'];
			$server_type = $row['type'];
			$servid = $row['id'];
			
			//Query by Type
			switch ($server_type) {

			case  'cs16': {
					$result = lgsl_query_live('halflife', ''.$server_ip.'',  $server_port, $server_port, 0, "s");
					if (!isset($result['b']['status'])) {
						//offline
						$query_q_cs = $dbh->query("UPDATE ".getenv('DB_PREFIX')."greyfish_servers SET status='0', players='0',maxplayers='0' WHERE id='$servid'");
					} else {
						//online
						$host_cron = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $result['s']['name']);
						$map_cron = $result['s']['map'];
						$p_cron = $result['s']['players'];
						$maxp_cron = $result['s']['playersmax'];
						$dbh->query("UPDATE ".getenv('DB_PREFIX')."greyfish_servers SET status='1',hostname='$host_cron',map='$map_cron', players='$p_cron',maxplayers='$maxp_cron' WHERE id='$servid'");
					}
					break;
				}

			case 'csgo': {
					$result = lgsl_query_live('halflife', ''.$server_ip.'',  $server_port, $server_port, 0, "s");
					if (!isset($result['b']['status'])) {
						//offline
						$query_q_cs = $dbh->query("UPDATE ".getenv('DB_PREFIX')."greyfish_servers SET status='0', players='0',maxplayers='0' WHERE id='$servid'");
					} else {
						//online
						$host_cron = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $result['s']['name']);
						$map_cron = $result['s']['map'];
						$p_cron = $result['s']['players'];
						$maxp_cron = $result['s']['playersmax'];
						$dbh->query("UPDATE ".getenv('DB_PREFIX')."greyfish_servers SET status='1',hostname='$host_cron',map='$map_cron', players='$p_cron',maxplayers='$maxp_cron' WHERE id='$servid'");
					}
					break;
				}

the servers is from gametracker, current list: https://ibb.co/cYG0xM5

Sometimes servers is not querying properly (hostname and players/maxplayers).

I use phpfastcache and set it to 300 (seconds). All servers is updating on 300 seconds. But when this 300 seconds expire, all fetching starting again and query live function started again, and again, and again for all servers because while cycle. Maybe its need more time to fetch properly all servers.. Can you test it more servers (in cycle) with this function ?

Sometimes servers are properly fetching the data, but sometimes not. When i add servers from admin panel everything is ok, but this while cycle maybe kill all querying when started to using live function on and on...

Can you give me advice - what to do ?

Pok4 avatar Feb 27 '22 12:02 Pok4

It happens most often with samp/minecraft and ts3. (they are last in while cycle for querying)

Pok4 avatar Feb 27 '22 14:02 Pok4

I'm wondering about curl vs fsockopen method... Which is default for live function? And can be changed?

Pok4 avatar Feb 27 '22 14:02 Pok4

i try to use sleep function on every case in while cycle but without success. I set sleep(3). The script run slowly with this type of function, but nothing change... Maybe the problem is in query live function ?

Pok4 avatar Feb 28 '22 12:02 Pok4

Change the end of your $result line and query everything to see if that makes a diiference.

change

$result = lgsl_query_live('halflife', ''.$server_ip.'', $server_port, $server_port, 0, "s");

to

$result = lgsl_query_live('halflife', ''.$server_ip.'', $server_port, $server_port, 0, "sep");

TacTicTow avatar Mar 11 '22 15:03 TacTicTow

i try but without success... It works in same way..

Pok4 avatar Mar 11 '22 16:03 Pok4

Reproduction:

<?php
//lgsl (UDP Gaming Queries)
$lgsl_config['timeout'] = 0; //globalize
include(__DIR__.'/vendor/tltneon/lgsl/lgsl_files/lgsl_protocol.php');

$servers = [
"35.212.223.37"=>"25565",
"91.216.250.11"=>"27015",
"87.98.241.203"=>"27015",
"216.52.148.47"=>"27015",
"193.203.39.42"=>"7777",
"62.122.215.195"=>"9987",
"54.37.245.51"=>"27045",
];

$game = "";//globalize
foreach($servers as $k =>$v) {
	
	if($v == "9987") {
		$result = lgsl_query_live('ts3', ''.$k.'',$v, 10011, 0, "s");
		$game = "ts3";
	} else if($v == "7777") {
		$result = lgsl_query_live('samp', ''.$k.'',$v, $v, 0, "s");
		$game = "samp";
	} else if($v == "25565") {
		$result = lgsl_query_live('minecraft', ''.$k.'',$v, $v, 0, "s");
		$game="mc";
	} else {
		$result = lgsl_query_live('halflife', ''.$k.'',$v, $v, 0, "s");
		$game = "halflife";
	}
	
	if (!isset($result['b']['status'])) {
		echo 'offline';
	} else {
		$host_cron = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $result['s']['name']);
		$map_cron = $result['s']['map'];
		$p_cron = $result['s']['players'];
		$maxp_cron = $result['s']['playersmax'];
		
		echo "Game: $game<br/>$host_cron<br/>$map_cron<br/>$p_cron / $maxp_cron<hr/>";
	}
}

please, test it with more servers..

Pok4 avatar Mar 14 '22 21:03 Pok4

Here is a pic: https://i.ibb.co/p4hPCxQ/ss.png

For the test process, please use only F5 (few times) - 8~10 times. The servers not fetching properly the data.

Pok4 avatar Mar 14 '22 21:03 Pok4

Odd: I have always used the class file to read my servers. For example.

<?php
  include(../lgsl/lgsl_files/lgsl_class.php');
  $result = lgsl_query_live('callofduty4', '64.74.97.92', 28960, 28960, 0, "sep");
  print_r($result);  // there is will be an array of result of querying
?>

Just for the hell of it, try it and let me know if it makes a difference.

Change:

include(DIR.'/vendor/tltneon/lgsl/lgsl_files/lgsl_protocol.php');

To:

include(DIR.'/vendor/tltneon/lgsl/lgsl_files/lgsl_class.php');

TacTicTow avatar Mar 15 '22 01:03 TacTicTow

Nothing change. Maybe the problem is in query_live function... The strange thing is that the servers is not marked as offline, the status is not show the players,map and the hostname. The "offline" text not appear...

Pok4 avatar Mar 15 '22 10:03 Pok4

Hello @Pok4

Status sets to "offline" cause gets no certain data from server. It maybe be timeout if server didn't respond. Or just gets wrong packet data (if server can send several packets). Or because exceeded php max execution time

I'll test your code and find why it happens.

tltneon avatar Apr 01 '22 08:04 tltneon

I'm waiting for your response :)

Pok4 avatar Apr 01 '22 11:04 Pok4

So I testing your code and looking how it works.

Minecraft protocol works good with some servers - maybe protocol has changes in different MC versions. Need to look deeper.. image

SAMP sometimes just not to send packet :unamused: image

  • it can be fixed with additional sending query (I add it next commit) or $lgsl_config['retry_offline'] = true can be useful in this case.

And TS3 is clock-work. I can't connect to your server but two random servers works. image

tltneon avatar Apr 13 '22 07:04 tltneon

i see sometimes in random servers (cs 1.6), the players not showing from first click. When i click on the players tab second time - they shows. Maybe something is changed ...

$lgsl_config['retry_offline'] - can this works with lgsl query live function or only in lgsl_class ?

Thank you for your time, i know that this stuff is boring, but anyway, you are the guy :)

Pok4 avatar Apr 13 '22 09:04 Pok4

Players data and server data sended in different packets for 1.6 so there is nothing odd that sometimes players not showing. Its just loads next time

$lgsl_config['retry_offline'] works only in lgsl_class. If you don't want to use lgsl_class you need to make something like:

      $server = lgsl_query_live($type, $ip, $c_port, $q_port, $s_port, "sep");

      if (!$server ['b']['status'])
      {
        $server = lgsl_query_live($type, $ip, $c_port, $q_port, $s_port, "sep");
      }

tltneon avatar Apr 13 '22 12:04 tltneon