Re: Acquiring an NSConnection otherwise than by registered name?
Re: Acquiring an NSConnection otherwise than by registered name?
- Subject: Re: Acquiring an NSConnection otherwise than by registered name?
- From: Oleg Krupnov <email@hidden>
- Date: Fri, 1 Oct 2010 09:47:18 +0300
Hi Ken,
I've tried sockets instead of mach ports, but the result is almost the
same. At first, the request via the new connection was handled in the
new server thread, which rejoiced me, but then the second request to
the same connection from the same client thread was handled in the
main server thread again. The threads seem to be picked arbitrary as
well as in the case of mach ports :(
I have a strange idea, but if it would be possible, it could solve the
problem. Can I, hem, just launch a new server thread and create
another connection point, and register it under a different name, and
vend another root object through it (different instance of different
class) and then launch a run loop in that thread?
Here is the code:
- (void)threadMain
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSConnection* connectionPoint = [[NSConnection new] autorelease];
NSProtocolChecker* vendedThreadObject = [NSProtocolChecker
protocolCheckerWithTarget:self
protocol:@protocol(ThreadObjectProtocol)];
[connectionPoint setRootObject:vendedThreadObject];
if ([connectionPoint registerName:@"anotherName02"])
{
while (!isTerminated)
{
[[NSRunLoop currentRunLoop] run];
}
}
[pool release];
}
However, the line "registerName" returns NO. What is the problem?
Thanks,
Oleg.
On Thu, Sep 30, 2010 at 7:45 PM, Ken Thomases <email@hidden> wrote:
> On Sep 30, 2010, at 11:17 AM, Oleg Krupnov wrote:
>
>> NSSocketPort* port = [[NSSocketPort alloc] initWithTCPPort:1234];
>> NSConnection* connectionPoint = [[NSConnection alloc]
>> initWithReceivePort:port sendPort:nil];
>> if ([connectionPoint registerName:CONNECTION_NAME
>> withNameServer:[NSSocketPortNameServer sharedInstance]])
>>
>> This time it registered, but on client side I get nil when asking for proxy:
>>
>> proxy = [NSConnection
>> rootProxyForConnectionWithRegisteredName:CONNECTION_NAME host:nil];
>>
>> I get nil. I tried "localhost" and "128.0.0.1" for host, but still no luck.
>
> Try +rootProxyForConnectionWithRegisteredName:host:usingNameServer: and specify the socket port name server (similar to what you did, above).
>
>> There must be something simple I've overlooked? The docs do not tell
>> how to set up sockets easily, only Mach ports come by default...
>
> There are multiple approaches. You can use the initializers of NSSocketPort to create socket ports. You can use one of the -portForName:... methods of NSSocketPortNameServer to obtain a socket port for communicating with the named service.
>
> Once you have the socket ports, you can create NSConnection objects from them, as you did above.
>
> Cheers,
> Ken
>
>
_______________________________________________
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