Recently, I've been asking here about creating a NWProtocolFramerImplementation that splits across line breaks. Examples and questions usually involve some glob in a "while true" loop. From reading around, couldn't that function be implemented as:
func handleInput(framer: NWProtocolFramer.Instance) -> Int {
// Pull out objects until an incomplete object is discovered.
var domainData: Data?
while framer.parseInput(
minimumIncompleteLength: 1,
maximumLength: .max,
parse: { buffer, isComplete in
guard let buffer else { return 0 } // Why is `buffer` an Optional?
// Other checks and computations
// Mutate domainData with the result of this iteration.
// Return how many bytes from the buffer are an irrevocable part of domainData.
}
) {
defer { domainData = nil }
guard let domainData else { break }
let domainMessage = //...
let framerMessage = NWProtocolFramer.Message(myMessage: domainMessage)
// The return value is whether or not the delivery was immediate.
_ = framer.deliverInputNoCopy(length: domainData.count, message: framerMessage, isComplete: true)
}
return 0
}
A clear separation of concerns, and makes figuring out what's going on easier.