-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathTestHub.cs
368 lines (309 loc) · 12.5 KB
/
TestHub.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Threading.Channels;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
namespace TestServer
{
public class TestHub : Hub
{
private ILogger _logger;
public TestHub(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<TestHub>();
}
public string Echo(string message)
{
_logger.LogInformation("Echo invoked: " + message);
return message;
}
public void ErrorMethod()
{
throw new InvalidOperationException("Error occurred.");
}
public Task VoidMethod()
{
return Task.CompletedTask;
}
public Task InvokeGetNumber(int number)
{
return Clients.Client(Context.ConnectionId).SendAsync("GetNumber", number);
}
public Task InvokeGetPerson(Person person)
{
return Clients.Client(Context.ConnectionId).SendAsync("GetPerson", person);
}
public IEnumerable<Person> SortByName(Person[] people)
{
return people.OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
}
public ChannelReader<int> StreamNumbers(int count, int delay)
{
var channel = Channel.CreateUnbounded<int>();
Task.Run(async () =>
{
for (var i = 0; i < count; i++)
{
await channel.Writer.WriteAsync(i);
await Task.Delay(delay);
}
channel.Writer.TryComplete();
});
return channel.Reader;
}
public ChannelReader<string> ErrorStreamMethod()
{
var channel = Channel.CreateUnbounded<string>();
Task.Run(async () =>
{
await channel.Writer.WriteAsync("abc");
await channel.Writer.WriteAsync(null);
channel.Writer.TryComplete(new InvalidOperationException("Error occurred while streaming."));
});
return channel.Reader;
}
public string GetHeader(string name)
{
Context.GetHttpContext().Request.Headers.TryGetValue(name, out var header);
return header;
}
public void KillConnection()
{
Context.Abort();
}
public Task InvokeManyArgs(object[] args)
{
return Clients.Client(Context.ConnectionId).SendCoreAsync("ManyArgs", args);
}
public async Task<bool> InvokeNoArgs()
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs");
return true;
}
public async Task<bool> InvokeManyArgs1(object arg1)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1);
return true;
}
public async Task<bool> InvokeManyArgs2(object arg1, object arg2)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2);
return true;
}
public async Task<bool> InvokeManyArgs3(object arg1, object arg2, object arg3)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2, arg3);
return true;
}
public async Task<bool> InvokeManyArgs4(object arg1, object arg2, object arg3, object arg4)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2, arg3, arg4);
return true;
}
public async Task<bool> InvokeManyArgs5(object arg1, object arg2, object arg3, object arg4, object arg5)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2, arg3, arg4, arg5);
return true;
}
public async Task<bool> InvokeManyArgs6(object arg1, object arg2, object arg3, object arg4, object arg5, object arg6)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2, arg3, arg4, arg5, arg6);
return true;
}
public async Task<bool> InvokeManyArgs7(object arg1, object arg2, object arg3, object arg4, object arg5, object arg6, object arg7)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2, arg3, arg4, arg5, arg6, arg7);
return true;
}
public async Task<bool> InvokeManyArgs8(object arg1, object arg2, object arg3, object arg4, object arg5, object arg6, object arg7, object arg8)
{
await Clients.Client(Context.ConnectionId).SendAsync("ManyArgs", arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
return true;
}
public async Task InvokeWithArgs0VoidWithClientStream(IAsyncEnumerable<int> stream)
{
await InvokeWithManyArgsVoidWithClientStream(stream, [1]);
}
public async Task InvokeWithArgs1VoidWithClientStream(IAsyncEnumerable<int> stream, int arg1)
{
await InvokeWithManyArgsVoidWithClientStream(stream, [arg1]);
}
public async Task InvokeWithArgs2VoidWithClientStream(IAsyncEnumerable<int> stream, int arg1, int arg2)
{
await InvokeWithManyArgsVoidWithClientStream(stream, [arg1, arg2]);
}
public async Task InvokeWithArgs3VoidWithClientStream(IAsyncEnumerable<int> stream, int arg1, int arg2, int arg3)
{
await InvokeWithManyArgsVoidWithClientStream(stream, [arg1, arg2, arg3]);
}
public async Task InvokeWithArgs4VoidWithClientStream(IAsyncEnumerable<int> stream, int arg1, int arg2, int arg3, int arg4)
{
await InvokeWithManyArgsVoidWithClientStream(stream, [arg1, arg2, arg3, arg4]);
}
public async Task InvokeWithManyArgsVoidWithClientStream(IAsyncEnumerable<int> stream, int[] modifiers)
{
var result = 0;
var idx = 0;
await foreach (var value in stream)
{
result += value * modifiers[idx];
idx = (idx + 1) % modifiers.Length;
}
await Clients.All.SendAsync("ClientStreamResult", result);
}
public Task<int> InvokeWithArgs0WithClientStream(IAsyncEnumerable<int> stream)
{
return InvokeWithManyArgsWithClientStream(stream, [1]);
}
public Task<int> InvokeWithArgs1WithClientStream(IAsyncEnumerable<int> stream, int arg1)
{
return InvokeWithManyArgsWithClientStream(stream, [arg1]);
}
public Task<int> InvokeWithArgs2WithClientStream(IAsyncEnumerable<int> stream, int arg1, int arg2)
{
return InvokeWithManyArgsWithClientStream(stream, [arg1, arg2]);
}
public Task<int> InvokeWithArgs3WithClientStream(IAsyncEnumerable<int> stream, int arg1, int arg2, int arg3)
{
return InvokeWithManyArgsWithClientStream(stream, [arg1, arg2, arg3]);
}
public Task<int> InvokeWithArgs4WithClientStream(IAsyncEnumerable<int> stream, int arg1, int arg2, int arg3, int arg4)
{
return InvokeWithManyArgsWithClientStream(stream, [arg1, arg2, arg3, arg4]);
}
public async Task<int> InvokeWithManyArgsWithClientStream(IAsyncEnumerable<int> stream, int[] modifiers)
{
var result = 0;
var idx = 0;
await foreach (var value in stream)
{
result += value * modifiers[idx];
idx = (idx + 1) % modifiers.Length;
}
return result;
}
public string Concatenate(string s, int n)
{
return $"{s} {n}";
}
public ChannelReader<object> StreamManyArgs0()
{
return StreamManyArgs(new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
}
public ChannelReader<object> StreamManyArgs1(object n1)
{
return StreamManyArgs(new[] { n1 });
}
public ChannelReader<object> StreamManyArgs2(object n1, object n2)
{
return StreamManyArgs(new[] { n1, n2 });
}
public ChannelReader<object> StreamManyArgs3(object n1, object n2, object n3)
{
return StreamManyArgs(new[] { n1, n2, n3 });
}
public ChannelReader<object> StreamManyArgs4(object n1, object n2, object n3, object n4)
{
return StreamManyArgs(new[] { n1, n2, n3, n4 });
}
public ChannelReader<object> StreamManyArgs5(object n1, object n2, object n3, object n4, object n5)
{
return StreamManyArgs(new[] { n1, n2, n3, n4, n5 });
}
public ChannelReader<object> StreamManyArgs6(object n1, object n2, object n3, object n4, object n5, object n6)
{
return StreamManyArgs(new[] { n1, n2, n3, n4, n5, n6 });
}
public ChannelReader<object> StreamManyArgs7(object n1, object n2, object n3, object n4, object n5, object n6, object n7)
{
return StreamManyArgs(new[] { n1, n2, n3, n4, n5, n6, n7 });
}
public ChannelReader<object> StreamManyArgs8(object n1, object n2, object n3, object n4, object n5, object n6, object n7, object n8)
{
return StreamManyArgs(new[] { n1, n2, n3, n4, n5, n6, n7, n8 });
}
private ChannelReader<object> StreamManyArgs(object[] items)
{
var channel = Channel.CreateUnbounded<object>();
Task.Run(async () =>
{
foreach (var item in items)
{
await channel.Writer.WriteAsync(item);
}
channel.Writer.TryComplete();
});
return channel.Reader;
}
public ChannelReader<object> StreamManyArgs0WithClientStream(IAsyncEnumerable<int> stream)
{
return StreamWithManyArgsWithClientStream(stream, [1]);
}
public ChannelReader<object> StreamManyArgs1WithClientStream(IAsyncEnumerable<int> stream, int n1)
{
return StreamWithManyArgsWithClientStream(stream, [n1]);
}
public ChannelReader<object> StreamManyArgs2WithClientStream(IAsyncEnumerable<int> stream, int n1, int n2)
{
return StreamWithManyArgsWithClientStream(stream, [n1, n2]);
}
public ChannelReader<object> StreamManyArgs3WithClientStream(IAsyncEnumerable<int> stream, int n1, int n2, int n3)
{
return StreamWithManyArgsWithClientStream(stream, [n1, n2, n3]);
}
public ChannelReader<object> StreamManyArgs4WithClientStream(IAsyncEnumerable<int> stream, int n1, int n2, int n3, int n4)
{
return StreamWithManyArgsWithClientStream(stream, [n1, n2, n3, n4]);
}
private ChannelReader<object> StreamWithManyArgsWithClientStream(IAsyncEnumerable<int> stream, int[] modifiers)
{
var channel = Channel.CreateUnbounded<object>();
_ = Task.Run(async () =>
{
var idx = 0;
await foreach (var value in stream)
{
await channel.Writer.WriteAsync(value * modifiers[idx]);
idx = (idx + 1) % modifiers.Length;
}
channel.Writer.TryComplete();
});
return channel.Reader;
}
public enum SHAType
{
SHA1,
SHA256
}
public class MessageSHA
{
public byte[] Value { get; set; }
public SHAType SHAType { get; set; }
}
public async IAsyncEnumerable<MessageSHA> ComputeSHA(IAsyncEnumerable<byte[]> messageStream, SHAType shaType)
{
HashAlgorithm CreateAlgorithm(SHAType shaType)
{
switch (shaType)
{
case SHAType.SHA1:
return SHA1.Create();
case SHAType.SHA256:
return SHA256.Create();
default:
throw new ArgumentException($"Unrecognized SHA type {shaType}", nameof(shaType));
}
}
using (var hashAlgorithm = CreateAlgorithm(shaType))
{
await foreach (var message in messageStream)
{
yield return new MessageSHA { Value = hashAlgorithm.ComputeHash(message), SHAType = shaType };
}
}
}
}
}