AlliedModders

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

Kia 09-10-2013 00:33

AW: Re: [INC] HTTP2
 
Quote:

Originally Posted by Black Rose (Post 2031638)
Goddamnit.

+1

I think I can remove "Add Plugins to xRepo" from my list.

Black Rose 09-14-2013 15:01

Re: [INC] HTTP2
 
I was experimenting a little with new functions for HTTP2.
This enables you to download and install modules that is required for your plugin automatically. This only works for natives, if the inc has stocks which you are using it will not work since they are written into the plugin when you compile it.

I'm not sure I'll add this feature to HTTP2. The whole point is to exclude the use of includes and pre-installation of modules.

The point of defining the used natives if there's no include is that you could compile it with the AMXX webcompiler and it will still run. So if you're using this you'll have to copy/paste the neccessary native lines from the inc file. You only need the ones you use in your plugins, you can skip the rest.

library and name are not just made up or guesses. They are defined in the moduleconfig.h as (for example):
#define MODULE_NAME "Test"
#define MODULE_LIBRARY "test"

The only problem I've faced is that you can not restart the server using server_cmd("restart") at the point of plugin_natives(). So you'll have to wait for a mapchange/restart/crash.

What you should know: This does not work with zipped files because AMXX is unable to unpack them without the use of a module. If you want to use this you'll just have to whine to the module author to attach the binary separately.

Method #1, automatic library loading (if the module supports it):
Code:
#include <amxmodx> #include <http2> #tryinclude <test> #if !defined _test_included native testfunc(num); #endif #if AMXX_VERSION_NUM >= 175     #pragma reqlib test     #if !defined AMXMODX_NOAUTOLOAD         #pragma loadlib test     #endif #else     #pragma library test #endif public plugin_init() {     register_plugin("Test Plugin 7","","");     server_print("Testfunc: %d", testfunc(9)); } public plugin_natives() {     RequireModule("Test", "test", "http://DivinityX.eu/test/test_amxx.dll"); }

Method #2, library not defined in moduleconfig.h. We'll have to force loading.:
Code:
#include <amxmodx> #include <http2> #tryinclude <test> #if !defined _test_included native testfunc(num); #endif public plugin_init() {     register_plugin("Test Plugin 7","","");     server_print("Testfunc: %d", testfunc(9)); } public plugin_natives() {     RequireModule("Test", "test", "http://DivinityX.eu/test/test_amxx.dll", "", true); }

The function (_HTTP2_ParseHeader() and _HTTP2_ParseURL() can be found in HTTP2 inc):
Code:
/* name[] is the name of the loaded module, to prevent it from downloading again. filename[] is the filename without "_amxx_i386.so" or "_amxx.dll". windows_binary[] is the URL to the .dll version, while linux_binary[] is the same for linux. WriteModulesToIni tells the function to write the downloaded module to modules.ini. If the module doesn't support automatic loading, this is required. */ stock RequireModule(name[], filename[], windows_binary[] = "", linux_binary[] = "", bool:WriteToModulesIni = false, Port = 80) {         new module_name[32], module_author[32], module_version[10], module_status;     new modulesnum = get_modulesnum();         for ( new i = 0 ; i < modulesnum ; i++ ) {         get_module(i, module_name, charsmax(module_name), module_author, charsmax(module_author), module_version, charsmax(module_version), module_status);                 if ( equali(module_name, name) )             return 0;     }         new linux = is_linux_server();         if ( ( ! linux && ! strlen(windows_binary) ) || ( linux && ! strlen(linux_binary) ) ) {         server_print("[HTTP2] Invalid binary specified for this type of server");         return -1     }         new file_location[192];     new len = get_localinfo("amxx_modulesdir", file_location, charsmax(file_location));     formatex(file_location[len], charsmax(file_location) - len, "/%s%s", filename, linux ? "_amxx_i386.so" : "_amxx.dll");         if ( file_exists(file_location) ) {         server_print("[HTTP2] The file ^"%s^" already exists", file_location);         return -1;     }         new URL2[256];     new hFile, bool:SSL, PathPos = _HTTP2_ParseURL(linux ? linux_binary : windows_binary, URL2, charsmax(URL2), SSL, Port);         if ( ! ( hFile = fopen(file_location, "wb") ) ) {         server_print("[HTTP2] Error creating local file.");         return -1;     }         new ResultNum;     new hSocket = socket_open(URL2, Port, SOCKET_TCP, ResultNum);         if ( ResultNum ) {         switch ( ResultNum ) {             case 1: server_print("[HTTP2] Socket error: Error while creating socket.");             case 2: server_print("[HTTP2] Socket error: Couldn't resolve hostname.");             case 3: server_print("[HTTP2] Socket error: Couldn't connect to given hostname:port.");         }         return -1;     }         if ( PathPos <= 0 )         PathPos = strlen(URL2);             new Request[300];     new RequestLen = formatex(Request, charsmax(Request), "GET /%s HTTP/1.1^r^nHost: %s^r^n^r^n", URL2[PathPos], URL2);         socket_send(hSocket, Request, RequestLen);         new BytesReceived, BufferLen, FileSize, bool:Dummy = true, Status = _HTTP2_STATUS_FIRSTRUN;         while ( Dummy ) {         if ( ! socket_change(hSocket, 1000) )             continue;                 if ( ! ( BufferLen = socket_recv(hSocket, _gHTTP2_DataBuffer, 1024) ) ) {             server_print("[HTTP2] No data received.");             return -1;         }                 if ( Status & _HTTP2_STATUS_FIRSTRUN ) {             Status &= ~_HTTP2_STATUS_FIRSTRUN;                         new _HTTP2_ReturnCodeExtended[32], _HTTP2_Location[128];             new ReturnCode;                         BufferLen -= _HTTP2_ParseHeader(_gHTTP2_DataBuffer, BufferLen, FileSize, Status, ReturnCode, _HTTP2_ReturnCodeExtended, _HTTP2_Location);                         if ( ReturnCode != 200 ) {                 server_print("[HTTP2] Bad return code: %d.", ReturnCode);                 return -1;             }         }                 BytesReceived += fwrite_blocks(hFile, _gHTTP2_DataBuffer, BufferLen, BLOCK_BYTE)                 if ( BytesReceived == FileSize )             break;     }         socket_close(hSocket);     fclose(hFile);         if ( WriteToModulesIni ) {         get_localinfo("amxx_modules", file_location,charsmax(file_location));         hFile = fopen(file_location, "a");         fputs(hFile, "^r^n");         fputs(hFile, filename);         fclose(hFile);     }         return 1; }

Here's a result using method #1:
Run #1:
Code:

L 09/14/2013 - 21:04:34: [AMXX] Plugin "test7.amxx" failed to load: Module/Libra
ry "test" required for plugin.  Check modules.ini.

Run #2:
Code:

Testfunc: 27

As always, use it however you like. Copy it, modify it, create your version based on it. No need for credit.

The source code of the module in the example is useless but can be found here.

LordOfNothing 10-18-2013 04:59

Re: [INC] HTTP2
 
VEryy cool Include BK , but having a question , can help me with this inc at showing an example please :

stock HTTP2_UpdatePlugin(const URL[])

Black Rose 10-18-2013 11:00

Re: [INC] HTTP2
 
Code:
#include <amxmodx> #include <http2> public plugin_init() {     HTTP2_UpdatePlugin("http://www.amxmodx.org/plcompiler_vb.cgi?file_id=126862") // I took one at random from New Plugin Submission }
You can also directly link to an .amxx file, but I would avoid that as it would seem suspicious.

Of course there are problems with this. By linking to an automatically compiled file on the AM forum it cannot include HTTP2.
In this case I would suggest copying the HTTP2 include into your plugin and strip the things you don't need.

Another option is to use a third party webcompiler that compiles the source posted on AMXX.

It doesn't have to be used at plugin_init(), but make sure you only call it once per load at the absolute maximum.
To avoid flooding HTTP servers it really should be downloaded once per day or week.

LordOfNothing 10-18-2013 12:05

Re: [INC] HTTP2
 
Quote:

Originally Posted by Black Rose (Post 2050325)
Code:
#include <amxmodx> #include <http2> public plugin_init() { HTTP2_UpdatePlugin("http://www.amxmodx.org/plcompiler_vb.cgi?file_id=126862") // I took one at random from New Plugin Submission }

You can also directly link to an .amxx file, but I would avoid that as it would seem suspicious.

Of course there are problems with this. By linking to an automatically compiled file on the AM forum it cannot include HTTP2.
In this case I would suggest copying the HTTP2 include into your plugin and strip the things you don't need.

Another option is to use a third party webcompiler that compiles the source posted on AMXX.

It doesn't have to be used at plugin_init(), but make sure you only call it once per load at the absolute maximum.
To avoid flooding HTTP servers it really should be downloaded once per day or week.


Lets say i have this lin : *snip*
And not be a more good method to use in plugin_cfg ????

Black Rose 10-18-2013 12:34

Re: [INC] HTTP2
 
It doesn't matter where you call it. It will still be delayed because of the download process. It takes time to connect to the server and receiving the data. This is the reason it's using a task/think. It won't freeze the server until it's done.
The new binary will be loaded after mapchange either way.

LordOfNothing 10-21-2013 04:10

Re: [INC] HTTP2
 
So i have this subdomanin with a file test.amxx but when click him show descriptate code not auto download will work usie HTTP2_Update function ???

click : *snip*

Black Rose 10-21-2013 11:30

Re: [INC] HTTP2
 
I don't understand. What is "descriptate code"?
The link has to point directly to the binary file or indirectly by writing the data as output to the stream or through a 30X redirection. It will not work if the link is a page with a download link. You can on the other hand download that page first to parse for the download link and enter that instead.

LordOfNothing 10-28-2013 03:13

Re: [INC] HTTP2
 
How to use this Includes to making an radio ?

Black Rose 10-28-2013 12:09

[INC] HTTP2
 
What do you mean?


All times are GMT -4. The time now is 03:25.

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