Raised This Month: $51 Target: $400
 12% 

Server Query


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Scherzo
Senior Member
Join Date: Feb 2007
Location: Kwidzyn, Poland
Old 01-22-2012 , 16:54   Server Query
Reply With Quote #1

Server Queries

Library to asynchronous request fetching information about other server of HL game. Server queries - Valve Developer Community


Available requests:

Ping. Online state check.
Code:
stock ServerPing(const szServer[], const szCallback[]);
Code:
new const SERVER_ADDRESS[] = "192.168.0.14:27015";

public ping()
{
        ServerPing(SERVER_ADDRESS, "cbPing");
        return PLUGIN_HANDLED;
}

public cbPing(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{
        if(success)
        {
                client_print(0, print_chat, "Server %s is Online latency = %d", szServer, latency);
        }
        else
        {
                client_print(0, print_chat, "Server %s is Offline", szServer);
        }
}


Info. Server name, map name, current number of players, max number of players, name of game.
Code:
stock ServerInfo(const szServer[], const szCallback[]);
Code:
new const SERVER_ADDRESS[] = "192.168.0.14:27015";

public info()
{       
        ServerInfo(SERVER_ADDRESS, "cbInfo");
        return PLUGIN_HANDLED;
}

public cbInfo(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{               
        if(!success)
                return;
                        
        new szName[64], szMap[64], szDirectory[64], szDescription[64];
        new iPlayers = 0;
        new iMaxPlayers = 0;
        
        ServerResponseParseInfo(Response, szName, 63, szMap, 63, szDirectory, 63, szDescription, 63, iPlayers, iMaxPlayers);
        
        client_print(0, print_chat, "Server name: %s", szName);
        client_print(0, print_chat, "Map: %s", szMap);
        client_print(0, print_chat, "Folder: %s", szDirectory);
        client_print(0, print_chat, "Description: %s", szDescription);
        client_print(0, print_chat, "Players: %d / %d", iPlayers, iMaxPlayers);
}


Rules. List of spoonable cvars.
! Response can be in few packes, so callback can be called few times per one use.


Code:
stock ServerRules(const szServer[], const szCallback[]);
Code:
new const SERVER_ADDRESS[] = "192.168.0.14:27015";

public rules()
{       
        if(ServerGetChallenge(SERVER_ADDRESS))
        {       
                ServerRules(SERVER_ADDRESS, "cbRules");
        }
        else
        {
                ServerChallenge(SERVER_ADDRESS, "cbChallengeRules");
        }
        return PLUGIN_HANDLED;
}

public cbChallengeRules(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{
        if(!success)
                return;
                
        //Get and cche challenge        
        new challenge_value = ServerResponseParseChallenge(Response);
        ServerSetChallenge(szServer, challenge_value);
        
        //Now can get Rules
        ServerRules(SERVER_ADDRESS, "cbRules");
}

public cbRules(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{
        if(!success)
                return;
        
        new szValue[32];
        if(ServerResponseParseRules(Response, len, "sv_alltalk", szValue, 31))
        {
                client_print(0, print_chat, "Rule: sv_alltalk ^"%s^"", szValue);
        }
}


Player. List players on server.
Code:
stock ServerPlayer(const szServer[], const szCallback[]);
Code:
new const SERVER_ADDRESS[] = "192.168.0.14:27015";

public player()
{       
        if(ServerGetChallenge(SERVER_ADDRESS))
        {       
                ServerPlayer(SERVER_ADDRESS, "cbPlayer");
        }
        else
        {
                ServerChallenge(SERVER_ADDRESS, "cbChallengePlayer");
        }
        return PLUGIN_HANDLED;
}

public cbChallengePlayer(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{
        if(!success)
                return;
                
        //Get and cache challenge       
        new challenge_value = ServerResponseParseChallenge(Response);
        ServerSetChallenge(szServer, challenge_value);
        
        //Now can get Player
        ServerPlayer(SERVER_ADDRESS, "cbPlayer");
}


public cbPlayer(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{
        if(!success)
                return;
                
        new iPlayers = ServerResponseParsePlayerNum(Response);
        
        client_print(0, print_chat, "Players: %d", iPlayers);
        
        new szNick[32], id, kills, Float:uptime;
        for(new i=0;i<iPlayers; i++)
        {
                if(ServerResponseParsePlayer(Response, i, id, szNick, 31, kills, uptime))
                {
                        client_print(0, print_chat, "%d %s |  kills:%d, uptime = %f", id, szNick, kills, uptime);
                }
        }
        
        ServerResponseSave("Player", Response, len);
}

Challenge. Required code for Player i Rules
Code:
stock ServerChallenge(const szServer[], const szCallback[]);
Code:
new const SERVER_ADDRESS[] = "192.168.0.14:27015";

public challenge()
{       
        ServerChallenge(SERVER_ADDRESS, "cbChallenge");
        return PLUGIN_HANDLED;
}

public cbChallenge(const szServer[], _A2A_TYPE, const Response[], len, success, latency)
{
        if(!success)
                return;
                
        new challenge_value = ServerResponseParseChallenge(Response);
        client_print(0, print_chat, "Challenge: %X", challenge_value);
}
Attached Files
File Type: inc server_query.inc (10.8 KB, 828 views)

Last edited by Scherzo; 01-22-2012 at 16:55.
Scherzo is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-22-2012 , 16:56   Re: Server Query
Reply With Quote #2

Is not already made ? https://forums.alliedmods.net/showthread.php?t=142858

EDIT: Ah, well, guess more complete.
__________________

Last edited by Arkshine; 01-22-2012 at 16:58.
Arkshine is offline
Scherzo
Senior Member
Join Date: Feb 2007
Location: Kwidzyn, Poland
Old 01-22-2012 , 17:03   Re: Server Query
Reply With Quote #3

Maybe, didn`t see that. Having few solutions for same problem is nice whenever.
Scherzo is offline
xakintosh
I run no-steam servers!
Join Date: Feb 2010
Location: Edge of nowhere
Old 01-23-2012 , 07:34   Re: Server Query
Reply With Quote #4

Thanks very usefful.
__________________
As soon as possible.
xakintosh is offline
Send a message via Yahoo to xakintosh Send a message via Skype™ to xakintosh
K.K.Lv
Veteran Member
Join Date: Aug 2008
Location: GameFolder
Old 05-27-2012 , 03:21   Re: Server Query
Reply With Quote #5

the Rules reply type has been changed.
will be better to catch packet to re-check it .
also I have post the info at this post.
http://forums.alliedmods.net/showpos...66&postcount=3
__________________
QQ:116268742
K.K.Lv is offline
Send a message via MSN to K.K.Lv
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-27-2012 , 06:24   Re: Server Query
Reply With Quote #6

Quote:
Originally Posted by K.K.Lv View Post
the Rules reply type has been changed.
will be better to catch packet to re-check it .
also I have post the info at this post.
http://forums.alliedmods.net/showpos...66&postcount=3
It didn't change. 0x56 is correct. On your screenshot you have "E" character, which is 0x45, that stands for answer. Everything is correct.

PHP Code:
    /**
     * Packets sent
     */
    
const A2S_PING      0x69;
    const 
A2S_INFO      0x54;
    const 
A2S_PLAYER    0x55;
    const 
A2S_RULES     0x56;
    
    
/**
     * Packets received
     */
    
const S2A_PING      0x6A;
    const 
S2A_CHALLENGE 0x41;
    const 
S2A_INFO      0x49;
    const 
S2A_INFO_OLD  0x6D// Old GoldSource, HLTV uses it
    
const S2A_PLAYER    0x44;
    const 
S2A_RULES     0x45;
    const 
S2A_RCON      0x6C
__________________

Last edited by xPaw; 05-27-2012 at 06:27.
xPaw is offline
K.K.Lv
Veteran Member
Join Date: Aug 2008
Location: GameFolder
Old 05-27-2012 , 23:45   Re: Server Query
Reply With Quote #7

Quote:
Originally Posted by xPaw View Post
It didn't change. 0x56 is correct. On your screenshot you have "E" character, which is 0x45, that stands for answer. Everything is correct.

PHP Code:
    /**
     * Packets sent
     */
    
const A2S_PING      0x69;
    const 
A2S_INFO      0x54;
    const 
A2S_PLAYER    0x55;
    const 
A2S_RULES     0x56;
 
    
/**
     * Packets received
     */
    
const S2A_PING      0x6A;
    const 
S2A_CHALLENGE 0x41;
    const 
S2A_INFO      0x49;
    const 
S2A_INFO_OLD  0x6D// Old GoldSource, HLTV uses it
    
const S2A_PLAYER    0x44;
    const 
S2A_RULES     0x45;
    const 
S2A_RCON      0x6C

in pass, we usually use this
Code:
if (buffer[4] == 0x45)
but now
I use this
Code:
if (buffer[13] == 0x45)
you can use this inc file to check it .
you will find the first var was wrong .
all I have test .
__________________
QQ:116268742
K.K.Lv is offline
Send a message via MSN to K.K.Lv
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-28-2012 , 04:11   Re: Server Query
Reply With Quote #8

Quote:
Originally Posted by K.K.Lv View Post
Code:
if (buffer[13] == 0x45)
This is incorrect. You have to properly read split packets to receive correct answer.
https://developer.valvesoftware.com/...eries#Protocol
__________________
xPaw is offline
K.K.Lv
Veteran Member
Join Date: Aug 2008
Location: GameFolder
Old 05-28-2012 , 04:34   Re: Server Query
Reply With Quote #9

I have test my own steam server, you can use WPE Pro to cacth packet.
__________________
QQ:116268742
K.K.Lv is offline
Send a message via MSN to K.K.Lv
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-28-2012 , 08:18   Re: Server Query
Reply With Quote #10

Quote:
Originally Posted by K.K.Lv View Post
I have test my own steam server, you can use WPE Pro to cacth packet.
What are you trying to say?

Changing [5] to [13] is a work around, and won't lead to proper result. It's shifted because when packets are split, there is information prepended about split packets. You really should read wiki article.
__________________

Last edited by xPaw; 05-28-2012 at 08:18.
xPaw is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:27.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode