gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Modern" server socket programming?


From: Ivan Vučica
Subject: Re: "Modern" server socket programming?
Date: Tue, 8 Jan 2013 22:33:02 +0100

8. 1. 2013., u 18:19, Marcus Müller <address@hidden> je napisao:


I don't understand what you mean by "connected to a notification".

David is, if I understand correctly, referring to using methods such as -[NSFileHandle acceptConnectionInBackgroundAndNotify] after creating the file handle with -[NSFileHandle initWithFileDescriptor:closeOnDealloc:].

Once one of these observation methods is used, the method actually schedules observing for file events on the runloop, and upon noticing an event that you scheduled observing for, sends a notification through NSNotificationCenter.

For example, having created a socket...
    NSSocketPort* serverSock = [[NSSocketPort alloc] initWithTCPPort: 1234];
...wrap its file handle into a file handle...
    socketHandle = [[NSFileHandle alloc] initWithFileDescriptor: [serverSock socket]
                                                 closeOnDealloc: NO];
...begin observing for notifications...
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(newConnection:) 
                                                 name: NSFileHandleConnectionAcceptedNotification
                                               object: socketHandle];
... and finally schedule observing the event (and dispatching the notifications):
    [socketHandle acceptConnectionInBackgroundAndNotify];

Taken from this (broken) piece of code:
The relevant piece of code that I described does not seem broken, and the general idea seems to be sound. If the relevant code is fundamentally broken, someone please shame me. I didn't use this API before.

Looking at NSFileHandle documentation will probably be the best course of action.


This is easy from anything that allows you to get the underlying file descriptor for the socket, and if it's already encapsulated in an object then you can either use associative references or just create an NSDictionary indexed by the file handles.

if I understand you correctly, you're describing the way how to get back from the native file handle encapsulated in a notification (userInfo) towards my original object?

No, David is just talking about the fact that, if you already have access to a file handle for a socket (that is, the return value of the socket() call) in whatever underlying library you use or if you use the BSD sockets directly, you can use NSFileHandle quite easily.


What I don't understand: how exactly do you schedule an "NSFileHandle connected to a notification" in a NSRunLoop?


Via -[NSFileHandle acceptConnectionInBackgroundAndNotify] and other similar ...InBackgroundAndNotify methods.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]