Re: NSMessagePort/NSSocketPort & Distributed Objects [Solved]
Re: NSMessagePort/NSSocketPort & Distributed Objects [Solved]
- Subject: Re: NSMessagePort/NSSocketPort & Distributed Objects [Solved]
- From: James Bucanek <email@hidden>
- Date: Thu, 18 Oct 2007 22:09:44 -0700
James Bucanek <mailto:email@hidden> wrote (Saturday,
September 15, 2007 12:35 PM -0700):
I'm desperately seeking example code that successfully uses either
NSMessagePort or NSSocketPort using a local domain socket to create an
NSConnection.
Shockingly, I never found any code examples which demonstrate
this, and the documentation of NSSocketPort is maddeningly vague.
But it turns out that I was at the Zoo this morning with a
laptop and couple of hours to kill (long story), and managed to
stumble around blindly until I found a solution.
So, for anyone who's interested you can download my
DOConnectionTest project. It creates a DO connection between two
processes using named sockets (files).
<http://www.twilightandbarking.com/FTP/public/Software/Cocoa/DOConnectionTestBSDSocket.zip>
For anyone who's interested in trying this, there are a few
things to note:
sockaddr_un.sun_len must be set to sizeof(struct sockaddr_un)
not SUN_LEN(&address) as you would think. Failing to do this
will cause NSSocketPort to ignore you.
[NSSocketPort
initWithProtocolFamily:socketType:protocol:address:] is what you
need to call from the server. It will create the socket file
(via bind). If the socket file already exists, this call fails
and returns nil.
[NSSocketPort
initRemoteWithProtocolFamily:socketType:protocol:address:] is
what you need to call from the client. It expects to connect
with an already existing the socket. Oddly, it will return a
socket even if the named socket file doesn't exist. I don't know
if, after creating the Port, you eventually create the file the
connection will work or not. I ultimately gated my code so that
it first tests to see if the pipe file exists. This avoids
creating a connection that immediately throws an exception.
NSSocketPort/NSConnection takes care of all of the
bind()/listen()/connect() calls and it adds the port to the run
loop. So don't try to do this yourself or nothing will work.
It appears that NSSocketPort does not, however, destroy the
socket/pipe file once the NSSocketPort is invalidated. You'll
have to do this yourself. Currently, I'm adding code to catch
the port's NSPortDidBecomeInvalidNotification and delete the
socket file.
NSSocketPortNameServer does NOT work with AF_UNIX addresses at
all. There are no usable name server registration services in
this scenario. (The "names" of the connections are being managed
by the file system, so a name server would be superfluous anyway
-- although it would be nice because it would make the code more consistent.)
Hope this helps someone else. And if anyone wants to know why I
went to all of this trouble, I can answer that question next Friday...
James
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden