Considering my usage pattern and the high traffic volume I
expect (millions of packets per second, each around 1500
bytes)
Given your use case, I recommend you go straight to the Dispatch data receive functions, that is:
-
receiveDiscontiguous(minimumIncompleteLength:maximumLength:completion:) -
receiveMessageDiscontiguous(completion:)
These yield DispatchData values, which are the Swift equivalent of dispatch_data_t. You can pass these to C++ which has then access the bytes directly via a pointer; see dispatch_data_apply.
It’s important to note this quote from the doc comments in <dispatch/data.h>:
Each invocation of the block is passed a data object representing the current region and its logical offset, along with the memory location and extent of the region. These allow direct read access to the memory region, but are only valid until the passed-in region object is released.
The upshot of this is that it’s possible for your C++ code to keep the buffer pointer valid indefinitely, as long as it returns the region object.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple