Request.cs
3.22 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#if OVR_PLATFORM_ASYNC_MESSAGES
using System.Threading.Tasks;
#endif
using UnityEngine;
namespace Oculus.Platform
{
public sealed class Request<T> : Request
{
#if OVR_PLATFORM_ASYNC_MESSAGES
private TaskCompletionSource<Message<T>> tcs_ = null;
#endif
private Message<T>.Callback callback_ = null;
public Request(ulong requestID) : base (requestID) { }
public Request<T> OnComplete(Message<T>.Callback callback)
{
if (callback_ != null)
{
throw new UnityException("Attempted to attach multiple handlers to a Request. This is not allowed.");
}
#if OVR_PLATFORM_ASYNC_MESSAGES
if (tcs_ != null)
{
throw new UnityException("Attempted to attach multiple handlers to a Request. This is not allowed.");
}
#endif
callback_ = callback;
Callback.AddRequest(this);
return this;
}
#if OVR_PLATFORM_ASYNC_MESSAGES
new public async Task<Message<T>> Gen()
{
if (callback_ != null || tcs_ != null)
{
throw new UnityException("Attempted to attach multiple handlers to a Request. This is not allowed.");
}
tcs_ = new TaskCompletionSource<Message<T>>();
Callback.AddRequest(this);
return await tcs_.Task;
}
#endif
override public void HandleMessage(Message msg)
{
if (! (msg is Message<T>))
{
Debug.LogError("Unable to handle message: " + msg.GetType());
return;
}
#if OVR_PLATFORM_ASYNC_MESSAGES
if (tcs_ != null)
{
tcs_.SetResult( (Message<T>)msg);
return;
}
#endif
if (callback_ != null)
{
callback_( (Message<T>)msg);
return;
}
throw new UnityException("Request with no handler. This should never happen.");
}
}
public class Request
{
#if OVR_PLATFORM_ASYNC_MESSAGES
private TaskCompletionSource<Message> tcs_;
#endif
private Message.Callback callback_;
public Request(ulong requestID) {this.RequestID = requestID;}
public ulong RequestID {get; set;}
public Request OnComplete(Message.Callback callback)
{
callback_ = callback;
Callback.AddRequest(this);
return this;
}
#if OVR_PLATFORM_ASYNC_MESSAGES
public async Task<Message> Gen() {
tcs_ = new TaskCompletionSource<Message>();
Callback.AddRequest(this);
return await tcs_.Task;
}
#endif
virtual public void HandleMessage(Message msg)
{
#if OVR_PLATFORM_ASYNC_MESSAGES
if (tcs_ != null)
{
tcs_.SetResult(msg);
return;
}
#endif
if (callback_ != null)
{
callback_(msg);
return;
}
throw new UnityException("Request with no handler. This should never happen.");
}
/**
* This will run callbacks on all messages that returned from the server.
* If too many message are coming back at once, then a limit can be passed in
* as an arg to limit the number of messages to run callbacks on at a time
*/
public static void RunCallbacks(uint limit = 0)
{
// default of 0 will run callbacks on all messages on the queue
if (limit == 0)
{
Callback.RunCallbacks();
}
else
{
Callback.RunLimitedCallbacks(limit);
}
}
}
}