AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Steamworks > HTTP Requets > Non-Threaded requests (https://forums.alliedmods.net/showthread.php?t=307690)

Neuro Toxin 05-21-2018 06:33

Steamworks > HTTP Requets > Non-Threaded requests
 
I've been working on a CPU friendly way to consistently complete a threaded HTTP request in the Steamworks extension.

Here is my final snippet.

Code:

#include <steamworks>

/*
        The magic is here as it enables us to sleep in a CPU friendly way for a
        specific amount of time as per the seconds parameter.
*/
stock void Sleep(float seconds)
{
        ServerCommand("snd_debug_sleep %f", seconds);
}

public bool Example_SendHTTPRequest_NonThreaded()
{
        // Create the request as per normal
        char[] url = "https://google.com/";
        Handle request = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, url);
        if (request == null)
                return false;
       
        // Send the request (note: We didn't assign a callback!)
        SteamWorks_SendHTTPRequest(request);
       
        // We need to set a timeout for invalid / failed requests
        float timeouttime = GetEngineTime() + 5.0;
       
        // Loop until we have a response
        int responsesize = 0;
        while (GetEngineTime() < timeouttime)
        {
                // Use the sleep function
                Sleep(0.1);
               
                // Check the response size
                SteamWorks_GetHTTPResponseBodySize(request, responsesize);
                if (responsesize > 0)
                {
                        OnInfoReceived(request, false, true, k_EHTTPStatusCode200OK)
                        return true;
                }
        }
       
        // The request has timed out
        OnInfoReceived(request, true, false, k_EHTTPStatusCode5xxUnknown);
        return false;
}

public int OnInfoReceived(Handle request, bool failure, bool requestSuccessful, EHTTPStatusCode statusCode)
{
        if (!failure && requestSuccessful && statusCode == k_EHTTPStatusCode200OK)
                SteamWorks_GetHTTPResponseBodyCallback(request, APIWebResponse);
       
        delete request;
        return 0;
}

public APIWebResponse(char[] response)
{
        // Do stuff
}

If you know a better way using Steamworks, please say something :P

ThatKidWhoGames 06-02-2018 14:06

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Thanks for this!!

KyleS 06-02-2018 20:01

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Code:
snd_debug_sleep
isn't present on all games; it might just even be CS:GO.

Out of principle I'd say the paradigm should be adjusted so your code can handle callbacks as it's all networked anyways and latency is to be expected. Blocking like this on a single threaded platform is almost certainly bad news and doesn't reflect the normalcy of the era.

Neuro Toxin 06-03-2018 00:52

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Quote:

Originally Posted by KyleS (Post 2594977)
Code:
snd_debug_sleep
isn't present on all games; it might just even be CS:GO.

Out of principle I'd say the paradigm should be adjusted so your code can handle callbacks as it's all networked anyways and latency is to be expected. Blocking like this on a single threaded platform is almost certainly bad news and doesn't reflect the normalcy of the era.

When you look at socket coding in general the paradigm is to give the choice by having both threaded and non-threaded operations. Even Sourcemod does this with the SQL API. To force people to use threaded operations because you beleive there is no legitimacy for non-threaded operations is naive imo.

nosoop 06-03-2018 12:29

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Quote:

Originally Posted by Neuro Toxin (Post 2595007)
When you look at socket coding in general the paradigm is to give the choice by having both threaded and non-threaded operations. Even Sourcemod does this with the SQL API. To force people to use threaded operations because you beleive there is no legitimacy for non-threaded operations is naive imo.

Emphasis on the fact that it's a single threaded platform. Blocking would be fine if there was nothing else going on (e.g., utility scripts or simple applications), but there's expectations that a game server is able to respond in a predictable amount of time.

SteamWorks only has async HTTP/S functions anyways, so you're more than welcome to blame Valve for forcing this on you.

As an aside, snd_debug_sleep isn't present on the Linux version of the CS:GO dedicated server.

KyleS 06-03-2018 14:34

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Quote:

Originally Posted by Neuro Toxin (Post 2595007)
When you look at socket coding in general the paradigm is to give the choice by having both threaded and non-threaded operations.

No, it isn't.

Quote:

Originally Posted by Neuro Toxin (Post 2595007)
Even Sourcemod does this with the SQL API. To force people to use threaded operations because you beleive there is no legitimacy for non-threaded operations is naive imo.

Remember you have at the most 8ms to complete an operation before it's apparent to clients in-game. Using blocking operations like this to foreign servers is going to actively cause harm from an ancient mindset.

hmmmmm 06-04-2018 03:54

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Quote:

Originally Posted by Neuro Toxin (Post 2595007)
Even Sourcemod does this with the SQL API.

Pretty sure even Sourcemod is trying to phase out non-threaded queries in new syntax.

Mathias. 06-09-2018 14:44

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Just out of curiosity, why would you want to make a HTTP request in no threaded way?

SM9 06-14-2018 19:42

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Quote:

Originally Posted by Mathias The Vegan (Post 2596109)
Just out of curiosity, why would you want to make a HTTP request in no threaded way?

So you can get the response within same function, no need for a callback, but its a really bad idea and I would avoid this.

gevHard 08-07-2020 11:52

Re: Steamworks > HTTP Requets > Non-Threaded requests
 
Hi! Installed all but an error still take off my sleep.
'Cannot load matches from a url without the SteamWorks extension running'

Can anyone help me?


Sorry my english


All times are GMT -4. The time now is 09:22.

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