title: CFReadStreamSetClient Callback function - How do I give the C callback function a reference to the containing class?
body: <p>I am assing a client callback function on a network read stream below:  </p>
  
  <pre><code> -(void)connectToServer
  {
      if([self isServerConfigured])
      {
          CFReadStreamRef readStream; 
          CFWriteStreamRef writeStream;
          CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)self.serverAddress);
          CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host, self.port, &amp;readStream, &amp;writeStream);
  
          CFStreamClientContext myContext = {
              0,
              self,
              (void *(*)(void *info))CFRetain,
              (void (*)(void *info))CFRelease,
              (CFStringRef (*)(void *info))CFCopyDescription
          };
  
          CFOptionFlags registeredEvents = kCFStreamEventHasBytesAvailable |
          kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered;
  
          if(CFReadStreamSetClient(readStream, registeredEvents, serverCB, &amp;myContext))
          {
              CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
          }
          if(!CFReadStreamOpen(readStream)){
              NSLog(@"Could not open read stream");
          }
      }
  }
  </code></pre>
  
  <p>I would like this callback function to be able to call instance functions in the current object (self). It does not appear that CFReadStreamSetClient will allow me to change the callback functions signature and so I cannot just pass a reference to the current object to the callback I don't think. </p>
  
  <p>This is the callback function:</p>
  
  <pre><code> void serverCB(CFReadStreamRef stream, CFStreamEventType event, void *myPtr)
  {
      switch(event) {
          case kCFStreamEventHasBytesAvailable:
              [self readStreamData:stream];
              break;
          case kCFStreamEventErrorOccurred:
              NSLog(@"A Read Stream Error Has Occurred!");
              break;
          case kCFStreamEventEndEncountered:
              NSLog(@"A Read Stream Event End!");
              break;
          default:
              break;
      }
  }
  </code></pre>
  
  <p>ServerCB will not know what 'self' is in this context. Can someone give me a pointer on how I can work around this?</p>
  
  <p>Thanks!</p>