Dabbling with Network.framework to see if I can replace some old socket based code on iOS. I was able to establish a TCP connection and send/receive data but I'm puzzled as to how receive timeouts should be handled: I can code my own timeouts using GCD but there doesn't seem to be a way to actually cancel the pending receive (it does seem to timeout by itself after a minute or so, tearing down the NWConnection with it - definitely not what I'm looking for).
Any suggestions?
timeouts just mean the real Modbus device isn't responding.
Unless your user walks into a lift (per the example in my 31 Jul post), right?
Regardless, emulating your current approach using Network framework is still feasible. The basic idea would be for the receive side of your code to run continuously, that is, on open it would start a receive and then, when that receive completes, it would start another receive. When it receives a response, it would check to see if there’s someone waiting for that response. If so, it delivers the response to that waiter. Finally, the sender side of your code would register its interest with the receive side, do the send, and then wait for the response. From there, one of two things happens:
If the wait times out, it deregisters its interest and returns an error.
Otherwise, it deregisters its interest and then returns that response.
Sorting out all the details would be tricky [1] but it should work.
Then again, if you’re going to go to all this trouble it might be better spending your time switching to UDP, which better reflects the reality of your setup.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] Especially the locking, because the send and receive sides must necessarily run on separate threads.