AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   is worth and possible cache user ip and auth in index as global string var? (https://forums.alliedmods.net/showthread.php?t=222499)

seriousspot 08-03-2013 12:48

is worth and possible cache user ip and auth in index as global string var?
 
Well i got my custom plugin its about 4k lines and i thinking about few simple optimisations so get_user_ip and get_user_authid exists in 11 functions its useless to getting same results for same id everytime its called. As far i have done research that calling such things may cause cpu usage, why just don't save it to memory for every player index, on client putinserver event it could be cached to an string variable, as player disconnect called client disconnect event, the ip and authid will be reseted to zero. Also its impossible that authid or ip can be changed in realtime @server.


Okay for the questions..

Is this worth (i see less cpu chokes, slutters, less cpu usage)?

Is this possible (cache ip and auth in player index (global string variable))?

I know that this will use a bit more memory, can i expect memory garbages?

YamiKaitou 08-03-2013 12:51

Re: is worth and possible cache user ip and auth in index as global string var?
 
Quote:

Originally Posted by seriousspot (Post 2004881)
Is this worth (i see less cpu chokes, slutters, less cpu usage)?

Probably not, but it depends on the number of times it gets called in total

Quote:

Is this possible (cache ip and auth in player index (global variable))?
Yes

seriousspot 08-03-2013 13:07

Re: is worth and possible cache user ip and auth in index as global string var?
 
Quote:

Originally Posted by YamiKaitou (Post 2004883)
Probably not, but it depends on the number of times it gets called in total

as expecting caching it one and the only time in client putinserver event


and how about memory garbages?

YamiKaitou 08-03-2013 14:40

Re: is worth and possible cache user ip and auth in index as global string var?
 
Quote:

Originally Posted by seriousspot (Post 2004888)
as expecting caching it one and the only time in client putinserver event

What I meant was it all depends on how many times you call get_user_ip or get_user_authid throughout the life of that players connection.


Quote:

and how about memory garbages?
Don't understand the question, hence why I didn't answer it

seriousspot 08-03-2013 15:28

Re: is worth and possible cache user ip and auth in index as global string var?
 
Quote:

Originally Posted by YamiKaitou (Post 2004936)
What I meant was it all depends on how many times you call get_user_ip or get_user_authid throughout the life of that players connection.



Don't understand the question, hence why I didn't answer it

alot of times, its management, technical, stastics, admin AIO plugin


memory leaks*, I am sorry

ConnorMcLeod 08-03-2013 15:47

Re: is worth and possible cache user ip and auth in index as global string var?
 
PHP Code:

#include < amxmodx >

public plugin_init()
{
    
register_clcmd("say /info""ClCmd_Say_Info");
}

GGETPLAYERAUTHID(idbGet false)
{
    static 
szAuthid[33][32];
    if( 
bGet )
    {
        
get_user_authid(idszAuthid[id], charsmax(szAuthid[]));
    }
    return 
szAuthid[id];
}

public 
client_connect(id)
{
    
GETPLAYERIP(idtrue);
}

public 
client_authorized(id)
{
    
GGETPLAYERAUTHID(idtrue);
}

GETPLAYERAUTHID(idbGet false)
{
    static 
szAuthid[33][32];
    if( 
bGet )
    {
        
get_user_authid(idszAuthid[id], charsmax(szAuthid[]));
    }
    return 
szAuthid[id];
}

GETPLAYERIP(idbGet false)
{
    static 
szAddress[33][16];
    if( 
bGet )
    {
        
get_user_ip(idszAddress[id], charsmax(szAddress[]), 1);
    }
    return 
szAddress[id];
}

public 
ClCmd_Say_Info(id)
{
    
client_print(idprint_chat"Your IP is ^"%s^" and your SteamID is ^"%s^""GETPLAYERIP(id), GETPLAYERAUTHID(id));
}

// function in which you need to use steamid more that one and ip more than once :
public function( id )
{
    static 
szAuthid[32], szIp[16];
    
szAuthid GETPLAYERAUTHID(id);
    
szIp GETPLAYERIP(id);

    if( !(
'0' <= szAuthid[10] <= '9') && !IsLocalIpszIp ) )
    {
        
client_print(idprint_chat"Your IP is ^"%s^" and your SteamID is ^"%s^" tell us you are not using steam"szIpszAuthid);
    }
}

IsLocalIp( const IP[] )
{
    return 
false;



seriousspot 08-03-2013 16:25

Re: is worth and possible cache user ip and auth in index as global string var?
 
Quote:

Originally Posted by ConnorMcLeod (Post 2004973)
PHP Code:

#include < amxmodx >

public plugin_init()
{
    
register_clcmd("say /info""ClCmd_Say_Info");
}

GGETPLAYERAUTHID(idbGet false)
{
    static 
szAuthid[33][32];
    if( 
bGet )
    {
        
get_user_authid(idszAuthid[id], charsmax(szAuthid[]));
    }
    return 
szAuthid[id];
}

public 
client_connect(id)
{
    
GETPLAYERIP(idtrue);
}

public 
client_authorized(id)
{
    
GGETPLAYERAUTHID(idtrue);
}

GETPLAYERAUTHID(idbGet false)
{
    static 
szAuthid[33][32];
    if( 
bGet )
    {
        
get_user_authid(idszAuthid[id], charsmax(szAuthid[]));
    }
    return 
szAuthid[id];
}

GETPLAYERIP(idbGet false)
{
    static 
szAddress[33][16];
    if( 
bGet )
    {
        
get_user_ip(idszAddress[id], charsmax(szAddress[]), 1);
    }
    return 
szAddress[id];
}

public 
ClCmd_Say_Info(id)
{
    
client_print(idprint_chat"Your IP is ^"%s^" and your SteamID is ^"%s^""GETPLAYERIP(id), GETPLAYERAUTHID(id));
}

// function in which you need to use steamid more that one and ip more than once :
public function( id )
{
    static 
szAuthid[32], szIp[16];
    
szAuthid GETPLAYERAUTHID(id);
    
szIp GETPLAYERIP(id);

    if( !(
'0' <= szAuthid[10] <= '9') && !IsLocalIpszIp ) )
    {
        
client_print(idprint_chat"Your IP is ^"%s^" and your SteamID is ^"%s^" tell us you are not using steam"szIpszAuthid);
    }
}

IsLocalIp( const IP[] )
{
    return 
false;




done myself i think more efficient way, but on client_disconnect don't know how to reset


PHP Code:

#include <amxmodx>
#include <regex>


#define REGEX_IP "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
#define REGEX_AUTH "^^STEAM_0:(0|1):\d+$"

#define is_valid_ip(%1) (regex_match_c(%1, regex_ip, regex_return) > 0)
#define is_valid_auth(%1) (regex_match_c(%1, regex_auth, regex_return) > 0)

new Regex:regex_ipRegex:regex_authregex_returng_AUTH[33][64], g_IP[33][16]

public 
plugin_init() {
    
// regex stuff
    
new error[2]
    
regex_ip regex_compile(REGEX_IPregex_returnerrorcharsmax(error))
    
regex_auth regex_compile(REGEX_AUTHregex_returnerrorcharsmax(error))
    
    
// testing
    
    
register_concmd("/test""echo")
}

public 
client_authorized(id) {
    new 
AUTH[64// future proof
    
    
get_user_authid(idAUTHcharsmax(AUTH))
    
    if (
is_valid_auth(AUTH)) {
        
g_AUTH[id] = AUTH
    
}
    
    else {
        
// client is not authorized
        
return 1
    
}
    
    return 
0
}

public 
client_connect(id) {
    new 
IP[16
    
    
get_user_ip(idIPcharsmax(IP), 1)
    
    if (
is_valid_ip(IP)) {
        
g_IP[id] = IP
    
}
    
    else {
        
// client ip is not valid
        
return 1
    
}
    
    return 
0
}

/*

        TODO

public client_disconnect(id) {
    new null = 0
    
    g_AUTH[id] = null
    g_IP[id] = null
}


*/

public echo(id) {
    
client_print(idprint_console"AUTH: %s, IP: %s"g_AUTH[id], g_IP[id])



fysiks 08-04-2013 02:15

Re: is worth and possible cache user ip and auth in index as global string var?
 
Regex is NOT a more efficient method and should not be used here since the possible outputs of get_user_authid() and get_user_ip() are very limited (in fact, the IP output from the function will always be valid). Connor's method will be more efficient.

ConnorMcLeod 08-04-2013 04:56

Re: is worth and possible cache user ip and auth in index as global string var?
 
But you can use global variables as you did, also steamid array size doesn't need to be 64, 32 is enough.

Backstabnoob 08-04-2013 07:31

Re: is worth and possible cache user ip and auth in index as global string var?
 
Or just use the AMXX natives each time you need them, because they're fast and caching them is trivial and can only cause headaches.


All times are GMT -4. The time now is 15:58.

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