More socket work
authorAlexander Strange <astrange@ithinksw.com>
Sat, 15 Mar 2003 20:24:00 +0000 (20:24 +0000)
committerAlexander Strange <astrange@ithinksw.com>
Sat, 15 Mar 2003 20:24:00 +0000 (20:24 +0000)
ITAppleEventCenter.m
ITByteStream.m
ITInetSocket.h
ITInetSocket.m
ITServiceBrowserDelegate.h [new file with mode: 0755]
ITServiceBrowserDelegate.m [new file with mode: 0755]
ShowcaseController.m

index 54987cc..46da952 100755 (executable)
@@ -450,7 +450,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
 - (void)printCarbonDesc:(AEDesc*)desc {
     Handle xx;
     AEPrintDescToHandle(desc,&xx);
 - (void)printCarbonDesc:(AEDesc*)desc {
     Handle xx;
     AEPrintDescToHandle(desc,&xx);
-    //NSLog(@"Handle: %s", *xx);
+    NSLog(@"Handle: %s", *xx);
     DisposeHandle(xx);
 }
 
     DisposeHandle(xx);
 }
 
index e868008..eff6eb7 100755 (executable)
@@ -8,7 +8,7 @@
 
 #import "ITByteStream.h"
 
 
 #import "ITByteStream.h"
 
-// TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? I could hack it with socketpair(), i guess)
+// TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? NSConditionLock?)
 
 @implementation ITByteStream
 -(id) init
 
 @implementation ITByteStream
 -(id) init
 
 -(int) availableDataLength
 {
 
 -(int) availableDataLength
 {
-    return [data length];
+    int len;
+    [lock lock];
+    len = [data length];
+    [lock unlock];
+    return len;
 }
 
 -(NSData*) readDataOfLength:(int)length
 }
 
 -(NSData*) readDataOfLength:(int)length
     [lock lock];
     ret = [data subdataWithRange:range];
 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
     [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]; // this should delete off the end. should test.
+    [data replaceBytesInRange:range withBytes:nil length:0];
 #else
     range = {length, [data length]};
     tmp = [data subdataWithRange:range];
 #else
     range = {length, [data length]};
     tmp = [data subdataWithRange:range];
-    [data setData:tmp]; // maybe i should add a lock to this? it would be bad if someone was writing when it was reading...
+    [data setData:tmp];
 #endif
     [lock unlock];
     return ret;
 #endif
     [lock unlock];
     return ret;
index 2430267..edbfa64 100755 (executable)
@@ -43,10 +43,14 @@ typedef enum {
     struct addrinfo *ai;
     ITByteStream *readPipe, *writePipe;
     ITInetSocketState state;
     struct addrinfo *ai;
     ITByteStream *readPipe, *writePipe;
     ITInetSocketState state;
+    NSArray *sarr;
 }
 }
++(void)startAutoconnectingToService:(NSString*)type delegate:(id)d;
 -(id) initWithFD:(int)fd delegate:(id)d;
 -(id) initWithDelegate:(id)d;
 
 -(void) connectToHost:(NSString*)host onPort:(short)port;
 -(id) initWithFD:(int)fd delegate:(id)d;
 -(id) initWithDelegate:(id)d;
 
 -(void) connectToHost:(NSString*)host onPort:(short)port;
+-(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
+-(void) connectWithSockaddrArray:(NSArray*)arr;
 -(ITInetSocketState) state;
 @end
 -(ITInetSocketState) state;
 @end
index 0be64d5..4e40628 100755 (executable)
@@ -7,14 +7,29 @@
 //
 
 #import "ITInetSocket.h"
 //
 
 #import "ITInetSocket.h"
+#import "ITServiceBrowserDelegate.h"
 #import <sys/socket.h>
 #import <arpa/inet.h>
 
 #import <sys/socket.h>
 #import <arpa/inet.h>
 
+
 @interface ITInetSocket(Debugging)
 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
 @end
 
 @interface ITInetSocket(Debugging)
 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
 @end
 
+@interface ITInetSocket(Private)
+-(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
+@end
+
 @implementation ITInetSocket
 @implementation ITInetSocket
++(void)startAutoconnectingToService:(NSString*)type delegate:(id)d
+{
+    NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
+    ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
+
+    [browse setDelegate:bd];
+    [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
+}
+
 -(id)initWithFD:(int)fd delegate:(id)d
 {
     if (self = [super init])
 -(id)initWithFD:(int)fd delegate:(id)d
 {
     if (self = [super init])
           writePipe = [[ITByteStream alloc] init];
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
           writePipe = [[ITByteStream alloc] init];
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
+          sarr = nil;
           }
     return self;
 }
 
           }
     return self;
 }
 
+-(void) dealloc
+{
+    shutdown(sockfd,2);
+    [delegate release];
+    [writePipe release];
+    [readPipe release];
+    if (!sarr) freeaddrinfo(ai);
+    [sarr release];
+}
+
 -(id)initWithDelegate:(id)d
 {
     if (self = [super init])
 -(id)initWithDelegate:(id)d
 {
     if (self = [super init])
@@ -41,6 +67,7 @@
           writePipe = [[ITByteStream alloc] init];
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
           writePipe = [[ITByteStream alloc] init];
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
+          sarr = nil;
           }
     return self;
 }
           }
     return self;
 }
 {
     if (state == ITInetSocketDisconnected)
           {
 {
     if (state == ITInetSocketDisconnected)
           {
-          struct addrinfo hints;
-          int err;
-          const char *portNam = [[[NSNumber numberWithShort:thePort] stringValue] cString], *hostCStr = [host cString];
-
-          hints.ai_flags = 0;
-          hints.ai_family = PF_INET6;
-          hints.ai_socktype = SOCK_STREAM;
-          hints.ai_protocol = IPPROTO_TCP;
-          hints.ai_canonname = NULL;
-          hints.ai_addr = NULL;
-          hints.ai_next = NULL;
+          NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
+          [self doConnSetupWithHost:host namedPort:nport];
+          }
+}
 
 
-          err = getaddrinfo(hostCStr,portNam,&hints,&ai);
-          if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
+-(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
+{
+    if (state == ITInetSocketDisconnected)
           {
           {
-                 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
+          [self doConnSetupWithHost:host namedPort:_port];
           }
           }
-          NSLog([self dumpv6Addrinfo:ai]);
+}
+
+-(void) connectWithSockaddrArray:(NSArray*)arr
+{
+    NSEnumerator *e = [arr objectEnumerator];
+    NSData *d;
+    struct addrinfo *a;
+    ai = malloc(sizeof(struct addrinfo));
+    a = ai;
+    while (d = [e nextObject])
+          {
+          struct sockaddr *s = (struct sockaddr*)[d bytes];
+          a->ai_family = s->sa_family;
+          a->ai_addr = s;
+          a->ai_next = malloc(sizeof(struct addrinfo));
+          a = a->ai_next;
           }
 }
 
           }
 }
 
     "\tFamily = %x\n"
     "\tPort = %d\n"
     "\tFlowinfo = %x\n"
     "\tFamily = %x\n"
     "\tPort = %d\n"
     "\tFlowinfo = %x\n"
-    "\tAddr = {%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx}\n"
+    "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
     "\tScope = %x\n"
     "}\n"
     "Next = ";
     "\tScope = %x\n"
     "}\n"
     "Next = ";
     [buf appendString:@"nil\n}"];
     return buf;
 }
     [buf appendString:@"nil\n}"];
     return buf;
 }
+@end
+
+@implementation ITInetSocket(Private)
+-(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
+{
+    struct addrinfo hints;
+          int err;
+          const char *portNam = [namedPort cString], *hostCStr = [host cString];
+
+          hints.ai_flags = 0;
+          hints.ai_family = PF_INET6;
+          hints.ai_socktype = SOCK_STREAM;
+          hints.ai_protocol = IPPROTO_TCP;
+          hints.ai_canonname = NULL;
+          hints.ai_addr = NULL;
+          hints.ai_next = NULL;
+
+          err = getaddrinfo(hostCStr,portNam,&hints,&ai);
+          if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
+          {
+                 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
+          }
+          NSLog([self dumpv6Addrinfo:ai]);
+}
 @end
\ No newline at end of file
 @end
\ No newline at end of file
diff --git a/ITServiceBrowserDelegate.h b/ITServiceBrowserDelegate.h
new file mode 100755 (executable)
index 0000000..2cecadf
--- /dev/null
@@ -0,0 +1,16 @@
+//
+//  ITServiceBrowserDelegate.h
+//  ITFoundation
+//
+//  Created by Alexander Strange on Sat Mar 15 2003.
+//  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <Foundation/NSNetServices.h>
+
+@interface ITServiceBrowserDelegate : NSObject {
+    id delegate;
+}
+-(id) initWithDelegate:(id)delegate;
+@end
diff --git a/ITServiceBrowserDelegate.m b/ITServiceBrowserDelegate.m
new file mode 100755 (executable)
index 0000000..0dfcce8
--- /dev/null
@@ -0,0 +1,35 @@
+//
+//  ITServiceBrowserDelegate.m
+//  ITFoundation
+//
+//  Created by Alexander Strange on Sat Mar 15 2003.
+//  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import "ITServiceBrowserDelegate.h"
+#import "ITInetSocket.h"
+#import <Foundation/NSNetServices.h>
+
+@implementation ITServiceBrowserDelegate
+- (id) initWithDelegate:(id)_delegate
+{
+    if (self = [super init])
+          {
+          delegate = _delegate;
+          }
+    return self;
+}
+
+- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
+{
+    ITInetSocket *sock;
+    if (!moreComing)
+          {
+          [aNetServiceBrowser stop];
+          [aNetServiceBrowser release];
+          }
+    sock = [[ITInetSocket alloc] initWithDelegate:delegate];
+    [sock connectWithSockaddrArray:[aNetService addresses]];
+}
+
+@end
index 00fb696..722756f 100755 (executable)
 @implementation ShowcaseController
 - (void)awakeFromNib
 {
 @implementation ShowcaseController
 - (void)awakeFromNib
 {
-    
+    /*
     ITInetServerSocket *sock = [[ITInetServerSocket alloc] initWithDelegate:self];
     NSLog(@"rawr?");
     [sock setPort:4776];
     [sock setServiceName:@"Test Rendezvous Service"];
     [sock setServiceType:@"ittest" useForPort:NO];
     [sock start];
     ITInetServerSocket *sock = [[ITInetServerSocket alloc] initWithDelegate:self];
     NSLog(@"rawr?");
     [sock setPort:4776];
     [sock setServiceName:@"Test Rendezvous Service"];
     [sock setServiceType:@"ittest" useForPort:NO];
     [sock start];
+       */
 }
 
 - (void)newClientJoined:(ITInetSocket*)client
 }
 
 - (void)newClientJoined:(ITInetSocket*)client