Raised This Month: $12 Target: $400
 3% 

[INC] Sockets Forwards


Post New Thread Reply   
 
Thread Tools Display Modes
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
MyPc
Senior Member
Join Date: Sep 2011
Old 10-11-2011 , 04:45   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #2

Good Job =D
MyPc is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-11-2011 , 18:21   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #3

Sounds sexy but too bad it's windows-only. :/
__________________
Arkshine is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-11-2011 , 18:24   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #4

sockets forwards is windows & unix, the other module I am working on is windows only. The main difference is this inc/module combo uses a thinking entity to monitor changes to trigger forwards and the windows-only module gets notified by the OS of changes so no looping/set_task/thinking-ent. The overall functionality is the same.
__________________

Last edited by Bugsy; 10-11-2011 at 18:27.
Bugsy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-11-2011 , 18:31   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #5

Oh. It was confusing. Will compile for you.
__________________
Arkshine is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-11-2011 , 21:05   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #6

Everything is back up and running. As always, comments\recommendations\bug-reports are welcome.
__________________
Bugsy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-12-2011 , 04:40   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #7

You should clean up your ZIP, you have included many useless directories.
__________________
Arkshine is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 10-12-2011 , 04:46   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #8

508K ? debug version ?
metamod is only 1.18M
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-12-2011 , 05:18   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #9

Yes, probably the debug version, I get 56 ko with msvc2010 in release mode.

Jim, if you have time, can you try to compile for linux ? I have some difficulties to fix the code to be compatible linux.


EDIT : Oh. Fixed all. Though it needs testing.
Attached Files
File Type: so sockets2_amxx_i386.so (13.4 KB, 824 views)
__________________

Last edited by Arkshine; 10-12-2011 at 05:39.
Arkshine is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-12-2011 , 07:03   Re: [INC/MODULE] Sockets Forwards
Reply With Quote #10

Re-uploaded fixed source zip and release version of dll. Thanks Arkshine.
__________________
Bugsy is offline
Reply


Thread Tools
Display Modes

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 14:00.


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