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 szServerIP[ 15 ] , iError;
get_user_ip( 0 , szServerIP , charsmax( szServerIP ) , 1 );
if ( !( g_Socket = g_Socket = Sock_Forwards_Listen( szServerIP , TCP_Listen_Port , SOCKET_TCP , iError ) ) || iError )
{
set_fail_state( "Error opening listen socket" );
}
}
public plugin_end()
{
Sock_Forwards_Close( g_Socket );
}
public Sock_Forwards_ConnectionRequest( iSocket )
{
new iError , iNewSocket = Sock_Forwards_Accept( iSocket , iError );
server_print( "Connection request[Listen Socket=%d, New socket=%d]" , iSocket , iNewSocket );
}
public Sock_Forwards_IncomingData( iSocket , iBytes )
{
new szData[ 64 ];
Sock_Recv( iSocket , szData , sizeof( szData ) );
server_print( "Incoming data [Socket=%d, Bytes=%d]" , iSocket , iBytes );
server_print( "Data=[%s]" , szData );
}
public Sock_Forwards_TimedOut( iSocket )
{
server_print( "Socket timed out [Socket=%d]" , iSocket );
}
public Sock_Forwards_Disconnected( iSocket )
{
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_Connected( iSocket )
{
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_Send( iSocket , szPacket , sizeof( szPacket ) , iError );
}
public Sock_Forwards_TimedOut( iSocket )
{
server_print( "[%d] Socket Timed Out" , iSocket );
Sock_Forwards_Close( iSocket );
}
public Sock_Forwards_Disconnected( iSocket )
{
server_print( "[%d] Socket Disconnected" , iSocket );
fclose( g_File );
Sock_Forwards_Close( iSocket );
}
public Sock_Forwards_IncomingData( iSocket , iBytesReceived )
{
server_print( "[%d] Received %d Bytes of Data" , iSocket , iBytesReceived );
static szData[ 1024 ] , iDataStart , iBytes;
iBytes = Sock_Forwards_Recv( iSocket , szData , charsmax( szData ) );
if ( ( ++g_PacketNum == 1 ) && ( ( iDataStart = strfind( szData , "^r^n^r^n" ) ) > -1 ) )
iDataStart += 4;
else
iDataStart = 0;
fwrite_blocks( g_File , szData[ iDataStart ] , ( iBytes - iDataStart ) , BLOCK_BYTE );
}
__________________