Give me a B! Give me a U! Give me a G!
[ITFoundation.git] / ITInetSocket.m
index c51c05a..b7441cb 100755 (executable)
@@ -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 <ITInetSocketDelegate,NSObject>)d
           sockfd = fd;
           delegate = [d retain];
           port = 0;
-          writePipe = [[ITByteStream alloc] init];
-          readPipe = [[ITByteStream alloc] init];
+          writePipe = [[ITByteStream alloc] initWithDelegate:self];
+          readPipe = [[ITByteStream alloc] initWithDelegate:d];
           ai = nil;
           sarr = nil;
           bufs = 512;
           actionflag = dieflag = 0;
+          nc = 0;
           }
+    [self spinoffReadLoop];
     return self;
 }
 
           sockfd = -1;
           delegate = [d retain];
           port = 0;
-          writePipe = [[ITByteStream alloc] init];
-          readPipe = [[ITByteStream alloc] init];
+          writePipe = [[ITByteStream alloc] initWithDelegate:self];
+          readPipe = [[ITByteStream alloc] initWithDelegate:d];
           ai = nil;
           sarr = nil;
           bufs = 512;
           actionflag = dieflag = 0;
+          nc = 1;
           }
     return self;
 }
           {
           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];
+                 bzero(a,sizeof(struct addrinfo));
                  a->ai_family = s->sa_family;
                  a->ai_addr = s;
                  a->ai_next = malloc(sizeof(struct addrinfo));
+                 oa = a;
                  a = a->ai_next;
           }
+          free(a);
+          oa->ai_next = NULL;
+          NSLog(@"Sockaddr connecting....");
+          [self dumpv6Addrinfo:ai];
+          [self realDoConnection];
           }
 }
 
 -(void)disconnect
 {
+    NSLog(@"Got a disconnect");
     dieflag = 1;
     do {} while (dieflag == 1);
 }
 {
     bufs = _bufs;
 }
+
+-(void)newDataAdded:(ITByteStream*)sender
+{
+}
+
+-(ITByteStream*)readPipe {return readPipe;}
+-(ITByteStream*)writePipe {return writePipe;}
 @end
 
 @implementation ITInetSocket(Debugging)
     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;
 
 -(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];
 }
 
 {
     NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
     NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]];
-    NSProxy *dp = [dcon rootProxy];
+    NSProxy *dp = [[dcon rootProxy] retain];
     char *buf = malloc(bufs);
-    unsigned long readLen = 0;
+    unsigned long readLen = 0,wpl = 0;
     signed int err;
-    NSLog(@"EYE MAEK CONNECT");
+    [readPipe setDelegate:(id <ITInetSocketDelegate>)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;
           }
+    }
+    NSLog(@"Sending finishedConnecting");
     [(id)dp finishedConnecting:self];
 lstart:
-
-          while (!actionflag && ![writePipe availableDataLength] && !dieflag)
+          state = ITInetSocketListening;
+          while (!actionflag && !dieflag && !(wpl = CFDataGetLength((CFDataRef)writePipe->data)))
           {
-                 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) {
-                        d = [NSData alloc];
-                        [d initWithBytesNoCopy:buf length:readLen freeWhenDone:NO];
-                        [readPipe writeData:d];
-                        [d release];
-                        [(id)dp dataReceived:self];
+                        NSLog(@"recv'd");
+                        NSLog(@"Doing writeData to readPipe");
+                        [readPipe writeBytes:buf len:readLen];
+                        [ap release];
+                        ap = [[NSAutoreleasePool alloc] init];
                  }
           }
-
+          state = ITInetSocketListening;
     actionflag = 0;
 
     if (dieflag)
           {
 dieaction:
+          state = ITInetSocketDisconnected;
           perror("Awh");
           free(buf);
           shutdown(sockfd,2);
           [dcon release];
+          [dp release];
           [ap release];
           dieflag = 0;
           return;
           }
 
     {
-          NSData *d = [writePipe readAllData];
-          write(sockfd,[d bytes],[d length]);
+          const char *d = CFDataGetBytePtr((CFDataRef)writePipe->data);
+          state = ITInetSocketWriting;
+          NSLog(@"Writing");
+          [writePipe lockStream];
+          wpl = send(sockfd,d,wpl,0);
+          [writePipe shortenData:wpl];
+          [writePipe unlockStream];
+          [ap release];
+          ap = [[NSAutoreleasePool alloc] init];
           goto lstart;
           }
     goto dieaction;