AlliedModders

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

Bugsy 11-10-2010 22:10

[INC] FTP
 
4 Attachment(s)
http://upload.wikimedia.org/wikipedi...te-ftp.svg.png
.: Description

This include provides the ability to upload files, download files, and retrieve a directory-list of files from a FTP server. Using my ISP provided FTP file server (Comcast, cable) and a 6.42mb file, I was able to download in 18 seconds and upload in 35 seconds. Currently, only one transaction can take place at a time. There is currently no error trapping for remote-side errors, make sure directories are correct/accessible and server-name\port\login\password are accurate, etc. The previous version of this include used a new connection for each file transaction. The new version uses one connection for multiple transactions. There is no 'keep-alive' functionality in place so do all transactions shortly after opening a connection.

.: Commands
  • FTP_Open - Establish connection and login to a FTP server.
  • FTP_Close - Log-out and close connection to FTP server.
  • FTP_Ready - Returns FTP status; true if ready for a transaction.
  • FTP_SendFile - Transfer a file to FTP server.
  • FTP_GetFile - Retrieve a file from FTP server.
  • FTP_GetList - Retrieve directory contents from FTP server.

.: Command Usage
  • FTP_Open( szServer[] , iPort , szUser[] , szPassword[] , szForward[] )
    • szServer - FTP server address.
    • iPort - FTP server port.
    • szUser - Username to login with.
    • szPassword - Password for login.
    • szForward - Forward function for monitoring status of login attempt. The forward takes 1 param which is a bool to indicate whether or not the login attempt was successful.
      • Forward( bool:bLoginSuccessful )

  • FTP_Close()

  • FTP_Ready()

  • FTP_SendFile( szLocalFile[] , szRemoteFile[] , szForward[] )
    • szLocalFile - Local file to be transferred to FTP server.
    • szRemoteFile - Remote file for storing transferred file.
    • szForward - Forward function for monitoring status of file transfer. The forward takes 3 params; first is file name being transferred, second is bytes transferred, third is total bytes.
      • Forward( szFile[] , iBytesTransferred , iTotalBytes )

  • FTP_GetFile( szLocalFile[] , szRemoteFile[] , szForward[] )
    • szLocalFile - Local file to save file that was retrieved from FTP server.
    • szRemoteFile - Remote file to retrieve from FTP server.
    • szForward - Forward function for monitoring status of file transfer. The forward takes 3 params; first is file name being transferred, second is bytes transferred, third is total bytes.
      • Forward( szFile[] , iBytesTransferred , iTotalBytes )

  • FTP_GetList( szLocalFile[] , szRemoteDirectory[] , szForward[] )
    • szLocalFile - Local file to save directory list.
    • szRemoteFile - Remote directory to retrieve a list of files from.
    • szForward - Forward function for monitoring status of list retrieval. The forward takes 2 params; first is file name of the local file list, second is bytes retrieved.
      • Forward( szFile[] , iBytesRetrieved )

.: Requirements
  • sockets
  • engine

.: Example plugin
The test server provided will work for all commands except ftp_send.
PHP Code:

/*    
    FTP Test Plugin
         v0.1
       by bugsy
    
    http://forums.alliedmods.net/showthread.php?t=142850
*/

#include <amxmodx>
#include <ftp>

new FTP_Server[] = "ftp.secureftp-test.com";
new 
FTP_Port 21;
new 
FTP_User[] = "test";
new 
FTP_Pass[] = "test";
new 
FTP_LocalFile[] = "hamlet.zip"
new FTP_RemoteFile[] = "hamlet.zip";
new 
FTP_ListFile[] = "DirList.txt";
new 
FTP_RemoteDir[] = "//";

new 
g_iStartTime;

public 
plugin_init() 
{
    
register_plugin"FTP Test" "0.1" "bugsy" );
    
    
register_concmd"ftp_open" "Open" );
    
register_concmd"ftp_close" "Close" );
    
register_concmd"ftp_send" "Send" );
    
register_concmd"ftp_get" "Get" );
    
register_concmd"ftp_list" "List" );
}

//Commands
public Open()
{
    
FTP_OpenFTP_Server FTP_Port FTP_User FTP_Pass "FwdFuncOpen" );
}

public 
Close()
{
    
FTP_Close();
}

public 
Send()
{
    if ( 
FTP_Ready() )
    {
        
FTP_SendFileFTP_LocalFile FTP_RemoteFile "FwdFuncTransfer" );    
        
g_iStartTime get_systime();
    }
}

public 
Get()
{
    if ( 
FTP_Ready() )
    {
        
FTP_GetFileFTP_LocalFile FTP_RemoteFile "FwdFuncTransfer" );    
        
g_iStartTime get_systime();
    }
}

public List()
{
    if ( 
FTP_Ready() )
    {
        
FTP_GetListFTP_ListFile FTP_RemoteDir "FwdFuncList" );
    }
}

//Forward functions
public FwdFuncOpenbool:bLoggedIn )
{
    
server_print"Login was %ssuccessful!" bLoggedIn "" "un" );
}

public 
FwdFuncTransferszFile[] , iBytesComplete iTotalBytes )
{
    
server_print"[%.1f%%] [%s] [ %d of %d bytes ][ %dkB/s ]" , ( floatdivfloatiBytesComplete ) , floatiTotalBytes ) ) * 100.0 ) , 
                                    
szFile 
                                    
iBytesComplete ,
                                    
iTotalBytes 
                                    ( ( 
iBytesComplete ) / 1000 ) / ( get_systime() - g_iStartTime ) );
                                    
    
    if ( 
iBytesComplete == iTotalBytes )
        
server_print"File transfer completed in %d seconds!" get_systime() - g_iStartTime );
}

public 
FwdFuncListszFile[] , iBytesComplete )
{
    
server_print"[%s] [ %d bytes ]" szFile iBytesComplete );


.: ChangeLog
  • 0.3
    • Modified functionality so that a connection remains established for multiple transactions. You must first call FTP_Open() before making making any transactions and then call FTP_Close() when complete.
    • Fixed bug where FTP server responses were not being handled properly when the server would send multiple responses in the same packet. This bug caused only the first response to be processed leaving all others ignored. My previous attempt to fix this bug did not work properly and I haven't had any problems with the new method.
    • Adjusted interval for checking sockets for new data. Previously the same interval was used at all times whether the FTP server was waiting for commands or data was being transferred. Now, a longer interval is used for processing commands and when a data transfer is initiated, the interval is increased to make the transfer faster. This will make the include\plugin a little more efficient by reducing the number of times the sockets are being checked for data when sitting idle.
  • 0.2
    • Added FTP_GetFileList command to retrieve directory contents from a FTP server. This command creates a local text file with the contents of the remove directory (files and directories).
    • Added FTP_IsReady function to determine if a FTP command is able to be processed. If FTP_IsReady returns false and a command is called anyway, the plugin will error so be sure to always check this prior to calling FTP_FileTransfer or FTP_GetFileList.
    • Fixed bug that affected only some FTP servers that return multiple command responses in the same packet. The command reader in this include reads the first 3 characters from the server response to determine the type of response. When two responses are sent in the same packet, the second command was never getting processed. This only happened while testing using very small files or when retrieving a directory listing.
    • Removed set_task(), both command and data handling are now done with a thinking entity.

Exolent[jNr] 11-10-2010 22:29

Re: [INC] FTP
 
Nice work.

Suggestion:
You could create a variable that stores whether or not a connection is already made, then return 0 if already connected, or 1 if it was allowed.

Bugsy 11-10-2010 22:39

Re: [INC] FTP
 
Quote:

Originally Posted by Exolent[jNr] (Post 1346967)
Nice work.

Suggestion:
You could create a variable that stores whether or not a connection is already made, then return 0 if already connected, or 1 if it was allowed.

I definitely plan on adding a 'status' variable along with other things, I wanted to get this out there to get some feedback before coding further. I was able to drastically improve transfer time since the original post; from 90 seconds for upload\download to 35\18 seconds respectively.

shuttle_wave 11-10-2010 23:47

Re: [INC] FTP
 
great job bugsy

abdul-rehman 11-11-2010 05:43

Re: [INC] FTP
 
I just love tutroials like these !
GJ as usual :D

meTaLiCroSS 11-11-2010 16:43

Re: [INC] FTP
 
F*CK DAMN THIS ROCKS I LOVE YOU MAN.

nikhilgupta345 11-11-2010 17:02

Re: [INC] FTP
 
This is amazing. Gonna be using this often :)

AfteR. 11-13-2010 22:18

Re: [INC] FTP
 
Nice one! Good job :D

Bugsy 11-24-2010 21:18

Re: [INC] FTP
 
Posted a new (experimental) release above

Exolent[jNr] 11-28-2010 23:24

Re: [INC] FTP
 
Tested experimental release and I never got any response back for about 5 minutes of waiting (storing a file).
The login information was correct as well as the files and their directories.
I tried the same test with the original release and it worked flawlessly.


All times are GMT -4. The time now is 05:30.

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