X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/14a301f7aa7b244c4831006cfc4cfa42185517a4..1d23d1181a753fffef7a9d28db4b0b0546bd0032:/ITInetSocket.m diff --git a/ITInetSocket.m b/ITInetSocket.m index af5d8a2..b61e44f 100755 --- a/ITInetSocket.m +++ b/ITInetSocket.m @@ -30,7 +30,7 @@ ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d]; [browse setDelegate:bd]; - [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil]; + [browse searchForServicesOfType:[NSString stringWithFormat:@"_%@._tcp.",type] inDomain:@""]; } -(id)initWithFD:(int)fd delegate:(id )d @@ -47,7 +47,9 @@ sarr = nil; bufs = 512; actionflag = dieflag = 0; + nc = 0; } + [self spinoffReadLoop]; return self; } @@ -65,6 +67,7 @@ sarr = nil; bufs = 512; actionflag = dieflag = 0; + nc = 1; } return self; } @@ -109,11 +112,14 @@ while (d = [e nextObject]) { struct sockaddr *s = (struct sockaddr*)[d bytes]; + bzero(a,sizeof(struct addrinfo)); a->ai_family = s->sa_family; a->ai_addr = s; a->ai_next = malloc(sizeof(struct addrinfo)); a = a->ai_next; } + ai_cur = ai; + [self realDoConnection]; } } @@ -121,7 +127,6 @@ { NSLog(@"Got a disconnect"); dieflag = 1; - do {} while (dieflag == 1); } -(void)retryConnection @@ -154,6 +159,7 @@ { NSLog(@"writePipe got something"); actionflag = 1; + do {} while (actionflag == 1); NSLog(@"thread saw actionFlag"); } @end @@ -193,7 +199,7 @@ struct addrinfo hints; int err; const char *portNam = [namedPort cString], *hostCStr = [host cString]; - + state = ITInetSocketConnecting; hints.ai_flags = 0; hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -213,7 +219,7 @@ -(void)realDoConnection { - sockfd = socket(ai_cur->ai_addr->sa_family,SOCK_STREAM,IPPROTO_TCP); + sockfd = socket(ai_cur->ai_family,SOCK_STREAM,IPPROTO_TCP); [self spinoffReadLoop]; } @@ -235,6 +241,7 @@ unsigned long readLen = 0; signed int err; [readPipe setDelegate:dp]; + if (nc){ NSLog(@"Connecting"); err = connect(sockfd,ai_cur->ai_addr,ai_cur->ai_addrlen); if (err == -1) @@ -243,14 +250,15 @@ [(id)dp errorOccured:ITInetCouldNotConnect during:ITInetSocketConnecting onSocket:self]; goto dieaction; } + } NSLog(@"Sending finishedConnecting"); [(id)dp finishedConnecting:self]; lstart: - + state = ITInetSocketListening; while (!actionflag && !dieflag) { - NSData *d; readLen = recv(sockfd,buf,bufs,0); + state = ITInetSocketReading; if (readLen == -1) {[(id)dp errorOccured:ITInetConnectionDropped during:ITInetSocketReading onSocket:self];goto dieaction;} if (readLen) { NSLog(@"recv'd"); @@ -260,12 +268,13 @@ lstart: ap = [[NSAutoreleasePool alloc] init]; } } - + state = ITInetSocketListening; actionflag = 0; if (dieflag) { dieaction: + state = ITInetSocketDisconnected; perror("Awh"); free(buf); shutdown(sockfd,2); @@ -277,6 +286,7 @@ dieaction: } { + state = ITInetSocketWriting; NSLog(@"Emptying writePipe"); NSData *d = [writePipe readAllData]; write(sockfd,[d bytes],[d length]);