Raised This Month: $32 Target: $400
 8% 

[INC] FTP


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-10-2010 , 22:10   [INC] FTP
Reply With Quote #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
  • 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.
Attached Files
File Type: inc ftp.inc (10.6 KB, 877 views)
File Type: sma Get Plugin or Get Source (ftp_test.sma - 1409 views - 2.0 KB)
__________________

Last edited by Bugsy; 08-10-2011 at 20:58. Reason: New version
Bugsy is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-10-2010 , 22:29   Re: [INC] FTP
Reply With Quote #2

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-10-2010 , 22:39   Re: [INC] FTP
Reply With Quote #3

Quote:
Originally Posted by Exolent[jNr] View Post
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.
__________________

Last edited by Bugsy; 11-10-2010 at 23:06.
Bugsy is offline
shuttle_wave
Veteran Member
Join Date: Apr 2009
Location: New Zealand
Old 11-10-2010 , 23:47   Re: [INC] FTP
Reply With Quote #4

great job bugsy
shuttle_wave is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 11-11-2010 , 05:43   Re: [INC] FTP
Reply With Quote #5

I just love tutroials like these !
GJ as usual
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
meTaLiCroSS
Gaze Upon My Hat
Join Date: Feb 2009
Location: Viņa del Mar, Chile
Old 11-11-2010 , 16:43   Re: [INC] FTP
Reply With Quote #6

F*CK DAMN THIS ROCKS I LOVE YOU MAN.
__________________
Quote:
Originally Posted by joropito View Post
You're right Metalicross
meTaLiCroSS is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 11-11-2010 , 17:02   Re: [INC] FTP
Reply With Quote #7

This is amazing. Gonna be using this often
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
AfteR.
Veteran Member
Join Date: Dec 2008
Location: λ
Old 11-13-2010 , 22:18   Re: [INC] FTP
Reply With Quote #8

Nice one! Good job
AfteR. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-24-2010 , 21:18   Re: [INC] FTP
Reply With Quote #9

Posted a new (experimental) release above
__________________
Bugsy is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-28-2010 , 23:24   Re: [INC] FTP
Reply With Quote #10

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 10:38.


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