Bug 1558738. Stop using [array] in nsIUDPSocket. r=valentin

Differential Revision: https://phabricator.services.mozilla.com/D34672

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Boris Zbarsky 2019-06-12 07:00:01 +00:00
Родитель ec33fbcdf2
Коммит 8e413da004
7 изменённых файлов: 43 добавлений и 58 удалений

Просмотреть файл

@ -373,14 +373,12 @@ void UDPSocketParent::Send(const InfallibleTArray<uint8_t>& aData,
switch (aAddr.type()) {
case UDPSocketAddr::TUDPAddressInfo: {
const UDPAddressInfo& addrInfo(aAddr.get_UDPAddressInfo());
rv = mSocket->Send(addrInfo.addr(), addrInfo.port(), aData.Elements(),
aData.Length(), &count);
rv = mSocket->Send(addrInfo.addr(), addrInfo.port(), aData, &count);
break;
}
case UDPSocketAddr::TNetAddr: {
const NetAddr& addr(aAddr.get_NetAddr());
rv = mSocket->SendWithAddress(&addr, aData.Elements(), aData.Length(),
&count);
rv = mSocket->SendWithAddress(&addr, aData, &count);
break;
}
default:

Просмотреть файл

@ -147,12 +147,10 @@ interface nsIUDPSocket : nsISupports
* @param host The remote host name.
* @param port The remote port.
* @param data The buffer containing the data to be written.
* @param dataLength The maximum number of bytes to be written.
* @return number of bytes written. (0 or dataLength)
* @return number of bytes written. (0 or length of data)
*/
unsigned long send(in AUTF8String host, in unsigned short port,
[const, array, size_is(dataLength)]in uint8_t data,
in unsigned long dataLength);
in Array<uint8_t> data);
/**
* sendWithAddr
@ -161,12 +159,10 @@ interface nsIUDPSocket : nsISupports
*
* @param addr The remote host address.
* @param data The buffer containing the data to be written.
* @param dataLength The maximum number of bytes to be written.
* @return number of bytes written. (0 or dataLength)
* @return number of bytes written. (0 or length of data)
*/
unsigned long sendWithAddr(in nsINetAddr addr,
[const, array, size_is(dataLength)]in uint8_t data,
in unsigned long dataLength);
in Array<uint8_t> data);
/**
* sendWithAddress
@ -175,12 +171,10 @@ interface nsIUDPSocket : nsISupports
*
* @param addr The remote host address.
* @param data The buffer containing the data to be written.
* @param dataLength The maximum number of bytes to be written.
* @return number of bytes written. (0 or dataLength)
* @return number of bytes written. (0 or length of data)
*/
[noscript] unsigned long sendWithAddress([const] in NetAddrPtr addr,
[const, array, size_is(dataLength)]in uint8_t data,
in unsigned long dataLength);
in Array<uint8_t> data);
/**
* sendBinaryStream

Просмотреть файл

@ -982,8 +982,7 @@ PendingSend::OnLookupComplete(nsICancelable* request, nsIDNSRecord* rec,
NetAddr addr;
if (NS_SUCCEEDED(rec->GetNextAddr(mPort, &addr))) {
uint32_t count;
nsresult rv = mSocket->SendWithAddress(&addr, mData.Elements(),
mData.Length(), &count);
nsresult rv = mSocket->SendWithAddress(&addr, mData, &count);
NS_ENSURE_SUCCESS(rv, rv);
}
@ -1060,7 +1059,7 @@ class SendRequestRunnable : public Runnable {
NS_IMETHODIMP
SendRequestRunnable::Run() {
uint32_t count;
mSocket->SendWithAddress(&mAddr, mData.Elements(), mData.Length(), &count);
mSocket->SendWithAddress(&mAddr, mData, &count);
return NS_OK;
}
@ -1086,17 +1085,14 @@ nsUDPSocket::AsyncListen(nsIUDPSocketListener* aListener) {
}
NS_IMETHODIMP
nsUDPSocket::Send(const nsACString& aHost, uint16_t aPort, const uint8_t* aData,
uint32_t aDataLength, uint32_t* _retval) {
nsUDPSocket::Send(const nsACString& aHost, uint16_t aPort,
const nsTArray<uint8_t>& aData, uint32_t* _retval) {
NS_ENSURE_ARG_POINTER(_retval);
if (!((aData && aDataLength > 0) || (!aData && !aDataLength))) {
return NS_ERROR_INVALID_ARG;
}
*_retval = 0;
FallibleTArray<uint8_t> fallibleArray;
if (!fallibleArray.InsertElementsAt(0, aData, aDataLength, fallible)) {
if (!fallibleArray.InsertElementsAt(0, aData, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -1106,27 +1102,26 @@ nsUDPSocket::Send(const nsACString& aHost, uint16_t aPort, const uint8_t* aData,
nsresult rv = ResolveHost(aHost, mOriginAttributes, listener);
NS_ENSURE_SUCCESS(rv, rv);
*_retval = aDataLength;
*_retval = aData.Length();
return NS_OK;
}
NS_IMETHODIMP
nsUDPSocket::SendWithAddr(nsINetAddr* aAddr, const uint8_t* aData,
uint32_t aDataLength, uint32_t* _retval) {
nsUDPSocket::SendWithAddr(nsINetAddr* aAddr, const nsTArray<uint8_t>& aData,
uint32_t* _retval) {
NS_ENSURE_ARG(aAddr);
NS_ENSURE_ARG(aData);
NS_ENSURE_ARG_POINTER(_retval);
NetAddr netAddr;
aAddr->GetNetAddr(&netAddr);
return SendWithAddress(&netAddr, aData, aDataLength, _retval);
return SendWithAddress(&netAddr, aData, _retval);
}
NS_IMETHODIMP
nsUDPSocket::SendWithAddress(const NetAddr* aAddr, const uint8_t* aData,
uint32_t aDataLength, uint32_t* _retval) {
nsUDPSocket::SendWithAddress(const NetAddr* aAddr,
const nsTArray<uint8_t>& aData,
uint32_t* _retval) {
NS_ENSURE_ARG(aAddr);
NS_ENSURE_ARG(aData);
NS_ENSURE_ARG_POINTER(_retval);
*_retval = 0;
@ -1143,8 +1138,9 @@ nsUDPSocket::SendWithAddress(const NetAddr* aAddr, const uint8_t* aData,
// socket is not initialized or has been closed
return NS_ERROR_FAILURE;
}
int32_t count = PR_SendTo(mFD, aData, sizeof(uint8_t) * aDataLength, 0,
&prAddr, PR_INTERVAL_NO_WAIT);
int32_t count =
PR_SendTo(mFD, aData.Elements(), sizeof(uint8_t) * aData.Length(), 0,
&prAddr, PR_INTERVAL_NO_WAIT);
if (count < 0) {
PRErrorCode code = PR_GetError();
return ErrorAccordingToNSPR(code);
@ -1153,7 +1149,7 @@ nsUDPSocket::SendWithAddress(const NetAddr* aAddr, const uint8_t* aData,
*_retval = count;
} else {
FallibleTArray<uint8_t> fallibleArray;
if (!fallibleArray.InsertElementsAt(0, aData, aDataLength, fallible)) {
if (!fallibleArray.InsertElementsAt(0, aData, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -1161,7 +1157,7 @@ nsUDPSocket::SendWithAddress(const NetAddr* aAddr, const uint8_t* aData,
new SendRequestRunnable(this, *aAddr, std::move(fallibleArray)),
NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
*_retval = aDataLength;
*_retval = aData.Length();
}
return NS_OK;
}

Просмотреть файл

@ -316,7 +316,7 @@ class MulticastDNS {
let packet = this._makeServicePacket(svc, [address]);
let data = packet.serialize();
try {
socket.send(target.address, target.port, data, data.length);
socket.send(target.address, target.port, data);
} catch (err) {
DEBUG && debug("Failed to send packet to "
+ target.address + ":" + target.port);
@ -402,7 +402,7 @@ class MulticastDNS {
this._getQuerySocket().then((querySocket) => {
DEBUG && debug('sending query on query socket ("' + name + '")');
querySocket.send(MDNS_MULTICAST_GROUP, MDNS_PORT, data, data.length);
querySocket.send(MDNS_MULTICAST_GROUP, MDNS_PORT, data);
});
// Automatically announce previously-discovered

Просмотреть файл

@ -101,9 +101,10 @@ UDPClientListener::OnPacketReceived(nsIUDPSocket* socket,
if (TEST_SEND_API == phase && CheckMessageContent(message, REQUEST)) {
uint32_t count;
const uint32_t data = RESPONSE;
mResult = socket->SendWithAddr(fromAddr, (const uint8_t*)&data,
sizeof(uint32_t), &count);
nsTArray<uint8_t> data;
const uint32_t dataBuffer = RESPONSE;
data.AppendElements((const uint8_t*)&dataBuffer, sizeof(uint32_t));
mResult = socket->SendWithAddr(fromAddr, data, &count);
if (mResult == NS_OK && count == sizeof(uint32_t)) {
SUCCEED();
} else {
@ -261,11 +262,12 @@ TEST(TestUDPSocket, TestUDPSocketMain)
// Write data to server
uint32_t count;
const uint32_t data = REQUEST;
nsTArray<uint8_t> data;
const uint32_t dataBuffer = REQUEST;
data.AppendElements((const uint8_t*)&dataBuffer, sizeof(uint32_t));
phase = TEST_OUTPUT_STREAM;
rv = client->Send(NS_LITERAL_CSTRING("127.0.0.1"), serverPort,
(uint8_t*)&data, sizeof(uint32_t), &count);
rv = client->Send(NS_LITERAL_CSTRING("127.0.0.1"), serverPort, data, &count);
ASSERT_TRUE(NS_SUCCEEDED(rv));
EXPECT_EQ(count, sizeof(uint32_t));
@ -284,8 +286,7 @@ TEST(TestUDPSocket, TestUDPSocketMain)
clientAddr.inet.ip = PR_htonl(127 << 24 | 1);
phase = TEST_SEND_API;
rv = server->SendWithAddress(&clientAddr, (uint8_t*)&data, sizeof(uint32_t),
&count);
rv = server->SendWithAddress(&clientAddr, data, &count);
ASSERT_TRUE(NS_SUCCEEDED(rv));
EXPECT_EQ(count, sizeof(uint32_t));
@ -314,8 +315,7 @@ TEST(TestUDPSocket, TestUDPSocketMain)
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
sizeof(uint32_t), &count);
rv = client->SendWithAddress(&multicastAddr, data, &count);
ASSERT_TRUE(NS_SUCCEEDED(rv));
EXPECT_EQ(count, sizeof(uint32_t));
@ -333,8 +333,7 @@ TEST(TestUDPSocket, TestUDPSocketMain)
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
sizeof(uint32_t), &count);
rv = client->SendWithAddress(&multicastAddr, data, &count);
ASSERT_TRUE(NS_SUCCEEDED(rv));
EXPECT_EQ(count, sizeof(uint32_t));
@ -356,8 +355,7 @@ TEST(TestUDPSocket, TestUDPSocketMain)
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
sizeof(uint32_t), &count);
rv = client->SendWithAddress(&multicastAddr, data, &count);
ASSERT_TRUE(NS_SUCCEEDED(rv));
EXPECT_EQ(count, sizeof(uint32_t));
@ -379,8 +377,7 @@ TEST(TestUDPSocket, TestUDPSocketMain)
// Send multicast ping
timerCb->mResult = NS_OK;
timer->InitWithCallback(timerCb, MULTICAST_TIMEOUT, nsITimer::TYPE_ONE_SHOT);
rv = client->SendWithAddress(&multicastAddr, (uint8_t*)&data,
sizeof(uint32_t), &count);
rv = client->SendWithAddress(&multicastAddr, data, &count);
ASSERT_TRUE(NS_SUCCEEDED(rv));
EXPECT_EQ(count, sizeof(uint32_t));

Просмотреть файл

@ -30,7 +30,7 @@ add_test(function test_udp_message_raw_data() {
for (let i = 0; i < HELLO_WORLD.length; i++) {
rawData[i] = HELLO_WORLD.charCodeAt(i);
}
let written = socket.send("127.0.0.1", socket.port, rawData, rawData.length);
let written = socket.send("127.0.0.1", socket.port, rawData);
Assert.equal(written, HELLO_WORLD.length);
});
@ -76,7 +76,7 @@ add_test(function test_udp_message_zero_length() {
});
let rawData = new Uint8Array(EMPTY_MESSAGE.length);
let written = socket.send("127.0.0.1", socket.port, rawData, rawData.length);
let written = socket.send("127.0.0.1", socket.port, rawData);
Assert.equal(written, EMPTY_MESSAGE.length);
});

Просмотреть файл

@ -174,7 +174,7 @@ var SimpleServiceDiscovery = {
let msgData = data.replace("%SEARCH_TARGET%", target);
try {
let msgRaw = converter.convertToByteArray(msgData);
socket.send(SSDP_ADDRESS, SSDP_PORT, msgRaw, msgRaw.length);
socket.send(SSDP_ADDRESS, SSDP_PORT, msgRaw);
} catch (e) {
log("failed to convert to byte array: " + e);
}