Search code examples
iphoneioshttpnetwork-programmingcfnetwork

How do I setup CFReadStreamRead to timeout in iOS?


I have legacy code using CFReadStreamRead on the iOS, but if there is no/loss of a connection CFReadStreamRead will block forever. How do I setup CFReadStreamRead to timeout?

Thanks in advance


Solution

  • I ended up using a home grown timeout. The code was something like this:

    uint64_t start = get_current_time();
    
    while(TRUE) {
         if(CFReadStreamHasBytesAvailable(stream) == TRUE) {
              while ((bytes_read = CFReadStreamRead(stream, buffer, read_size)) > 0) {
                   // do work!
              }
              start = get_current_time();
         }
         uint64_t elapsed = get_current_time() - start;
         if(elapsed > timeout) {
              break;
         }
         sleep(10);
    }