View Single Post
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-10-2011 , 21:23   [INC] Sockets Forwards
Reply With Quote #1

Under construction
Sockets Forwards
This include provides forwards for events related to sockets. These events eliminate the need to use a task or thinking entity in a plugin to constantly monitor for changes. The usage is pretty simple so I don't think much explanation is necessary beyond the examples below; the one thing I do recommend is always close a listening socket before map-change to prevent errors when a listen is again invoked (close socket at plugin_end()). If you use AMXX-Studio, you can play with the server example below with the sockets terminal (Tools->Sockets Terminal).

Forwards
  • Sock_Forwards_Connected( iSocket ) - Called when onnection is made when using Sock_Forwards_Open().
  • Sock_Forwards_Disconnected( iSocket ) - Called when the remote host closed the connection.
  • Sock_Forwards_ConnectionRequest( iSocket ) - Called when a listening socket gets a connection request. You must accept connection request here.
  • Sock_Forwards_IncomingData( iSocket , iNumBytes ) - Called when data has been received and needs to be read. You MUST read data in this forward or the forward will keep firing until there is no data to be read.
  • Sock_Forwards_TimedOut( iSocket ) - Called when no activity has taken place on a given socket for 15 seconds. This does not apply to listening sockets.
Commands

To avoid confusion and prevent unexpected behavior, I renamed all of the sockets2 functions in the sockets_forwards include. This is needed because the Open, Close, Listen and Accept commands are modified to make the forwards possible.
  • Sock_Forwards_Open()
  • Sock_Forwards_Close()
  • Sock_Forwards_Listen()
  • Sock_Forwards_Accept()
  • Sock_Forwards_Change()
  • Sock_Forwards_Recv()
  • Sock_Forwards_RecvFrom()
  • Sock_Forwards_Send()
  • Sock_Forwards_SendBinary()
  • Sock_Forwards_SendTo()
  • Sock_Forwards_IsWritable()
  • Sock_Forwards_DataAvailable()
  • Sock_Forwards_GetPeerAddr()

Example #1 - Simple TCP echo server. Connect and send data and it will echo'd in server console
PHP Code:

#include <amxmodx>
#include <sockets_forwards>

new const Version[] = "0.1";

const 
TCP_Listen_Port 2345;

new 
g_Socket;

public 
plugin_init() 
{
    
register_plugin"Example TCP Server" Version "bugsy" );
    
    new 
szServerIP15 ] , iError;
    
get_user_ipszServerIP charsmaxszServerIP ) , );
    
    if ( !( 
g_Socket g_Socket Sock_Forwards_ListenszServerIP TCP_Listen_Port SOCKET_TCP iError ) ) || iError )
    {
        
set_fail_state"Error opening listen socket" );
    }
}

public 
plugin_end() 
{
    
Sock_Forwards_Closeg_Socket );
}

public 
Sock_Forwards_ConnectionRequestiSocket )
{
    new 
iError iNewSocket Sock_Forwards_AcceptiSocket iError );
    
    
server_print"Connection request[Listen Socket=%d, New socket=%d]" iSocket iNewSocket );
}

public 
Sock_Forwards_IncomingDataiSocket iBytes )
{
    new 
szData64 ];
    
    
Sock_RecviSocket szData sizeofszData ) );
    
    
server_print"Incoming data [Socket=%d, Bytes=%d]" iSocket iBytes );
    
server_print"Data=[%s]" szData );
}

public 
Sock_Forwards_TimedOutiSocket )
{
    
server_print"Socket timed out [Socket=%d]" iSocket );
}

public 
Sock_Forwards_DisconnectediSocket )
{
    
server_print"Socket closed [Socket=%d]" iSocket );

Example #2 - Retrieve image file from http server
PHP Code:
#include <amxmodx>
#include <sockets_forwards>

new const Version[] = "0.1";

new 
g_File g_PacketNum;

public 
plugin_init()
{
    
register_plugin"Sockets Forwards Client Demo" Version "bugsy" );

    
register_concmd"test" "TestCmd" );
}

public 
TestCmd()
{
    new 
iSocket iError;
    if ( ( ( 
iSocket Sock_Forwards_Open"i.telegraph.co.uk" 80 SOCKET_TCP iError ) ) == INVALID_SOCKET ) && iError )
    {
        
server_print"Error opening socket [%d]" iError );
    }
    else
    {
        
server_print"[%d] Initiating connection" iSocket );
        
g_File fopen"squirrel.jpg" "wb" );
        
g_PacketNum 0;
    }
}

public 
Sock_Forwards_ConnectediSocket )
{
    new 
iError;
    
    
server_print"[%d] Socket Connected, Sending Request" iSocket );

    new 
szPacket[] = "GET /multimedia/archive/03571/potd-squirrel_3571152k.jpg HTTP/1.1^r^nHost: i.telegraph.co.uk^r^n^r^n";
    
Sock_Forwards_SendiSocket szPacket sizeofszPacket ) , iError );
}

public 
Sock_Forwards_TimedOutiSocket )
{
    
server_print"[%d] Socket Timed Out" iSocket );
    
    
Sock_Forwards_CloseiSocket );
}

public 
Sock_Forwards_DisconnectediSocket )
{
    
server_print"[%d] Socket Disconnected" iSocket );
    
    
fcloseg_File );
    
Sock_Forwards_CloseiSocket );
}

public 
Sock_Forwards_IncomingDataiSocket iBytesReceived )
{
    
server_print"[%d] Received %d Bytes of Data" iSocket iBytesReceived );
    
    static 
szData1024 ] , iDataStart iBytes;
    
iBytes Sock_Forwards_RecviSocket szData charsmaxszData ) );
    
    if ( ( ++
g_PacketNum == ) && ( ( iDataStart strfindszData "^r^n^r^n" ) ) > -) )
        
iDataStart += 4;
    else
        
iDataStart 0;
        
    
fwrite_blocksg_File szDataiDataStart ] , ( iBytes iDataStart ) , BLOCK_BYTE );

__________________

Last edited by Bugsy; 03-12-2016 at 17:00.
Bugsy is offline