Raised This Month: $51 Target: $400
 12% 

[ANY] Async - Efficient event based HTTPS client. HTTP2 and Compression supported


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
bottiger
AlliedModders Donor
Join Date: Dec 2010
Old 03-29-2014 , 22:29   [ANY] Async - Efficient event based HTTPS client. HTTP2 and Compression supported
Reply With Quote #1

Async

An HTTP(S) client focused on ease of use, performance, and stability.

We moved to Github.

Source Code: https://github.com/bottiger1/sourcemod-async
Include: https://github.com/bottiger1/sourcem...pawn/async.inc
Binary: https://github.com/bottiger1/sourcemod-async/releases

Newest Update

November 13, 2019

This is a major update. Everything has been moved to Github. Libraries have all been updated and HTTP2 and Brotli compression is now supported. You can also have the extension gzip post data outside the game thread.

PHP Code:
CurlHandle h Async_CurlNew();
// tell curl to request compressed data. the data you get will be automatically decompressed
Async_CurlSetString(hCURLOPT_ACCEPT_ENCODING"");
// to compress post data. 

// Your webserver or website must manually decompress this data. Most webservers do not automatically do this.
// compression is done with zlib
Async_CurlPostRawCompress(hurldatastrlen(data), OnRequestComplete); 
PHP Code:
// to decompress data from Async_CurlPostRawCompress in php
$post file_get_contents('php://input');
if(
$_SERVER['HTTP_CONTENT_ENCODING'] == 'gzip')
{
    
$post zlib_decode($post128000);

Why another HTTP client?

Unlike the other CURL based HTTP clients here that spawn a thread for each request, this extension spawns only 1 thread and handles all requests through an event loop.

One may think 1 thread is worse than 8 threads, but http requests are mostly limited by network speed and not cpu. By using the same thread for the requests, it cuts down on context switching overhead. This is why many benchmarks show that single threaded network loops are often faster than ones that just spawn 1 thread per request. It is also safer. If you spawn 100 threads, it will take a lot of memory.

Example

Code:
#include <async>

public OnPluginStart() {
    new CurlHandle:h = Async_CurlNew(123);
    Async_CurlGet(h, "http://www.sourcemod.net", OnRequestDone);
}

public OnRequestDone(CurlHandle:request, curlcode, httpcode, size, any:userdata) {
    decl String:buffer[size+1];
    // see http://curl.haxx.se/libcurl/c/libcurl-errors.html for curlcode numbers
    // see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes for http codes
    if(curlcode == 0 && httpcode == 200) {
        Async_CurlGetData(request, buffer, size+1);
        PrintToServer("%s", buffer);
    }
    Async_Close(request);
}
Curl errors codes are located here http://curl.haxx.se/libcurl/c/libcurl-errors.html

Usage Notes
  • It is up to you to use options and set buffer sizes correctly (ie Using a number for a string option or setting buffer sizes larger than the actual size). I don't have time to idiot-proof everything. Not doing so may result in crashes.
  • Function callback options are not supported.
  • If you make an array a bit larger than 16834 (default sourcemod stack size). Sourcemod will silently fail. This is a Sourcemod problem, not a problem with the extension.
  • This extension does not use the Sourcemod handle system which means it does not count towards the 16k handle limit. But you must close handles yourself with Async_Close() and NOT with CloseHandle().
  • However the handles will be closed for you if the plugin is unloaded.
  • Sending a post larger than 1000 bytes to some webservers will cause the request to not post any data and time out. To fix this, you must add an empty Expect: header. Async_CurlAddHeader(h, "Expect: "); This is because curl sends an Expect command and the webserver just doesn't reply.
  • Windows will not be supported but the code is written to be platform-neutral, so there should be Windows support as long as someone builds a copy.

Why another HTTP client?

As I am writing this there are currently 3 options. There are problems with each one as described here.

https://forums.alliedmods.net/showthread.php?t=232396
  • Curl
    People report that it starts failing randomly. It seems to support every single CURL option and is a nightmare to maintain. Does something crazy like using select in each request thread.
  • Sockets
    You need to implement a lot of logic just for http. I've seen a few people here make the assumption that the entire response will return in one callback.
  • Steamtools/Steamworks
    The amount of reverse engineering needed to make this works scares me and it should scare you too. It also doesn't support years old features like ECDSA which means Valve probably doesn't care about maintaining the HTTP client.

How to Compile

Compiling this extension is really annoying. I have tried to write notes on how to do this in readme.txt. My releases are compiled in Ubuntu 18.04 x64.

Changelog
Code:
11/13/2019

2.0

Moved to Github. https://github.com/bottiger1/sourcem...commits/master

7/9/2015

1.6
  • Fixed Async_CurlGetData so it will only add a null byte to the end if the sourcepawn buffer is larger than data received.
5/13/2015 1.5
  • Removed boost and added back the C++0x for compiling. Libstdc++ is now statically compiled into the binary to avoid any problems with the game's libstdc++ being a different version or missing. This should not cause any problems because the extension does not use dlopen or throws any exceptions that I know of.
4/23/2015 1.4
  • You can now close requests that are being processed. They will close without firing the callback when they finish or timeout.
1.3
  • Removed unused code
1.2
  • Added debug symbols to the build.
4/22/2015 1.2
  • Improved Stability
1.1
  • Added Async_CurlGetDataString. This is the same as Async_CurlGetData except it will put a null byte at the end of your buffer for you.
  • Boost is now required instead of worrying if the c++ library your game uses is new enough.
  • Library versions: libcurl/7.42.0 OpenSSL/1.0.1f zlib/1.2.8 c-ares/1.10.0 libidn/1.28
3/29/2014 1.0
  • Initial Release
__________________

Last edited by bottiger; 12-18-2019 at 00:11. Reason: updated example
bottiger is offline
 



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 06:25.


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