From 1d23d1181a753fffef7a9d28db4b0b0546bd0032 Mon Sep 17 00:00:00 2001 From: Alexander Strange Date: Mon, 17 Mar 2003 07:21:55 +0000 Subject: [PATCH] Sockets don't quite work, but they will almost work. --- ITInetServerSocket.m | 6 +++++- ITInetSocket.h | 7 ++----- ITInetSocket.m | 10 ++++++++-- ITServiceBrowserDelegate.m | 2 ++ ShowcaseController.h | 5 +++-- ShowcaseController.m | 33 ++++++++++++++++++++------------- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/ITInetServerSocket.m b/ITInetServerSocket.m index 61791f8..08a8fe0 100755 --- a/ITInetServerSocket.m +++ b/ITInetServerSocket.m @@ -185,7 +185,9 @@ static NSMutableSet *servsockets; - (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]; } @@ -202,6 +204,7 @@ static NSMutableSet *servsockets; 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]; } @@ -226,6 +229,7 @@ static NSMutableSet *servsockets; { signed int cfd; cfd = accept(sockfd,NULL,NULL); + NSLog(@"Someone connected!"); [(id)dp newClient:cfd]; } dieflag = 0; diff --git a/ITInetSocket.h b/ITInetSocket.h index 3a09aab..61d16b0 100755 --- a/ITInetSocket.h +++ b/ITInetSocket.h @@ -86,13 +86,10 @@ typedef enum { * @discussion ITInetSocket is an Internet socket class supporting IPv6 and Rendezvous. */ @interface ITInetSocket : NSObject { - @public - /*! - * @var sockfd - * @abstract KLWONZ - */ + int sockfd; int port; + int nc; unsigned short bufs; volatile int dieflag; volatile int actionflag; diff --git a/ITInetSocket.m b/ITInetSocket.m index decfd43..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,6 +112,7 @@ 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)); @@ -215,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]; } @@ -237,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) @@ -245,6 +250,7 @@ [(id)dp errorOccured:ITInetCouldNotConnect during:ITInetSocketConnecting onSocket:self]; goto dieaction; } + } NSLog(@"Sending finishedConnecting"); [(id)dp finishedConnecting:self]; lstart: diff --git a/ITServiceBrowserDelegate.m b/ITServiceBrowserDelegate.m index 0dfcce8..31dde47 100755 --- a/ITServiceBrowserDelegate.m +++ b/ITServiceBrowserDelegate.m @@ -27,8 +27,10 @@ { [aNetServiceBrowser stop]; [aNetServiceBrowser release]; + [self release]; } sock = [[ITInetSocket alloc] initWithDelegate:delegate]; + NSLog(@"Detected a service! name %@ type %@",[aNetService name],[aNetService type]); [sock connectWithSockaddrArray:[aNetService addresses]]; } diff --git a/ShowcaseController.h b/ShowcaseController.h index 8b4aef8..516094b 100755 --- a/ShowcaseController.h +++ b/ShowcaseController.h @@ -8,9 +8,10 @@ #import #import +#import -@interface ShowcaseController : NSObject { - +@interface ShowcaseController : NSObject { + ITInetServerSocket *server; } @end diff --git a/ShowcaseController.m b/ShowcaseController.m index 5a775c7..d121bb6 100755 --- a/ShowcaseController.m +++ b/ShowcaseController.m @@ -8,33 +8,40 @@ #import "ShowcaseController.h" #import "ITInetSocket.h" +#import +#import -ITInetSocket *sock; @implementation ShowcaseController - (void)awakeFromNib { - sock = [[ITInetSocket alloc] initWithDelegate:self]; - NSLog(@"rawr?"); - [sock connectToHost:@"irc.freenode.net" onPort:6667]; + server = [[ITInetServerSocket alloc] initWithDelegate:self]; + [server setPort:1338]; + [server setServiceType:@"ittest" useForPort:NO]; + [server setServiceName:[(NSString*)SCDynamicStoreCopyComputerName(NULL,NULL) autorelease]]; + [server start]; + [ITInetSocket startAutoconnectingToService:@"ittest" delegate:self]; } - (void) finishedConnecting:(ITInetSocket *)sender { + [sender disconnect]; +} +- (void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(in ITInetSocket*)sender +{ } -- (void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(in ITInetSocket*)sender {NSLog(@"wtf");[sender retryConnection];} - (void) dataReceived:(ITInetSocket *)sender { } -- (void) newDataAdded:(ITByteStream*)sender { - static int firstTime = YES; - NSString *ircini = @"USER m0nk3ys . . :Not Telling\r\nNICK ITFTest\r\n", *irc2 = @"JOIN #iThink\r\nPRIVMSG #iThink :w00t\r\nQUIT :!\r\n"; - NSLog(@"Writing something"); - NSData *d = [NSData dataWithBytes:[firstTime?ircini:irc2 cString] length:[firstTime?ircini:irc2 length]]; - [sock->writePipe writeData:d]; - NSLog(@"Reading something"); - firstTime = NO; +- (void) newDataAdded:(ITByteStream*)sender +{ + +} + +- (void)newClientJoined:(ITInetSocket*)client +{ + } @end -- 2.20.1