I am writing an application that should communicate with a device connected via USB. The application sends and receives data in turn from a device with a fixed time. All Rx / Tx occur in a separate thread, because otherwise the user interface will be blocked. The basic structure looks basically like this. (auto-detection pools and missing things)
-(void)comThread:(id)arg {
while(state == kIsConnected) {
[runLoop runUntilDate:[NSDate distantFuture]];
if(rxTxState == kRx) {
rxTxState = kTx;
}
if(rxTxState == kTx) {
rxTimeoutTimer = [NSTimer scheduledTimer....];
}
}
}
After sending data, the application expects to receive data or rxTimeoutTimerto start, which leads to a retransmission of the packet. The rx operation works because the base layers use asynchronous system calls and invoke the rx handler, which looks mostly like this.
-(void)receiveData:(NSData*)data{
[rxQueue addObject:data];
[rxTimeoutTimer invalidate];
}
() [runLoop runUntilDate:] receiveData:? Apple , RunLoop. - performSelector:onThread:..., , .
.