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

Steamworks > HTTP Requets > Non-Threaded requests


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



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 08:45.


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