-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
429 lines (394 loc) · 12.9 KB
/
client.js
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
let currentTransport, streamNumber;
let fghex = "33a5ffd491c68a214e93da10f46f01deec6e0b25c1f52ef052343aaa516930a0";
let fingerprint = [];
let bidiStreamCount = 0;
for (let c = 0; c < fghex.length - 1; c += 2) {
fingerprint.push(parseInt(fghex.substring(c, c + 2), 16));
}
var auto_test_suc_cnt = 0;
const randomData_tosend = new Set();
const recv_data_for_auto_test = new Set();
let LogTimeSpent = 0;
let LogQueue = [];
var Button_timeout = 20;
let ClientList = [];
// "Connect" button handler.
async function AddClient() {
let url = document.getElementById('url').value;
let client = new WebTransport(url, {
serverCertificateHashes: [
{
algorithm: 'sha-256',
value: new Uint8Array(fingerprint)
},
]
});
ClientList.push(client);
console.log(ClientList.length);
connect(client);
addToEventLog('Client number: ' + ClientList.length, 'summary');
flushLogQueue();
}
async function connectHandler() {
const url = document.getElementById('url').value;
try {
transport = new WebTransport(url, {
serverCertificateHashes: [
{
algorithm: 'sha-256',
value: new Uint8Array(fingerprint)
}
]
});
addToEventLog('Initiating connection...');
} catch (e) {
addToEventLog('Failed to create connection object. ' + e, 'error');
return;
}
console.log(transport);
currentTransport = transport;
await connect(transport);
await flushLogQueue();
}
async function connect(transport) {
generateRandomString();
randomString = generateRandomString(10);
// const url = document.getElementById('url').value;
// try {
// transport = new WebTransport(url, {
// serverCertificateHashes: [
// {
// algorithm: 'sha-256',
// value: new Uint8Array(fingerprint)
// }
// ]
// });
// addToEventLog('Initiating connection...');
// } catch (e) {
// addToEventLog('Failed to create connection object. ' + e, 'error');
// return;
// }
// console.log(transport);
try {
await transport.ready;
addToEventLog('Connection ready.');
} catch (e) {
addToEventLog('Connection failed. ' + e, 'error');
return;
}
transport.closed
.then(() => {
addToEventLog('Connection closed normally.');
})
.catch(() => {
addToEventLog('Connection closed abruptly.', 'error');
});
// currentTransport = transport;
streamNumber = 1;
try {
addToEventLog('Datagram writer ready.');
} catch (e) {
addToEventLog('Sending datagrams not supported: ' + e, 'error');
return;
}
readDatagrams(transport);
acceptUnidirectionalStreams(transport);
document.forms.sending.elements.send.disabled = false;
document.getElementById('connect').disabled = true;
document.getElementById('autotest').disabled = false; // Enable Auto test button
Button_checker();
flushLogQueue();
}
function Button_checker() {
// 这里要设置默认值 因为stream的上限是不同的
// 需要注意 这里uni_stream发的过多会超限(约100左右)
document.forms.sending.elements.mutiple_client_test.disabled = false;
document.getElementById('unidi-stream').addEventListener('change', function () {
if (this.value === 'unidi') {
document.getElementById('autotest-count').value = '64';
Button_timeout = 100;
}
});
document.getElementById('bidi-stream').addEventListener('change', function () {
if (this.value === 'bidi') {
document.getElementById('autotest-count').value = '64';
Button_timeout = 100;
}
});
document.getElementById('datagram').addEventListener('change', function () {
if (this.value === 'datagram') {
document.getElementById('autotest-count').value = '2048';
Button_timeout = 20;
}
});
}
// "Send data" button handler.
async function timeout(ms) {
await new Promise(r => setTimeout(r, ms));
}
async function sendDataHandler() {
let transport = currentTransport;
sendData(transport);
await timeout(100);
flushLogQueue();
}
async function sendData(transport) {
let form = document.forms.sending.elements;
let encoder = new TextEncoder('utf-8');
let random_gen_data = generateRandomString(10); // Generate and store random string
let data = encoder.encode(random_gen_data);
try {
switch (form.sendtype.value) {
case 'datagram': {
await randomData_tosend.add(random_gen_data);
let writer = transport.datagrams.writable.getWriter();
await writer.write(data);
// close the writer to indicate that we're done sending datagrams
// await transport.datagrams.writable.close(); is error
await writer.releaseLock();
await addToEventLog('Sent datagram: ' + random_gen_data);
break;
}
case 'unidi': {
let stream = await transport.createUnidirectionalStream();
let writer = stream.getWriter();
await randomData_tosend.add(random_gen_data);
await writer.write(data);
await writer.close();
addToEventLog('Sent a unidirectional stream with data: ' + random_gen_data);
break;
}
case 'bidi': {
let stream = await transport.createBidirectionalStream();
let number = streamNumber++;
acceptBidirectionalStreams(transport, stream, random_gen_data);
let writer = stream.writable.getWriter();
await writer.write(data);
await writer.close();
addToEventLog(
'Opened bidirectional stream #' + number +
' with data: ' + random_gen_data);
break;
}
}
} catch (e) {
addToEventLog('Error while sending data: ' + e, 'error');
}
}
// Reads datagrams from |transport| into the event log until EOF is reached.
// 这个地方 multi-client的时候会有问题 TODO
async function readDatagrams(transport) {
try {
var datagram_reader = transport.datagrams.readable.getReader();
addToEventLog('Datagram reader ready.');
} catch (e) {
addToEventLog('Receiving datagrams not supported: ' + e, 'error');
return;
}
let decoder = new TextDecoder('utf-8');
try {
while (true) {
const { value, done } = await datagram_reader.read();
if (done) {
addToEventLog('Done reading datagrams!');
return;
}
let data = decoder.decode(value);
await addToEventLog('Echo Datagram received: ' + data);
// await validateData(data);
recv_data_for_auto_test.add(data);
}
} catch (e) {
addToEventLog('Error while reading datagrams: ' + e, 'error');
}
}
async function closeTransport(transport) {
CloseDatagramsRead(transport);
}
async function CloseDatagramsRead(transport) {
try {
await transport.datagrams.readable.cancel();
addToEventLog('Close datagrams reader.');
} catch (e) {
addToEventLog('Close datagrams reader failed: ' + e, 'error');
}
flushLogQueue();
}
async function acceptUnidirectionalStreams(transport) {
addToEventLog('Waiting for incoming unidirectional streams...');
let reader = transport.incomingUnidirectionalStreams.getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
addToEventLog('Done accepting unidirectional streams!');
return;
}
let stream = value; // ReadableStream
let number = streamNumber++; // stream_id
// validateData(value);
readFromIncomingStream(stream, number);
}
} catch (e) {
addToEventLog('Error while accepting streams: ' + e, 'error');
}
}
async function acceptBidirectionalStreams(transport, bidiStream, expectedData) {
addToEventLog('Waiting for incoming bidirectional streams...');
let decoder = new TextDecoderStream('utf-8');
let reader = bidiStream.readable.pipeThrough(decoder).getReader();
let data = "";
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
bidiStreamCount++;
addToEventLog('Recv data on bidirectional stream: ' + bidiStreamCount + ' : ' + data);
validateData(data, expectedData);
recv_data_for_auto_test.add(data);
addToEventLog('Done accepting bidirectional streams!');
return;
}
data = value;
}
} catch (e) {
addToEventLog('Error while accepting streams: ' + e, 'error');
}
}
async function readFromIncomingStream(stream, number) {
let decoder = new TextDecoderStream('utf-8');
let reader = stream.pipeThrough(decoder).getReader();
let form = document.forms.sending.elements;
let expectedData = randomData_tosend; // Use the global randomData_tosend
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
addToEventLog('Stream #' + number + ' closed');
return;
}
let data = value;
addToEventLog('Received data on stream #' + number + ': ' + data);
validateData(data, expectedData);
if (data.length > 2)
recv_data_for_auto_test.add(data);
}
} catch (e) {
addToEventLog(
'Error while reading from stream #' + number + ': ' + e, 'error');
addToEventLog(' ' + e.message);
}
}
async function validateData(receivedData, expectedData) {
if (randomData_tosend.has(receivedData)) {
// randomData_tosend.delete(receivedData);
auto_test_suc_cnt += 1;
return;
}
if (receivedData === expectedData) auto_test_suc_cnt += 1;
}
async function addToEventLog(text, severity = 'info') {
let log = document.getElementById('event-log');
let mostRecentEntry = log.lastElementChild;
let entry = document.createElement('li');
let options = {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
};
let timestamp = new Date().toLocaleTimeString() + '.' + new Date().getMilliseconds().toString().padStart(3, '0');
entry.innerText = `[${timestamp}] ${text}`;
if (severity === 'summary') {
entry.style.fontWeight = 'bold';
entry.style.color = 'red';
} else {
entry.className = 'log-' + severity;
}
// 根据不同的严重性级别添加不同的样式
await LogQueue.push(entry);
}
async function flushLogQueue() {
console.log('queue size = ' + LogQueue.length);
let log = document.getElementById('event-log');
if (LogQueue.length > 1000) {
for (let i = 0; i < 5; i++) {
log.appendChild(LogQueue[i]);
}
let entry = document.createElement('li');
entry.innerText = '...';
log.appendChild(entry);
for (let i = LogQueue.length - 5; i < LogQueue.length; i++) {
log.appendChild(LogQueue[i]);
}
}
else {
for (let entry of LogQueue) {
log.appendChild(entry);
}
}
LogQueue = [];
log.scrollTop = log.scrollHeight;
}
// 随机生成字符串 给输入框
function generateRandomString(length = 10) {
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
let form = document.forms.sending.elements;
form.data.value = result; // Update the input field
return result; // Return the generated string
}
async function AutoTest() {
auto_test_suc_cnt = 0;
let autoTestCount = parseInt(document.getElementById('autotest-count').value, 10);
// 清空event-log
// document.getElementById('event-log').innerHTML = '';
LogTimeSpent = 0;
let record_timestamp = performance.now();
for (let i = 0; i < autoTestCount; i++) {
await addToEventLog('Running auto test iteration ' + (i + 1), 1);
// randomData_tosend = generateRandomString(); // Ensure generate new random data
await sendData(currentTransport);
}
// 计算总共耗时
let time_diff = performance.now() - record_timestamp;
// wait for 0.2s to check the result
await timeout(500);
let stateLog = document.getElementById('state-log');
// if (auto_test_suc_cnt == autoTestCount) {
if (randomData_tosend.size == recv_data_for_auto_test.size) {
addToEventLog('Auto test success! ' + auto_test_suc_cnt + '/' + autoTestCount, 'summary');
stateLog.innerText = 'State: Success';
stateLog.style.color = 'green';
} else {
addToEventLog('Auto test failed! ' + auto_test_suc_cnt + '/' + autoTestCount, 'summary');
addToEventLog('Fail test = ');
// addToEventLog("Recv data size = " + recv_data_for_auto_test.size);
// addToEventLog("Send data size = " + randomData_tosend.size);
// for (let entry of recv_data_for_auto_test) {
// if (!randomData_tosend.has(entry))
// addToEventLog(entry);
// }
stateLog.innerText = 'State: Failed';
stateLog.style.color = 'red';
}
addToEventLog('Total time: ' + time_diff + 'ms', 'summary');
randomData_tosend.clear();
recv_data_for_auto_test.clear();
flushLogQueue();
}
function clearLog() {
document.getElementById('event-log').innerHTML = '';
document.getElementById('state-log').innerText = 'State: ';
LogQueue = [];
}
async function MutipleClientTest() {
for (let entry of ClientList) {
await sendData(entry);
}
await timeout(200);
flushLogQueue();
}