-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancel-and-timeout.pwn
More file actions
52 lines (46 loc) · 1.49 KB
/
cancel-and-timeout.pwn
File metadata and controls
52 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// cancel-and-timeout.pwn — per-request timeout and cancellation.
//
// Demonstrates:
// * https_set_timeout_once to give a single call more (or less) time.
// * https_cancel to drop the callback for a request the gamemode no longer needs.
#include <a_samp>
#include <https_samp>
new g_playerRequestIndex[MAX_PLAYERS];
public OnPlayerConnect(playerid)
{
// Give this request 3 seconds total instead of the default 12.
https_set_timeout_once(3000);
https_set_bearer_once("demo-token");
g_playerRequestIndex[playerid] = playerid + 1000;
https(g_playerRequestIndex[playerid], HTTPS_GET,
"https://httpbin.org/delay/1", "", "OnPlayerLookup");
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
// Drop the pending callback if the player left before it arrived.
https_cancel(g_playerRequestIndex[playerid]);
return 1;
}
forward OnPlayerLookup(index, response[], status, error);
public OnPlayerLookup(index, response[], status, error)
{
new playerid = index - 1000;
if (!IsPlayerConnected(playerid))
{
// Defensive guard: cancel may race with delivery in unusual cases.
return 1;
}
if (error == HTTPS_ERROR_TIMEOUT)
{
printf("[example] lookup for %d timed out", playerid);
return 1;
}
if (error != HTTPS_ERROR_NONE)
{
printf("[example] lookup for %d failed: %d", playerid, error);
return 1;
}
printf("[example] lookup for %d ok: status=%d", playerid, status);
return 1;
}