- struct sockaddr_in csa;
- int csalen;
- signed int cfd;
- (void) timer;
- cfd = accept(sockfd,(struct sockaddr*)&csa,&csalen);
- if (cfd == -1) {
- if (errno == EWOULDBLOCK) return;
- else {perror("Too bad I haven't implemented error checking yet");}
- }
- else {
- ITInetSocket *csocket = [[[ITInetSocket alloc] initWithFD:cfd delegate:self] autorelease];
- [clients addObject:csocket];
- [delegate newClientJoined:csocket];
-
+ NSPort *p1 = [NSPort port], *p2 = [NSPort port];
+ 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
+{
+ dieflag = 1;
+}
+
+- (void)newClient:(int)cfd
+{
+ ITInetSocket *csocket = [[ITInetSocket alloc] initWithFD:cfd delegate:delegate];
+ [clients addObject:csocket];
+}
+
+- (void)socketAcceptLoop:(id)data
+{
+ NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
+ NSArray *par = data;
+ NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[par objectAtIndex:0] sendPort:[par objectAtIndex:1]];
+ NSProxy *dp = [dcon rootProxy];
+ while ((sockfd != -1) && !dieflag)
+ {
+ signed int cfd;
+ cfd = accept(sockfd,NULL,NULL);
+ NSLog(@"Someone connected!");
+ [(id)dp newClient:cfd];