Thread: Sockets Problem
View Single Post
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 07-06-2014 , 07:09   Re: Sockets Problem
Reply With Quote #12

Here are some corrections in your plugin. Don't declare huge arrays ( like new lines[30][100] ) in a function which is being looped frequently. As Black Rose said, Threaded MySql queries are better. You can make your SqlDb to accept remote sql connections form your GameServer IP only.

Form what i had observed, the parser.php file get newer data for more than 5 secs, so you can increase the the set_task() time.

PHP Code:
#include <amxmodx>
#include <sockets>

#define PLUGIN_NAME     "Radio BZ"
#define PLUGIN_AUTHOR   "aron9forever"
#define PLUGIN_VERSION  "1.1"

#define TASKREAD         12312

new g_sckweb

const SIZE 63
new const REMOTE_HOST[] =    "piratefm.ro"
new const SCRIPT_NAME[] = "/comm/parser.php"

/* new g_esong[64]
new g_rsong[64]
new g_dsong[64] */

// Make all the above vars as a 2D Array
new g_szSongs[3][SIZE+1];
new 
bool:isDisconnected true;

new const 
gVarsSizes[] = { 10// Size of "some_value" - 10
                            
11// Size of "some_value1" - 11
                            
11  // Size of "some_value2" - 11
}

new const 
gMessages[][] = {

    
".v[AMXX].e PirateFM Rap canta: -%s-",
    
".v[AMXX].e PirateFM Electro canta: -%s-",
    
".v[AMXX].e PirateFM Dub canta: -%s-"
}
    
public 
plugin_init()
{
    
register_plugin(PLUGIN_NAMEPLUGIN_VERSIONPLUGIN_AUTHOR)
    
set_task(10.0"tests", .flags="b")
    
    
register_clcmd"say /test""cmdTest" );
    
//set_task(40.0,"announce",_,_,_,"b",0)
}

public 
cmdTest(id)
{
    
client_printidprint_chat"g_eSong - %s"g_szSongs[0] );
    
client_printidprint_chat"g_rSong - %s"g_szSongs[1] );
    
client_printidprint_chat"g_dSong - %s"g_szSongs[2] );
}

public 
tests()
{
    
connect_web()
}

public 
connect_web()
{
    static 
constring[512], error;
    
error 0;

    if( !
isDisconnected disconnect_web();

    
g_sckweb socket_open(REMOTE_HOST80SOCKET_TCPerror)
    
isDisconnected false;

    if (
g_sckweb 0)
    {
        
chat_color(0".v[AMXX].e Socket Connected");
        
formatex(constring,511,"GET %s HTTP/1.1^nHost: %s^n^n",SCRIPT_NAME,REMOTE_HOST)
        
write_web(constring)
        
read_web()
    }
    else
    {
        switch (
error)
        {
            case 
1: { chat_color(0".v[AMXX].e Error creating socket"); }
            case 
2: { chat_color(0".v[AMXX].e Error resolving remote hostname"); }
            case 
3: { chat_color(0".v[AMXX].e Error connecting socket"); }
        }
    }
    return 
PLUGIN_CONTINUE
}

public 
read_web()
{
    
// static line_variable[SIZE + 1], line_value[SIZE + 1]
    
static some_values[3][SIZE+1];
    static 
szBuffer[512]
    static 
bool:iReadiRead false;
    static 
jkiiReadLine;
    
iReadLine 00;

    if (
socket_change(g_sckweb100))
    {
        
/*new lines[30][100], count = 0 ;*/
        
socket_recv(g_sckwebszBuffer511)
        
iRead true;

        
// Read Full String
        
while( szBuffer[i] != '^0' )
        {
            
// Are we at |"|some_value" "<value>" ??
            
if( szBuffer[i] == '^"' )
            { 
                
// log_amx( "%s", szBuffer[i] );
                // Position of the char '"' (i) + length of var (gVarsSizes) + '"' (1) + <space> (1) + '"' (1) + 1
                // "some_value" "<Value>"
                // log_amx( "%d", iReadLine );
                
=  gVarsSizes[iReadLine] + 4;
                
                
// log_amx( "%s", szBuffer[j] );
                
0;
                
// Read until we reach "some_value" "<value>|"| 
                
while( szBuffer[j] != '^"' && (<= SIZE) && (511) )
                {
                    
some_values[iReadLine][k] = szBuffer[j];
                    
k++; j++;
                }
                
iReadLine++;
                
some_values[iReadLine-1][k] = '^0'// Complete the string with a null character
                
j;
            }

            
// All three lines are Read, So stop looping now
            
if( iReadLine ) break;
            
i++;
        }

        for( 
03i++ )
        {
            if( !
equal(some_values[i], g_szSongs[i]) )
            {
                
// set_hudmessage(0, 255, 0, 0.02, -1.0);
                // show_hudmessage(0, "PirateFM Rap canta: -%s-", some_values[i]);
                
chat_color(0gMessages[i], some_values[i]);
                
g_szSongs[i] = some_values[i];
            }
        }            
        
        
/* count = ExplodeString(lines, 50, 119, szBuffer, 13)
        for(new i=0;i<count;i++)
        {
            parse(lines[i], line_variable, SIZE, line_value, SIZE)
            if (equal(line_variable, "some_value"))
            {
                if(!equal(line_value, g_rsong))
                {
                    set_hudmessage(0, 255, 0, 0.02, -1.0);
                    show_hudmessage(0, "PirateFM Rap canta: -%s-", line_value);
                    chat_color(0, ".v[AMXX].ePirateFM Rap canta: -%s-", line_value)
                    g_rsong = line_value
                }
            }
            if (equal(line_variable, "some_value2"))
            {
                if(!equal(line_value, g_esong))
                {
                    set_hudmessage(0, 255, 0, 0.02, -1.0);
                    show_hudmessage(0, "PirateFM Electro canta: -%s-", line_value);
                    chat_color(0, ".v[AMXX].ePirateFM Electro canta: -%s-", line_value)
                    g_esong = line_value
                }
            }
            if (equal(line_variable, "some_value3"))
            {
                if(!equal(line_value, g_dsong))
                {
                    set_hudmessage(0, 255, 0, 0.02, -1.0);
                    show_hudmessage(0, "PirateFM Dub canta: -%s-", line_value);
                    chat_color(0, ".v[AMXX].ePirateFM Dub canta: -%s-", line_value)
                    g_dsong = line_value
                }
            }
        } */
    
}

    
remove_taskTASKREAD );
    if (!
iRead)
    {
        
// We didn't read, so set a task
        
set_task(0.5"read_web"TASKREAD );
    }
    else
        
disconnect_web();

    return 
PLUGIN_HANDLED
}

public 
write_web(text[])
{
    
socket_send(g_sckwebtext511)
}

public 
disconnect_web()
{
    
isDisconnected true;
    
chat_color(0".v[AMXX].e Socket disconnected")
    
socket_close(g_sckweb)
}

/* stock ExplodeString( p_szOutput[][], p_nMax, p_nSize, p_szInput[], p_szDelimiter ) { // Function by xeroblood
    new nIdx = 0, l = strlen(p_szInput)
    new nLen = (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput, p_szDelimiter ))
    while( (nLen < l) && (++nIdx < p_nMax) )
        nLen += (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput[nLen], p_szDelimiter ))
    return nIdx
} */

stock chat_color(const id, const input[], any:...)
{
    new 
count 1players[32]
    static 
msg[191]
    
vformat(msg190input3)

    
replace_all(msg190".v""^4")
    
replace_all(msg190".g""^1")
    
replace_all(msg190".e""^3")

    if (
idplayers[0] = id; else get_players(playerscount"ch")
    {
        for (new 
0counti++)
        {
            if (
is_user_connected(players[i]))
            {
                
message_begin(MSG_ONE_UNRELIABLEget_user_msgid("SayText"), _players[i])
                
write_byte(players[i]);
                
write_string(msg);
                
message_end();
            }
        }
    }

__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here

Last edited by Shooting King; 07-06-2014 at 07:11.
Shooting King is offline