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
__________________