Skip to content

Commit ceaac85

Browse files
committed
wip: examples are working now
* Related #20 [ci skip]
1 parent fa45371 commit ceaac85

File tree

2 files changed

+78
-26
lines changed

2 files changed

+78
-26
lines changed

clientTest.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ async function main () {
2424
const host = "127.0.0.1";
2525
const port = 4433;
2626
const localPort = 55555;
27-
const STREAMS = 1000;
28-
const MESSAGES = 200;
27+
const STREAMS = 5000;
28+
const MESSAGES = 10;
29+
let finsSent = 0;
30+
let finsRecv = 0;
2931

3032
type StreamData = {
3133
messagesLeft: number;
@@ -164,7 +166,7 @@ async function main () {
164166
port: remoteInfo.port,
165167
},
166168
};
167-
console.log(recvInfo);
169+
// console.log('received bytes', data.byteLength);
168170
conn.recv(data, recvInfo);
169171
receivedEvent.resolveP();
170172
receivedEvent = promise();
@@ -178,6 +180,7 @@ async function main () {
178180
console.error(e);
179181
throw e;
180182
});
183+
// console.log('sent bytes ', write)
181184

182185
checkTimeout();
183186

@@ -215,9 +218,14 @@ async function main () {
215218
while(true) {
216219
try {
217220
const [_read, fin] = conn.streamRecv(streamId, buf);
218-
if (fin) console.log('stream finished: ', streamId);
221+
if (fin) {
222+
finsRecv ++;
223+
// console.log('stream finished: ', streamId, 'finsRecv', finsRecv);
224+
break;
225+
}
219226
} catch (e) {
220227
if (e.message == 'Done') break;
228+
console.log('error', e.message, )
221229
throw e;
222230
}
223231
}
@@ -237,10 +245,12 @@ async function main () {
237245
try {
238246
conn.streamSend(streamId, emptyBuffer, true);
239247
streamMap.delete(streamId);
240-
if (streamMap.size === 0) console.log('finished all streams')
248+
finsSent++;
249+
// console.log('finsihed', finsSent, streamMap.size)
250+
if (streamMap.size === 0) console.log('finished all streams, fins sent', finsSent)
241251
} catch (e) {
242252
if(e.message == 'Done') {
243-
// console.log('sending returned done');
253+
console.log('sending returned done');
244254
continue
245255
}
246256
throw e;
@@ -272,7 +282,7 @@ async function main () {
272282
throw e;
273283
}
274284
await socketSend(out, 0, write, sendInfo.to.port, sendInfo.to.host);
275-
// console.log('packet sent');
285+
// console.log('sent bytes ', write);
276286
checkTimeout();
277287
}
278288

serverTest.ts

+61-19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async function main () {
2020
timeout: NodeJS.Timeout | null;
2121
deadline: number;
2222
streams: Set<number>;
23+
fins: number;
2324
}
2425
const connectionMap: Map<string, ConnectionData> = new Map();
2526

@@ -157,7 +158,7 @@ async function main () {
157158
return;
158159
}
159160

160-
const dcid: Uint8Array = header.dcid;
161+
const dcid: Buffer = Buffer.from(header.dcid);
161162
let scid: Uint8Array = new QUICConnectionId(
162163
await crypto.ops.sign(
163164
crypto.key,
@@ -168,7 +169,8 @@ async function main () {
168169
);
169170

170171
let client: ConnectionData;
171-
if (!connectionMap.has(Buffer.from(dcid).toString())) {
172+
if (!connectionMap.has(dcid.toString())) {
173+
console.log('Got a new connection!!!!!!!!!!', dcid.toString('hex'))
172174
if (header.ty !== quiche.Type.Initial) {
173175
console.log(`QUIC packet must be Initial for new connections`);
174176
return;
@@ -192,9 +194,12 @@ async function main () {
192194
remoteInfo.port,
193195
remoteInfo.address,
194196
);
195-
} catch {
197+
// console.log('sent bytes', versionDatagramLength);
198+
} catch (e) {
199+
console.error(e);
196200
return;
197201
}
202+
console.log(`Sent VersionNegotiation packet to ${remoteInfo.address}"${remoteInfo.port}`);
198203
return;
199204
}
200205
// At this point we are processing an `Initial` packet.
@@ -203,6 +208,7 @@ async function main () {
203208
const token = header.token!;
204209
// Stateless Retry
205210
if (token.byteLength === 0) {
211+
console.log('Doing stateless retry')
206212
const token = await mintToken(dcid, remoteInfo.address, crypto);
207213
const retryDatagram = Buffer.allocUnsafe(quiche.MAX_DATAGRAM_SIZE);
208214
const retryDatagramLength = quiche.retry(
@@ -221,53 +227,76 @@ async function main () {
221227
remoteInfo.port,
222228
remoteInfo.address,
223229
);
230+
// console.log('sent bytes ', retryDatagramLength);
224231
} catch (e) {
232+
console.error(e);
225233
return;
226234
}
235+
console.log(`Sent Retry packet to ${remoteInfo.address}"${remoteInfo.port}`)
227236
return;
228237
}
229238
// At this point in time, the packet's DCID is the originally-derived DCID.
230239
// While the DCID embedded in the token is the original DCID that the client first created.
240+
console.log(
241+
'validate',
242+
Buffer.from(token),
243+
remoteInfo.address,
244+
);
231245
const dcidOriginal = await validateToken(
232246
Buffer.from(token),
233247
remoteInfo.address,
234248
crypto,
235249
);
250+
console.log(dcidOriginal);
236251
if (dcidOriginal == null) {
252+
console.log(
253+
`QUIC packet token failed validation due to missing DCID`,
254+
);
237255
return;
238256
}
239257
// Check that the newly-derived DCID (passed in as the SCID) is the same
240258
// length as the packet DCID.
241259
// This ensures that the derivation process hasn't changed.
242260
if (scid.byteLength !== header.dcid.byteLength) {
261+
console.log(
262+
`QUIC packet token failed validation due to mismatched length`,
263+
);
243264
return;
244265
}
245266
// Here we shall re-use the originally-derived DCID as the SCID
246267
scid = new QUICConnectionId(header.dcid);
247-
248-
console.log('creating new connection')
268+
console.log(
269+
`Accepting new connection from QUIC packet from ${remoteInfo.address}:${remoteInfo.port}`,
270+
);
271+
const localHost = {
272+
host: host,
273+
port: localPort,
274+
};
275+
const remoteHost = {
276+
host: remoteInfo.address,
277+
port: remoteInfo.port,
278+
};
279+
console.log(
280+
dcidOriginal,
281+
)
249282
const conn = quiche.Connection.accept(
250283
scid,
251284
dcidOriginal,
252-
{
253-
host: host,
254-
port: localPort,
255-
},
256-
{
257-
host: remoteInfo.address,
258-
port: remoteInfo.port,
259-
},
285+
localHost,
286+
remoteHost,
260287
config,
261288
)
262289
client = {
263290
deadline: Infinity,
264291
timeout: null,
265292
streams: new Set(),
266-
conn
293+
conn,
294+
fins: 0,
267295
}
296+
console.log('setting connection ', Buffer.from(scid).toString('hex'));
268297
connectionMap.set(Buffer.from(scid).toString(), client);
269298
} else {
270-
client = connectionMap.get(Buffer.from(dcid).toString())!;
299+
client = connectionMap.get(dcid.toString())!;
271300
}
272301

273302
client.conn.recv(data, recvInfo);
@@ -293,10 +322,17 @@ async function main () {
293322
const [, fin] = client.conn.streamRecv(streamId, data);
294323
if (fin) {
295324
client.streams.delete(streamId)
296-
console.log("Stream finished! ", streamId, "left", client.streams.size);
325+
client.fins ++;
326+
console.log("Stream finished! ", streamId, "left", client.streams.size, 'fins:', client.fins);
327+
break;
297328
}
298329
} catch (e) {
299330
if (e.message === 'Done') break;
331+
console.error(e);
332+
if (/^InvalidStreamState.*/.test(e.message)) {
333+
console.log('got ', e.message)
334+
break;
335+
}
300336
throw e;
301337
}
302338
}
@@ -326,12 +362,12 @@ async function main () {
326362
let sendInfo: SendInfo;
327363
try {
328364
[write, sendInfo] = client.conn.send(out);
329-
console.log(sendInfo);
330365
} catch (e) {
331366
if (e.message == 'Done') break;
332367
throw e;
333368
}
334369
await socketSend(out, 0, write, sendInfo.to.port, sendInfo.to.host);
370+
// console.log('sent bytes ', write);
335371
checkTimeout(client);
336372
}
337373
if (client.conn.isClosed()) {
@@ -350,7 +386,7 @@ async function mintToken(
350386
peerHost: string,
351387
crypto: any
352388
): Promise<Buffer> {
353-
const msgData = { dcid: Buffer.from(dcid).toString(), host: peerHost };
389+
const msgData = { dcid: Buffer.from(dcid).toString('hex'), host: peerHost };
354390
const msgJSON = JSON.stringify(msgData);
355391
const msgBuffer = Buffer.from(msgJSON);
356392
const msgSig = Buffer.from(
@@ -372,9 +408,11 @@ async function validateToken(
372408
let tokenData;
373409
try {
374410
tokenData = JSON.parse(tokenBuffer.toString());
375-
} catch {
411+
console.log(tokenData);
412+
} catch (e){
376413
return;
377414
}
415+
console.log(tokenData)
378416
if (typeof tokenData !== 'object' || tokenData == null) {
379417
return;
380418
}
@@ -395,6 +433,7 @@ async function validateToken(
395433
} catch {
396434
return;
397435
}
436+
console.log('data', msgData);
398437
if (typeof msgData !== 'object' || msgData == null) {
399438
return;
400439
}
@@ -404,6 +443,9 @@ async function validateToken(
404443
if (msgData.host !== peerHost) {
405444
return;
406445
}
446+
console.log('dcid', msgData.dcid);
447+
console.log('id', QUICConnectionId.fromString(msgData.dcid))
448+
console.log('buf', Buffer.from(msgData.dcid, 'hex'));
407449
return QUICConnectionId.fromString(msgData.dcid);
408450
}
409451

0 commit comments

Comments
 (0)