AlliedModders

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

Bugsy 09-20-2011 20:55

[INC] HTTP
 
2 Attachment(s)
http://icons.iconarchive.com/icons/l.../HTTP-icon.png
.: Description

This include provides the ability to download files via the HTTP protocol. Currently, the max simultaneous downloads is limited to 10. The title of the file may sound misleading since this include only has the ability to download files but I wanted to keep the title simple.

.: Commands
  • HTTP_DownloadFile - Download a file from a HTTP server.
  • HTTP_AbortTransfer - Abort an active file transfer.

.: Usage
Commands
  • HTTP_DownloadFile( const szRemoteFile[] , const szLocalFile[] )
    • szRemoteFile - The file to download, ie. "http://www.hello.net/mystuff/cars/ferrari.jpg"
    • szLocalFile - Local file to save what was retrieved (default directory is mod-directory) ie. "ferarri.jpg"
      • Return value: The function returns a unique integer value (download id) if successful or 0 on failure\all slots are full.

  • HTTP_AbortTransfer( iDownloadID , bool:bDeleteLocalFile=true )
    • iDownloadID - The unique download-id for the transfer. Returned by HTTP_DownloadFile and passed in download progress forward.
    • bDeleteLocalFile - Whether or not the local file is deleted after the download is aborted. Default is true.
      • Return value: 1 on success, 0 on failure.
Download Progress Forward
  • HTTP_Download( const szFile[] , iDownloadID , iBytesReceived , iFileSize , bool:bTransferComplete )
    • szFile - File as it is being saved on the local computer (local filename).
    • iDownloadID - Download ID; this is the unique id\value that is returned from the HTTP_Download function.
    • iBytesReceived - Bytes that have been downloaded so far.
    • iFileSize - The size of the file being downloaded.
    • bTransferComplete - Bool value for whether or not the download is complete.

.: Requirements
  • engine
  • sockets

.: Comments

I realize a similar plugin\native already exists [HTTP Downloader] but after reviewing the source I noticed it is not only inefficient but also guaranteed not to work on larger files. Many people in the thread have reported that it does not work so this will be a replacement for that. As always, comments and suggestions are welcome.

.: Example Plugin

PHP Code:

#include <amxmodx>
#include <http>

new const Version[] = "0.4";

public 
plugin_init()
{
    
register_plugin"HTTP Test" Version "bugsy" );
    
    
register_concmd"test" "DownloadFile" );
}

public 
DownloadFile()
{
    
HTTP_DownloadFile"www.colorado.edu/conflict/peace/download/peace_treatment.zip" "testzip.zip" );
}

public 
HTTP_Download( const szFile[] , iDownloadID iBytesRecv iFileSize bool:TransferComplete )
{
    if ( 
TransferComplete )
    {
        
server_print"File=[%s] DownloadID=%d BytesTransferred=%d iSize=%d" szFile iDownloadID iBytesRecv iFileSize );
        
server_print"%s download complete!" szFile );
    }
    else
    {
        
server_print"File=[%s] DownloadID=%d BytesTransferred=%d iSize=%d" szFile iDownloadID iBytesRecv iFileSize );
    }


.: Changelog
  • v0.4b
    • Fixed issue where when a download was completing, the file was not getting closed.
  • v0.4
    • Changed socket_change() timeout from default of 100000 to 0 to avoid blocking.
    • Added file size to download progress forward.
    • Added FTP_AbortTransfer() function to stop an active file transfer.
    • Fixed bug where if an error occurred opening\creating the local file the download would still proceed. Reported by dummy82.
  • v0.3
    • Changed CreateMultiForward() to CreateOneForward() to prevent the forward from being called in other plugins.
    • Misc: Added entity-id check in think forward for safety. Removed trim-space flag on strtok()-per Emp, this is buggy, used trim() instead. Instead of calling set_fail_state() on an error, changed to log_amx().
  • v0.2
    • Changed download-id from a random number to a sequential number to eliminate the chance of duplicates.
    • Fixed index out of bounds error when a download was attempted when all slots were already full.
    • When all downloads are complete the forward and entity are destroyed. They are re-created when\if any subsequent downloads are called.
    • The server-name and file are now parsed using strtok() instead of manually parsing with copy().
    • EV_FL_nextthink is only set in the download function if we are creating the entity, not if a download/entity already exists.
  • v0.1
    • Initial release

nikhilgupta345 09-20-2011 21:08

Re: [INC] HTTP
 
Nice job!

Quick question:
How often does the forward get called? Just twice? Once for download start and one for download end? Or is it constantly called until the download is finished?

EDIT: Nvrm, I see it's 0.1 seconds.

Bugsy 09-20-2011 21:10

Re: [INC] HTTP
 
Constantly called from download start to download end.

nikhilgupta345 09-20-2011 21:12

Re: [INC] HTTP
 
K. Well, good job again. Will be useful to a lot of people.

deadman909 09-21-2011 03:19

Re: [INC] HTTP
 
How exactly does this work. Do players have to type HTTP_DownloadFile muttley.gif in console? Because I type that in console but noting happens.

Arkshine 09-21-2011 04:22

Re: [INC] HTTP
 
That's not a client command but a function to be used in a plugin.

Fedde 09-21-2011 06:06

Re: [INC] HTTP
 
:orgasm:

Thanks you Bugsy ^^

[Edit]
How can I download from two webs and put the data into 1 file?
I think I must make two diferents files,read one of the files get the data,read it,and write the data to the other file.

Sorry for my bad english.

Bugsy 09-21-2011 07:53

Re: [INC] HTTP
 
Quote:

Originally Posted by Fedde (Post 1559931)
:orgasm:

Thanks you Bugsy ^^

[Edit]
How can I download from two webs and put the data into 1 file?
I think I must make two diferents files,read one of the files get the data,read it,and write the data to the other file.

Sorry for my bad english.

This can be done but is there a reason why you want to do this other than faster downloading of the same file?

Fedde 09-21-2011 08:15

Re: [INC] HTTP
 
Quote:

Originally Posted by Bugsy (Post 1559991)
This can be done but is there a reason why you want to do this other than faster downloading of the same file?

Yes,I want the records from cosy-climbing and xtreme-jumps to use with a plugin about world records.

deadman909 09-21-2011 09:55

Re: [INC] HTTP
 
I am still sort of confused as to what this plugin would help one do. I put it in but I checked my files and it didnt download the image when I connected to server.


All times are GMT -4. The time now is 17:16.

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