#import <fcntl.h>
#import <unistd.h>
-/* Too bad Objective-C doesn't have class variables... */
-static NSMutableSet *servsockets;
-
@interface ITInetServerSocket(Private)
-+(void)registerSocket:(ITInetServerSocket*)sock;
-+(void)unregisterSocket:(ITInetServerSocket*)sock;
-(short)lookupPortForServiceType:(NSString*)name;
-(void)setupConnection;
-(void)stopConnection;
@end
@implementation ITInetServerSocket
-+ (void)initialize
-{
- servsockets = [[NSMutableSet alloc] init];
- //[self setupTimer];
-}
-
- (id)init
{
if (self = [super init])
- (BOOL)start
{
if (!rndName || !rndType || !port) return NO;
- [ITInetServerSocket registerSocket:self];
+ [self setupConnection];
return YES;
}
- (void)stop
{
- [ITInetServerSocket unregisterSocket:self];
+ [self stopConnection];
}
- (void)setServiceType:(NSString*)type useForPort:(BOOL)p
@end
@implementation ITInetServerSocket(Private)
-+(void)registerSocket:(ITInetServerSocket*)sock
-{
- [sock setupConnection];
- [servsockets addObject:sock];
-}
-
-+(void)unregisterSocket:(ITInetServerSocket*)sock
-{
- [sock stopConnection];
- [servsockets removeObject:sock];
-}
-
-(short)lookupPortForServiceType:(NSString*)name
{
const char *_name = [name cString];
hints.ai_addrlen = 0;
hints.ai_canonname = hints.ai_addr = hints.ai_next = NULL;
getaddrinfo(NULL,[[[NSNumber numberWithShort:port] stringValue] cString],&hints,&ai);
+ sockfd = socket(PF_INET6,SOCK_STREAM,IPPROTO_TCP);
bind(sockfd,ai->ai_addr,ai->ai_addrlen);
listen(sockfd, SOMAXCONN);
freeaddrinfo(ai);
{
[self stopRendezvousAdvertising];
[clients makeObjectsPerformSelector:@selector(disconnect)];
- shutdown(sockfd,2);
+ [self stopThread];
close(sockfd);
sockfd = -1;
}
- (void)setupRendezvousAdvertising
{
- service = [[NSNetService alloc] initWithDomain:@"" type:[NSString stringWithFormat:@"_%@._tcp.",rndType] name:rndName port:htons(port)];
+ NSString *a = [NSString stringWithFormat:@"_%@._tcp.",rndType];
+ service = [[NSNetService alloc] initWithDomain:@"" type:a name:rndName port:htons(port)];
+ NSLog(@"Advertising a service of type %@ name %@",a,rndName);
[service publish];
}
NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:p1 sendPort:p2];
NSArray *par = [NSArray arrayWithObjects:p2,p1,nil];
[dcon setRootObject:self];
+ NSLog(@"detached server thread");
[NSThread detachNewThreadSelector:@selector(socketAcceptLoop:) toTarget:self withObject:par];
}
- (void)stopThread
{
+ NSLog(@"stopping server thread");
dieflag = 1;
+ do {} while (dieflag == 1);
}
- (void)newClient:(int)cfd
{
ITInetSocket *csocket = [[ITInetSocket alloc] initWithFD:cfd delegate:delegate];
+ NSLog(@"new client for this server");
[clients addObject:csocket];
}
NSArray *par = data;
NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[par objectAtIndex:0] sendPort:[par objectAtIndex:1]];
NSProxy *dp = [dcon rootProxy];
- while ((sockfd != -1) && !dieflag)
+ while ((sockfd >= 0) && !dieflag)
{
signed int cfd;
cfd = accept(sockfd,NULL,NULL);
+ NSLog(@"Someone connected!");
[(id)dp newClient:cfd];
+ [ap release];
+ ap = [[NSAutoreleasePool alloc] init];
}
+
+ NSLog(@"suiciding");
dieflag = 0;
[dcon release];
[ap release];