-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
HubProxy.cs
211 lines (187 loc) · 7.03 KB
/
HubProxy.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Infrastructure;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNet.SignalR.Client.Hubs
{
public class HubProxy : IHubProxy
{
private readonly string _hubName;
private readonly IHubConnection _connection;
private readonly Dictionary<string, JToken> _state = new Dictionary<string, JToken>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Subscription> _subscriptions = new Dictionary<string, Subscription>(StringComparer.OrdinalIgnoreCase);
public HubProxy(IHubConnection connection, string hubName)
{
_connection = connection;
_hubName = hubName;
}
public JToken this[string name]
{
get
{
lock (_state)
{
JToken value;
_state.TryGetValue(name, out value);
return value;
}
}
set
{
lock (_state)
{
_state[name] = value;
}
}
}
public JsonSerializer JsonSerializer
{
get { return _connection.JsonSerializer; }
}
public Subscription Subscribe(string eventName)
{
if (eventName == null)
{
throw new ArgumentNullException("eventName");
}
Subscription subscription;
if (!_subscriptions.TryGetValue(eventName, out subscription))
{
subscription = new Subscription();
_subscriptions.Add(eventName, subscription);
}
return subscription;
}
public Task Invoke(string method, params object[] args)
{
return Invoke<object>(method, args);
}
public Task<T> Invoke<T>(string method, params object[] args)
{
return Invoke<T, object>(method, onProgress: null, args: args);
}
public Task Invoke<T>(string method, Action<T> onProgress, params object[] args)
{
return Invoke<object, T>(method, onProgress, args);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are flown to the caller")]
public Task<TResult> Invoke<TResult, TProgress>(string method, Action<TProgress> onProgress, params object[] args)
{
if (method == null)
{
throw new ArgumentNullException("method");
}
if (args == null)
{
throw new ArgumentNullException("args");
}
var tokenifiedArguments = new JToken[args.Length];
for (int i = 0; i < tokenifiedArguments.Length; i++)
{
tokenifiedArguments[i] = args[i] != null
? JToken.FromObject(args[i], JsonSerializer)
: JValue.CreateNull();
}
var tcs = new DispatchingTaskCompletionSource<TResult>();
var callbackId = _connection.RegisterCallback(result =>
{
if (result != null)
{
if (result.Error != null)
{
if (result.IsHubException.HasValue && result.IsHubException.Value)
{
// A HubException was thrown
tcs.TrySetException(new HubException(result.Error, result.ErrorData));
}
else
{
tcs.TrySetException(new InvalidOperationException(result.Error));
}
}
else
{
try
{
if (result.State != null)
{
foreach (var pair in result.State)
{
this[pair.Key] = pair.Value;
}
}
if (result.ProgressUpdate != null)
{
onProgress(result.ProgressUpdate.Data.ToObject<TProgress>(JsonSerializer));
}
else if (result.Result != null)
{
tcs.TrySetResult(result.Result.ToObject<TResult>(JsonSerializer));
}
else
{
tcs.TrySetResult(default(TResult));
}
}
catch (Exception ex)
{
// If we failed to set the result for some reason or to update
// state then just fail the tcs.
tcs.TrySetUnwrappedException(ex);
}
}
}
else
{
tcs.TrySetCanceled();
}
});
var hubData = new HubInvocation
{
Hub = _hubName,
Method = method,
Args = tokenifiedArguments,
CallbackId = callbackId
};
string value = null;
lock (_state)
{
if (_state.Count != 0)
{
hubData.State = _state;
// Only keep the _state lock during JsonSerializeObject if hubData.State is set to _state.
value = _connection.JsonSerializeObject(hubData);
}
}
value = value ?? _connection.JsonSerializeObject(hubData);
_connection.Send(value).ContinueWith(task =>
{
if (task.IsCanceled)
{
_connection.RemoveCallback(callbackId);
tcs.TrySetCanceled();
}
else if (task.IsFaulted)
{
_connection.RemoveCallback(callbackId);
tcs.TrySetUnwrappedException(task.Exception);
}
},
TaskContinuationOptions.NotOnRanToCompletion);
return tcs.Task;
}
public void InvokeEvent(string eventName, IList<JToken> args)
{
Subscription subscription;
if (_subscriptions.TryGetValue(eventName, out subscription))
{
subscription.OnReceived(args);
}
}
}
}