NWConnection batch receive

I am using the NWConnection in UDP mode, however it is considerably slower than NWUDPSession when used with NEPacketTunnelProvider.


The reason is that while NWUDPSession will pass an array of datagrams to the callback function, that allows for a fast batch processing of the entire array and then batch write into the packetFlow.


The callback of NWConnection will only pass a single datagram, which means we have to process one packet at a time, and then write one packet into the packetFlow. The result is 2 times slower, whereas NWConnection is supposed to be faster.


Is there an efficient way to aggregate several callbacks and get a single array of packets, without expensive syncrhonization in user space?

Replies

NWConnection
has a
batch(_:)
method that calls the supplied closure and batches any send and receive calls you make within that closure. It’s pretty obvious how this works on the send side, but less so on the receive side. I don’t know if there’s an easy way to go from this semantics to the semantics you’re used to from
NWUDPSession
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes the batch thing seem helpful for send. Doesn't help us with receiveMessage. In fact the docs says it only works with send/receive.


> Calls other than send and receive will not be affected.


Does it even work with receiveMessage? If yes it should be explicitly mentioned.

Does it even work with

receiveMessage(…)
?

Even if it did, it’s hard to see how you’d assemble the semantics you want from the batch semantics provided.

I was curious about this issue so I had a quick chat with Network framework engineering about it. It seems that, as things currently stand, there isn’t a great way to achieve what you want. We’re hoping to improve that in the future but, for the moment, if

NWUDPSession
is working well for you, it’s fine to stick with that. It’s a very thin layer over
NWConnection
(well
nw_connection_t
) anyway.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

NWUDPSession work fine in terms of performance. But statring with iOS 12.2 we run into unrecoverable code 55 errors


> The operation couldn’t be completed. No buffer space available (code 55)


Once that error is triggered, every read and write fail with the same error. I am trying to distill that into a minimal sample code to open a bug report, but maybe you are already familiar with that problem?

But statring with iOS 12.2 we run into unrecoverable code 55 errors

Hmmm, that’s not good.

maybe you are already familiar with that problem?

No, I’ve not heard of that before. Did this break your shipping product? If so, you should file a bug about this even if you can’t reproduce it reliably. A bug report with a sysdiagnose log (per our Bug Reporting > Profiles and Logs) taken shortly after you see the problem is still going to be helpful.

If you do file a bug, please post the bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I filed a bug: 50864206


Thanks