Raised This Month: $ Target: $400
 0% 

[Socket] Detect when remote host is offline


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 10-31-2019 , 07:56   [Socket] Detect when remote host is offline
Reply With Quote #1

Hi folks, how i can detect when remote server is offline?

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

#define CONNECTION_TIMEOUT 10

new g_Host;
new 
g_Pass;

new 
g_Socket;
new 
g_Forward;
new 
g_Password[32];
new 
g_RconChallenge[32];
new 
g_ConnectionTime;

public 
plugin_init()
{
    
register_plugin("Pug Mod (HLTV)","0.0.1","SmileY");
    
    
g_Host create_cvar("amx_rcon_host","192.168.237.1:27020",FCVAR_NONE,"Remote address");    
    
g_Pass create_cvar("amx_rcon_pass","2133",FCVAR_NONE,"Remote rcon password");
    
    
register_concmd("amx_rcon","RconCommand",ADMIN_RCON);
}

public 
plugin_cfg()
{
    
SocketOpen();
}

public 
plugin_end()
{
    
SocketClose();
}

public 
RconCommand(id,Level)
{
    if(
access(id,Level))
    {
        new 
Command[128];
        
read_args(Command,charsmax(Command));
        
remove_quotes(Command);
        
        
SocketSendCommand(Command);
    }
    
    return 
PLUGIN_HANDLED;
}

SocketOpen()
{
    if(
g_Socket == 0)
    {
        new 
Host[MAX_IP_WITH_PORT_LENGTH];
        
get_pcvar_string(g_Host,Host,charsmax(Host));
        
        
get_pcvar_string(g_Pass,g_Password,charsmax(g_Password));
        
        new 
IP[MAX_IP_LENGTH],Port[6];
        
strtok(Host,IP,charsmax(IP),Port,charsmax(Port),':');  
        
        if(
IP[0] && Port[0] && g_Password[0])
        {
            new 
Error;
            
g_Socket socket_open(IP,str_to_num(Port),SOCKET_UDP,Error,SOCK_NON_BLOCKING);

            if(
Error == SOCK_ERROR_OK)
            {
                if(
socket_is_writable(g_Socket,0))
                {
                    new 
Data[32];
                    
formatex(Data,sizeof(Data),"%c%c%c%cchallenge rcon",0xFF,0xFF,0xFF,0xFF);
                    
                    if(
socket_send2(g_Socket,Data,charsmax(Data)) != -1)
                    {
                        
g_ConnectionTime get_systime();
                        
                        
g_Forward register_forward(FM_StartFrame,"SocketRecvChallenge");
                    }
                }
            }
        }
    }
    
    return 
0;
}

public 
SocketRecvChallenge()
{
    if(
g_Socket)
    {
        if((
get_systime() - g_ConnectionTime) < CONNECTION_TIMEOUT)
        {
            if(
socket_is_readable(g_Socket,0))
            {
                new 
Data[128];
    
                if(
socket_recv(g_Socket,Data,charsmax(Data)) > 0)
                {
                    new 
Dummy[64];
                    
parse(Data,Dummy,charsmax(Dummy),Dummy,charsmax(Dummy),g_RconChallenge,charsmax(g_RconChallenge));
                    
                    
unregister_forward(FM_StartFrame,g_Forward);
                }
            }
        }
        else
        {
            
SocketClose();
        }
    }
}

SocketSendCommand(const Command[])
{
    if(
g_Socket)
    {
        if(
socket_is_writable(g_Socket,0))
        {
            new 
Data[128];
            
format(Data,charsmax(Data),"%c%c%c%crcon %s ^"%s^" %s",0xFF,0xFF,0xFF,0xFF,g_RconChallenge,g_Password,Command);    
            
            
socket_send2(g_Socket,Data,charsmax(Data));
        }
    }
}

SocketClose()
{
    if(
socket_close(g_Socket))
    {
        
g_Socket 0;
    }
    
    
unregister_forward(FM_StartFrame,g_Forward);

I have set up a variable to check time and disconnect if server do not recv rcon challenge at defined time, but have a way to do it directly with sockets?

Thanks
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
 



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:43.


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