6 Replies
      Latest reply on May 16, 2019 1:02 PM by ds;lkfgjsd;lghjld;s
      ds;lkfgjsd;lghjld;s Level 1 Level 1 (0 points)

        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?

        • Re: NWConnection batch receive
          eskimo Apple Staff Apple Staff (12,105 points)

          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"

            • Re: NWConnection batch receive
              ds;lkfgjsd;lghjld;s Level 1 Level 1 (0 points)

              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.

                • Re: NWConnection batch receive
                  eskimo Apple Staff Apple Staff (12,105 points)

                  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"

                    • Re: NWConnection batch receive
                      ds;lkfgjsd;lghjld;s Level 1 Level 1 (0 points)

                      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?