title: CFSocket data callbacks
body: <p>In an iPhone app, I create a CFSocket object from an existing native UDP socket and set up a data callback whenever the socket receives some data. I then add that to my main program loop:</p>
  
  <pre><code>    //Set socket descriptor field
      cbData.s = udpSocket.getSocketDescriptor();
  
      CFSocketContext udpSocketContext;
      memset(&amp;udpSocketContext, 0, sizeof(udpSocketContext));
      udpSocketContext.info = &amp;cbData;
  
      cbData.socketRef = CFSocketCreateWithNative(NULL, cbData.s, kCFSocketDataCallBack, &amp;getSocketDataCallBack, &amp;udpSocketContext);
      cbData.runLoopSourceRef = CFSocketCreateRunLoopSource( NULL, cbData.socketRef, 0);
      CFRunLoopAddSource(CFRunLoopGetMain(), cbData.runLoopSourceRef, kCFRunLoopCommonModes);
  </code></pre>
  
  <p>I send 1024-byte datagrams over WiFi from a separate Mac server app every 5 mS, and receive them on my iPhone in my <strong>getSocketDataCallBack</strong> routine.</p>
  
  <p>I expect <strong>getSocketDataCallBack</strong> to be called every 5 mS (to match the period of the datagrams being sent from the Mac), which happens the majority of times. BUT, the calls often get delayed by 10s or 100s of mS. Thereafter, I get a rapid sequence of callbacks (fractions of a mS) to retrieve the multiple datagrams that have piled up over that delay. </p>
  
  <p>As iOS obviously keeps the delayed datagrams around, </p>
  
  <ul>
  <li><p>is there any way to grab all the delayed datagrams from the system at
  once instead of <strong>getSocketDataCallBack</strong> being called over and over
  in quick succession? </p>
  
  <p>[I do query how many bytes are available in the callback ala:</p>
  
  <pre><code>       CFDataRef dataRef = (CFDataRef)data;
         numBytesReceived = CFDataGetLength(dataRef);
  </code></pre>
  
  <p>but 'numBytesReceived' is always reported as 1024.]</p></li>
  <li>Alternatively, is there any way to improve/lessen the socket callback
  timing variability through other means?</li>
  </ul>