0

I am creating a server which must run as a privileged helper tool on OS X. In it, I am attempting to use NSSocketPort, NSFileHandle, and NSNotificationCenter to perform asynchronous IO on a listening socket. However, NSNotificationCenter refuses to call the notification handler. Here is the relevant code:

NSSocketPort* sock = [[NSSocketPort alloc] initWithTCPPort:8080];
NSFileHandle* listener = [[NSFileHandle alloc] initWithFileDescriptor:sock.socket closeOnDealloc:NO]
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onConnection:) name:NSFileHandleConnectionAcceptedNotification object:nil]

My main thread is running [NSRunLoop.mainRunLoop run], so that is not the issue. I have tried setting 'object' to 'listener' in the addObserver: call, but that made no difference.

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
John Colanduoni
  • 305
  • 2
  • 8

1 Answers1

1

Try registering as observer before creating your NSFileHandle instance. If the notification is send during the init method, you're registered too late. (I don't have specific experience with NSFileHandle, so this is just a hunch for a possible fix.)

Johan Kool
  • 15,637
  • 8
  • 64
  • 81