• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasIPv6)
5  common.skip('no IPv6 support');
6
7const assert = require('assert');
8const dgram = require('dgram');
9
10const client = dgram.createSocket('udp6');
11
12const toSend = [Buffer.alloc(256, 'x'),
13                Buffer.alloc(256, 'y'),
14                Buffer.alloc(256, 'z'),
15                'hello'];
16
17const received = [];
18let totalBytesSent = 0;
19let totalBytesReceived = 0;
20const arrayBufferViewLength = common.getArrayBufferViews(
21  Buffer.from('')
22).length;
23
24client.on('listening', common.mustCall(() => {
25  const port = client.address().port;
26
27  client.send(toSend[0], 0, toSend[0].length, port);
28  client.send(toSend[1], port);
29  client.send([toSend[2]], port);
30  client.send(toSend[3], 0, toSend[3].length, port);
31
32  totalBytesSent += toSend.map((buf) => buf.length)
33                      .reduce((a, b) => a + b, 0);
34
35  for (const msgBuf of common.getArrayBufferViews(toSend[0])) {
36    client.send(msgBuf, 0, msgBuf.byteLength, port);
37    totalBytesSent += msgBuf.byteLength;
38  }
39  for (const msgBuf of common.getArrayBufferViews(toSend[1])) {
40    client.send(msgBuf, port);
41    totalBytesSent += msgBuf.byteLength;
42  }
43  for (const msgBuf of common.getArrayBufferViews(toSend[2])) {
44    client.send([msgBuf], port);
45    totalBytesSent += msgBuf.byteLength;
46  }
47}));
48
49client.on('message', common.mustCall((buf, info) => {
50  received.push(buf.toString());
51  totalBytesReceived += info.size;
52
53  if (totalBytesReceived === totalBytesSent) {
54    client.close();
55  }
56  // For every buffer in `toSend`, we send the raw Buffer,
57  // as well as every TypedArray in getArrayBufferViews()
58}, toSend.length + (toSend.length - 1) * arrayBufferViewLength));
59
60client.on('close', common.mustCall((buf, info) => {
61  // The replies may arrive out of order -> sort them before checking.
62  received.sort();
63
64  const repeated = [...toSend];
65  for (let i = 0; i < arrayBufferViewLength; i++) {
66    // We get arrayBufferViews only for toSend[0..2].
67    repeated.push(...toSend.slice(0, 3));
68  }
69
70  assert.strictEqual(totalBytesSent, totalBytesReceived);
71
72  const expected = repeated.map(String).sort();
73  assert.deepStrictEqual(received, expected);
74}));
75
76client.bind(0);
77