Raised This Month: $7 Target: $400
 1% 

Steamworks > HTTP Requets > Non-Threaded requests


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 05-21-2018 , 06:33   Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #1

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
__________________
Neuro Toxin is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 06-02-2018 , 14:06   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #2

Thanks for this!!
ThatKidWhoGames is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 06-02-2018 , 20:01   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #3

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.
KyleS is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 06-03-2018 , 00:52   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #4

Quote:
Originally Posted by KyleS View Post
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.
__________________

Last edited by Neuro Toxin; 06-03-2018 at 00:54.
Neuro Toxin is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 06-03-2018 , 12:29   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #5

Quote:
Originally Posted by Neuro Toxin View Post
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.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 06-03-2018 at 23:02.
nosoop is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 06-03-2018 , 14:34   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #6

Quote:
Originally Posted by Neuro Toxin View Post
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 View Post
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.
KyleS is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 06-04-2018 , 03:54   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #7

Quote:
Originally Posted by Neuro Toxin View Post
Even Sourcemod does this with the SQL API.
Pretty sure even Sourcemod is trying to phase out non-threaded queries in new syntax.
hmmmmm is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 06-09-2018 , 14:44   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #8

Just out of curiosity, why would you want to make a HTTP request in no threaded way?
Mathias. is offline
SM9
Veteran Member
Join Date: Sep 2013
Location: United Kingdom
Old 06-14-2018 , 19:42   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #9

Quote:
Originally Posted by Mathias The Vegan View Post
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.

Last edited by SM9; 06-14-2018 at 19:43.
SM9 is offline
gevHard
New Member
Join Date: Feb 2020
Old 08-07-2020 , 11:52   Re: Steamworks > HTTP Requets > Non-Threaded requests
Reply With Quote #10

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
gevHard 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 03:55.


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