AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
|

11-10-2010
, 23:10
[INC] FTP
|
#1
|
 .: 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
.: 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_Open( FTP_Server , FTP_Port , FTP_User , FTP_Pass , "FwdFuncOpen" ); }
public Close() { FTP_Close(); }
public Send() { if ( FTP_Ready() ) { FTP_SendFile( FTP_LocalFile , FTP_RemoteFile , "FwdFuncTransfer" ); g_iStartTime = get_systime(); } }
public Get() { if ( FTP_Ready() ) { FTP_GetFile( FTP_LocalFile , FTP_RemoteFile , "FwdFuncTransfer" ); g_iStartTime = get_systime(); } }
public List() { if ( FTP_Ready() ) { FTP_GetList( FTP_ListFile , FTP_RemoteDir , "FwdFuncList" ); } }
//Forward functions public FwdFuncOpen( bool:bLoggedIn ) { server_print( "Login was %ssuccessful!" , bLoggedIn ? "" : "un" ); }
public FwdFuncTransfer( szFile[] , iBytesComplete , iTotalBytes ) { server_print( "[%.1f%%] [%s] [ %d of %d bytes ][ %dkB/s ]" , ( floatdiv( float( iBytesComplete ) , float( iTotalBytes ) ) * 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 FwdFuncList( szFile[] , 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.
__________________
Last edited by Bugsy; 08-10-2011 at 21:58.
Reason: New version
|
|