AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [INC] Sockets Forwards (https://forums.alliedmods.net/showthread.php?t=169315)

Bugsy 10-10-2011 21:23

[INC] Sockets Forwards
 
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 );



MyPc 10-11-2011 04:45

Re: [INC/MODULE] Sockets Forwards
 
Good Job =D

Arkshine 10-11-2011 18:21

Re: [INC/MODULE] Sockets Forwards
 
Sounds sexy but too bad it's windows-only. :/

Bugsy 10-11-2011 18:24

Re: [INC/MODULE] Sockets Forwards
 
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.

Arkshine 10-11-2011 18:31

Re: [INC/MODULE] Sockets Forwards
 
Oh. It was confusing. Will compile for you.

Bugsy 10-11-2011 21:05

Re: [INC/MODULE] Sockets Forwards
 
Everything is back up and running. As always, comments\recommendations\bug-reports are welcome.

Arkshine 10-12-2011 04:40

Re: [INC/MODULE] Sockets Forwards
 
You should clean up your ZIP, you have included many useless directories.

jim_yang 10-12-2011 04:46

Re: [INC/MODULE] Sockets Forwards
 
508K ? debug version ?
metamod is only 1.18M

Arkshine 10-12-2011 05:18

Re: [INC/MODULE] Sockets Forwards
 
1 Attachment(s)
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.

Bugsy 10-12-2011 07:03

Re: [INC/MODULE] Sockets Forwards
 
Re-uploaded fixed source zip and release version of dll. Thanks Arkshine.


All times are GMT -4. The time now is 12:24.

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