11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst dgram = require('dgram'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst toSend = [Buffer.alloc(256, 'x'), 101cb0ef41Sopenharmony_ci Buffer.alloc(256, 'y'), 111cb0ef41Sopenharmony_ci Buffer.alloc(256, 'z'), 121cb0ef41Sopenharmony_ci 'hello']; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst received = []; 151cb0ef41Sopenharmony_cilet totalBytesSent = 0; 161cb0ef41Sopenharmony_cilet totalBytesReceived = 0; 171cb0ef41Sopenharmony_ciconst arrayBufferViewsCount = common.getArrayBufferViews( 181cb0ef41Sopenharmony_ci Buffer.from('') 191cb0ef41Sopenharmony_ci).length; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciclient.on('listening', common.mustCall(() => { 221cb0ef41Sopenharmony_ci const port = client.address().port; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci client.send(toSend[0], 0, toSend[0].length, port); 251cb0ef41Sopenharmony_ci client.send(toSend[1], port); 261cb0ef41Sopenharmony_ci client.send([toSend[2]], port); 271cb0ef41Sopenharmony_ci client.send(toSend[3], 0, toSend[3].length, port); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci totalBytesSent += toSend.map((buf) => buf.length) 301cb0ef41Sopenharmony_ci .reduce((a, b) => a + b, 0); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci for (const msgBuf of common.getArrayBufferViews(toSend[0])) { 331cb0ef41Sopenharmony_ci client.send(msgBuf, 0, msgBuf.byteLength, port); 341cb0ef41Sopenharmony_ci totalBytesSent += msgBuf.byteLength; 351cb0ef41Sopenharmony_ci } 361cb0ef41Sopenharmony_ci for (const msgBuf of common.getArrayBufferViews(toSend[1])) { 371cb0ef41Sopenharmony_ci client.send(msgBuf, port); 381cb0ef41Sopenharmony_ci totalBytesSent += msgBuf.byteLength; 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci for (const msgBuf of common.getArrayBufferViews(toSend[2])) { 411cb0ef41Sopenharmony_ci client.send([msgBuf], port); 421cb0ef41Sopenharmony_ci totalBytesSent += msgBuf.byteLength; 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci})); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciclient.on('message', common.mustCall((buf, info) => { 471cb0ef41Sopenharmony_ci received.push(buf.toString()); 481cb0ef41Sopenharmony_ci totalBytesReceived += info.size; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci if (totalBytesReceived === totalBytesSent) { 511cb0ef41Sopenharmony_ci client.close(); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci // For every buffer in `toSend`, we send the raw Buffer, 541cb0ef41Sopenharmony_ci // as well as every TypedArray in getArrayBufferViews() 551cb0ef41Sopenharmony_ci}, toSend.length + (toSend.length - 1) * arrayBufferViewsCount)); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciclient.on('close', common.mustCall((buf, info) => { 581cb0ef41Sopenharmony_ci // The replies may arrive out of order -> sort them before checking. 591cb0ef41Sopenharmony_ci received.sort(); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci const repeated = [...toSend]; 621cb0ef41Sopenharmony_ci for (let i = 0; i < arrayBufferViewsCount; i++) { 631cb0ef41Sopenharmony_ci repeated.push(...toSend.slice(0, 3)); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci assert.strictEqual(totalBytesSent, totalBytesReceived); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci const expected = repeated.map(String).sort(); 691cb0ef41Sopenharmony_ci assert.deepStrictEqual(received, expected); 701cb0ef41Sopenharmony_ci})); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciclient.bind(0); 73