From: Alexander Strange Date: Sun, 23 Mar 2003 20:35:36 +0000 (+0000) Subject: Give me a B! Give me a U! Give me a G! X-Git-Tag: v0.1~28 X-Git-Url: http://git.ithinksw.org/ITFoundation.git/commitdiff_plain/af1fcfcf8c78f03127d3929b49330cc191259487 Give me a B! Give me a U! Give me a G! --- diff --git a/ITByteStream.h b/ITByteStream.h index 52f4856..a1053f3 100755 --- a/ITByteStream.h +++ b/ITByteStream.h @@ -10,7 +10,7 @@ @class ITByteStream; -@protocol ITByteStreamDelegate +@protocol ITByteStreamDelegate -(oneway void)newDataAdded:(ITByteStream *)sender; @end @@ -29,4 +29,7 @@ -(NSData*) readAllData; -(void) writeData:(in NSData*)data; -(void) writeBytes:(char *)b len:(long)length; +-(void) lockStream; +-(void) unlockStream; +-(void) shortenData:(long)length; @end diff --git a/ITByteStream.m b/ITByteStream.m index 67ecf31..b5554e1 100755 --- a/ITByteStream.m +++ b/ITByteStream.m @@ -70,17 +70,11 @@ -(NSData*) readDataOfLength:(int)length { - NSData *ret, *tmp; + NSData *ret; NSRange range = {0, length}; [lock lock]; ret = [data subdataWithRange:range]; -#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED [data replaceBytesInRange:range withBytes:nil length:0]; -#else - range = {length, [data length]}; - tmp = [data subdataWithRange:range]; - [data setData:tmp]; -#endif [lock unlock]; return ret; } @@ -110,4 +104,20 @@ [lock unlock]; [delegate newDataAdded:self]; } + +-(void) lockStream +{ + [lock lock]; +} + +-(void) unlockStream +{ + [lock unlock]; +} + +-(void) shortenData:(long)length +{ + NSRange range = {0, length}; + [data replaceBytesInRange:range withBytes:nil length:0]; +} @end diff --git a/ITInetServerSocket.h b/ITInetServerSocket.h index 5f8a479..56b6bb6 100755 --- a/ITInetServerSocket.h +++ b/ITInetServerSocket.h @@ -11,7 +11,7 @@ @class ITInetSocket; @protocol ITInetServerSocketOwner -- (void)newClientJoined:(ITInetSocket*)client; +- (oneway void)newClientJoined:(ITInetSocket*)client; @end @interface ITInetServerSocket : NSObject { diff --git a/ITInetServerSocket.m b/ITInetServerSocket.m index 08a8fe0..7d4a8f0 100755 --- a/ITInetServerSocket.m +++ b/ITInetServerSocket.m @@ -18,11 +18,8 @@ #import /* 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; @@ -35,12 +32,6 @@ static NSMutableSet *servsockets; @end @implementation ITInetServerSocket -+ (void)initialize -{ - servsockets = [[NSMutableSet alloc] init]; - //[self setupTimer]; -} - - (id)init { if (self = [super init]) @@ -85,7 +76,7 @@ static NSMutableSet *servsockets; - (BOOL)start { if (!rndName || !rndType || !port) return NO; - [ITInetServerSocket registerSocket:self]; + [self setupConnection]; return YES; } @@ -111,7 +102,7 @@ static NSMutableSet *servsockets; - (void)stop { - [ITInetServerSocket unregisterSocket:self]; + [self stopConnection]; } - (void)setServiceType:(NSString*)type useForPort:(BOOL)p @@ -134,18 +125,6 @@ static NSMutableSet *servsockets; @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]; @@ -167,6 +146,7 @@ static NSMutableSet *servsockets; 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); @@ -178,7 +158,7 @@ static NSMutableSet *servsockets; { [self stopRendezvousAdvertising]; [clients makeObjectsPerformSelector:@selector(disconnect)]; - shutdown(sockfd,2); + [self stopThread]; close(sockfd); sockfd = -1; } @@ -210,12 +190,15 @@ static NSMutableSet *servsockets; - (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]; } @@ -225,13 +208,17 @@ static NSMutableSet *servsockets; 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]; diff --git a/ITInetSocket.h b/ITInetSocket.h index 61d16b0..1613c69 100755 --- a/ITInetSocket.h +++ b/ITInetSocket.h @@ -55,13 +55,6 @@ typedef enum { * @discussion ITInetSockets use these methods to communicate with their delegates */ @protocol ITInetSocketDelegate -/*! - * @method dataReceived: - * @abstract Alerts the delegate of data. - * @discussion The delegate should check [sender readPipe] to get the data. - * @param sender The socket that the messages came from. - */ -- (oneway void) dataReceived:(ITInetSocket *)sender; /*! * @method errorOccured:during:onSocket: * @abstract Alerts the delegate of an error condition. @@ -86,14 +79,13 @@ typedef enum { * @discussion ITInetSocket is an Internet socket class supporting IPv6 and Rendezvous. */ @interface ITInetSocket : NSObject { - int sockfd; int port; int nc; unsigned short bufs; volatile int dieflag; volatile int actionflag; - id delegate; + id delegate; struct addrinfo *ai, *ai_cur; ITByteStream *readPipe, *writePipe; ITInetSocketState state; @@ -106,7 +98,7 @@ typedef enum { * @param type The type of Rendezvous service to listen on. * @param d The delegate that the sockets will belong to. */ -+(void)startAutoconnectingToService:(NSString*)type delegate:(id )d; ++(void)startAutoconnectingToService:(NSString*)type delegate:(id )d; /*! * @method initWithFD:delegate: * @abstract Wraps a socket around an existing socket descriptor. @@ -114,14 +106,14 @@ typedef enum { * @param fd The descriptor. * @param d The delegate for the socket. */ --(id) initWithFD:(int)fd delegate:(id )d; +-(id) initWithFD:(int)fd delegate:(id )d; /*! * @method initWithDelegate: * @abstract Creates a new socket. * @discussion The socket will not be connected to anything. * @param d The delegate of the socket. */ --(id) initWithDelegate:(id )d; +-(id) initWithDelegate:(id )d; -(id )delegate; -(unsigned short)bufferSize; @@ -132,4 +124,6 @@ typedef enum { -(ITInetSocketState) state; -(void) retryConnection; -(void) disconnect; +-(ITByteStream *)readPipe; +-(ITByteStream *)writePipe; @end diff --git a/ITInetSocket.m b/ITInetSocket.m index b61e44f..b7441cb 100755 --- a/ITInetSocket.m +++ b/ITInetSocket.m @@ -106,9 +106,11 @@ { NSEnumerator *e = [arr objectEnumerator]; NSData *d; - struct addrinfo *a; + struct addrinfo *a,*oa; ai = malloc(sizeof(struct addrinfo)); - a = ai; + ai_cur = ai; + oa = a = ai; + bzero(a,sizeof(struct addrinfo)); while (d = [e nextObject]) { struct sockaddr *s = (struct sockaddr*)[d bytes]; @@ -116,9 +118,13 @@ a->ai_family = s->sa_family; a->ai_addr = s; a->ai_next = malloc(sizeof(struct addrinfo)); + oa = a; a = a->ai_next; } - ai_cur = ai; + free(a); + oa->ai_next = NULL; + NSLog(@"Sockaddr connecting...."); + [self dumpv6Addrinfo:ai]; [self realDoConnection]; } } @@ -127,6 +133,7 @@ { NSLog(@"Got a disconnect"); dieflag = 1; + do {} while (dieflag == 1); } -(void)retryConnection @@ -157,11 +164,10 @@ -(void)newDataAdded:(ITByteStream*)sender { - NSLog(@"writePipe got something"); - actionflag = 1; - do {} while (actionflag == 1); - NSLog(@"thread saw actionFlag"); } + +-(ITByteStream*)readPipe {return readPipe;} +-(ITByteStream*)writePipe {return writePipe;} @end @implementation ITInetSocket(Debugging) @@ -238,15 +244,16 @@ NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]]; NSProxy *dp = [[dcon rootProxy] retain]; char *buf = malloc(bufs); - unsigned long readLen = 0; + unsigned long readLen = 0,wpl = 0; signed int err; - [readPipe setDelegate:dp]; + [readPipe setDelegate:(id )dp]; if (nc){ NSLog(@"Connecting"); err = connect(sockfd,ai_cur->ai_addr,ai_cur->ai_addrlen); if (err == -1) { perror("CAwh"); + *((char*)NULL) = 12; [(id)dp errorOccured:ITInetCouldNotConnect during:ITInetSocketConnecting onSocket:self]; goto dieaction; } @@ -255,7 +262,7 @@ [(id)dp finishedConnecting:self]; lstart: state = ITInetSocketListening; - while (!actionflag && !dieflag) + while (!actionflag && !dieflag && !(wpl = CFDataGetLength((CFDataRef)writePipe->data))) { readLen = recv(sockfd,buf,bufs,0); state = ITInetSocketReading; @@ -286,10 +293,13 @@ dieaction: } { + const char *d = CFDataGetBytePtr((CFDataRef)writePipe->data); state = ITInetSocketWriting; - NSLog(@"Emptying writePipe"); - NSData *d = [writePipe readAllData]; - write(sockfd,[d bytes],[d length]); + NSLog(@"Writing"); + [writePipe lockStream]; + wpl = send(sockfd,d,wpl,0); + [writePipe shortenData:wpl]; + [writePipe unlockStream]; [ap release]; ap = [[NSAutoreleasePool alloc] init]; goto lstart; diff --git a/ITServiceBrowserDelegate.m b/ITServiceBrowserDelegate.m index 31dde47..6d368c6 100755 --- a/ITServiceBrowserDelegate.m +++ b/ITServiceBrowserDelegate.m @@ -15,23 +15,36 @@ { if (self = [super init]) { - delegate = _delegate; + delegate = [_delegate retain]; } return self; } +- (void) dealloc +{ + [delegate release]; +} + - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing { ITInetSocket *sock; + NSArray *arr; + id d = delegate; if (!moreComing) { + NSLog(@"Nothing more coming"); + [[aNetService retain] autorelease]; [aNetServiceBrowser stop]; [aNetServiceBrowser release]; [self release]; } - sock = [[ITInetSocket alloc] initWithDelegate:delegate]; + sock = [[ITInetSocket alloc] initWithDelegate:d]; NSLog(@"Detected a service! name %@ type %@",[aNetService name],[aNetService type]); - [sock connectWithSockaddrArray:[aNetService addresses]]; + arr = [aNetService addresses]; + if ([arr count]) + [sock connectWithSockaddrArray:arr]; + else + NSLog(@"There are no sockaddrs for this service!"); } @end diff --git a/ShowcaseController.m b/ShowcaseController.m index d121bb6..a9aff62 100755 --- a/ShowcaseController.m +++ b/ShowcaseController.m @@ -24,17 +24,13 @@ } - (void) finishedConnecting:(ITInetSocket *)sender { - [sender disconnect]; + [[sender writePipe] writeBytes:"suck" len:4]; } - (void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(in ITInetSocket*)sender { } -- (void) dataReceived:(ITInetSocket *)sender -{ -} - - (void) newDataAdded:(ITByteStream*)sender { @@ -42,6 +38,6 @@ - (void)newClientJoined:(ITInetSocket*)client { - + [[client writePipe] writeBytes:"suck" len:4]; } @end