-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseDispatcher.cs
66 lines (57 loc) · 1.79 KB
/
ResponseDispatcher.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using UnityHttpRequests;
public class RequestDispatcher : MonoBehaviour
{
private static readonly Dictionary<string, string> requestHeaders = new Dictionary<string, string>{
{ "User-Agent", "FooAgent" },
{ "Accept-Encoding", "gzip" },
{ "Accept", "application/json" },
{ "Content-Type", "application/json" },
};
private HttpSession session = new HttpSession();
private byte[] body = new byte[4096];
private Dictionary<int, HttpSession.RequestCompleteDelegate> inFlight = new Dictionary<int, HttpSession.RequestCompleteDelegate>(16);
// Note: For this example to be allocation free, the caller of Get() or Post() needs to
// cache their HttpSession.RequestCompleteDelegate in a member variable so they are not
// creating a new delegate instance for every call.
public void Get(string url, HttpSession.RequestCompleteDelegate onResponse)
{
int rid = session.Get(url, requestHeaders);
inFlight[rid] = onResponse;
}
// See the comment on the Get() method.
public void Post(string url, byte[] requestBody, int requestBodyLength, HttpSession.RequestCompleteDelegate onResponse)
{
int rid = session.Post(url, body, bodyLen, requestHeaders);
inFlight[rid] = onResponse;
}
private void OnEnable()
{
session.RequestComplete += OnResponse;
}
private void OnDisable()
{
session.RequestComplete -= OnResponse;
}
private void Update()
{
session.Update();
}
private void OnDestroy()
{
session.Dispose();
session = null;
}
private void OnResponse(ref Response res)
{
// Lookup the response handler for this request id
HttpSession.RequestCompleteDelegate onResponse = null;
if (inFlight.TryGet(res.RequestId, onResponse))
{
// Deliver the response
onResponse?.Invoke(ref res);
inFlight.Remove(res.RequestId);
}
}
}