5 // Created by Alexander Strange on Tue Feb 11 2003.
6 // Copyright (c) 2003 __MyCompanyName__. All rights reserved.
9 #import "ITInetSocket.h"
10 #import "ITServiceBrowserDelegate.h"
11 #import <sys/socket.h>
15 @interface ITInetSocket(Debugging)
16 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
19 @interface ITInetSocket(Private)
20 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
21 -(void)realDoConnection;
22 -(void)spinoffReadLoop;
23 -(void)socketReadLoop:(id)data;
26 @implementation ITInetSocket
27 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d
29 NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
30 ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
32 [browse setDelegate:bd];
33 [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
36 -(id)initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d
38 if (self = [super init])
40 state = ITInetSocketListening;
42 delegate = [d retain];
44 writePipe = [[ITByteStream alloc] initWithDelegate:self];
45 readPipe = [[ITByteStream alloc] initWithDelegate:d];
49 actionflag = dieflag = 0;
54 -(id)initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d
56 if (self = [super init])
58 state = ITInetSocketDisconnected;
60 delegate = [d retain];
62 writePipe = [[ITByteStream alloc] initWithDelegate:self];
63 readPipe = [[ITByteStream alloc] initWithDelegate:d];
67 actionflag = dieflag = 0;
79 if (!sarr) freeaddrinfo(ai);
83 -(void) connectToHost:(NSString*)host onPort:(short)thePort
85 if (state == ITInetSocketDisconnected)
87 NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
88 [self doConnSetupWithHost:host namedPort:nport];
92 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
94 if (state == ITInetSocketDisconnected)
96 [self doConnSetupWithHost:host namedPort:_port];
100 -(void) connectWithSockaddrArray:(NSArray*)arr
102 if (state == ITInetSocketDisconnected)
104 NSEnumerator *e = [arr objectEnumerator];
107 ai = malloc(sizeof(struct addrinfo));
109 while (d = [e nextObject])
111 struct sockaddr *s = (struct sockaddr*)[d bytes];
112 a->ai_family = s->sa_family;
114 a->ai_next = malloc(sizeof(struct addrinfo));
122 NSLog(@"Got a disconnect");
124 do {} while (dieflag == 1);
127 -(void)retryConnection
129 ai_cur = ai_cur->ai_next?ai_cur->ai_next:ai_cur;
131 [self realDoConnection];
134 -(ITInetSocketState)state
138 -(id <ITInetSocketDelegate>)delegate
143 -(unsigned short)bufferSize
148 -(void)setBufferSize:(unsigned short)_bufs
153 -(void)newDataAdded:(ITByteStream*)sender
155 NSLog(@"writePipe got something");
157 NSLog(@"thread saw actionFlag");
161 @implementation ITInetSocket(Debugging)
162 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
177 "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
182 NSString *nsfmt = [NSString stringWithCString:cfmt];
183 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)_ai->ai_addr;
184 NSString *buf = [[NSMutableString alloc] initWithFormat:nsfmt,_ai->ai_flags,_ai->ai_family,_ai->ai_socktype,_ai->ai_protocol,_ai->ai_canonname?_ai->ai_canonname:"",sa->sin6_len,sa->sin6_family,sa->sin6_port,sa->sin6_flowinfo,sa->sin6_addr.__u6_addr.__u6_addr16[0],sa->sin6_addr.__u6_addr.__u6_addr16[1],sa->sin6_addr.__u6_addr.__u6_addr16[2],sa->sin6_addr.__u6_addr.__u6_addr16[3],sa->sin6_addr.__u6_addr.__u6_addr16[4],sa->sin6_addr.__u6_addr.__u6_addr16[5],sa->sin6_addr.__u6_addr.__u6_addr16[6],sa->sin6_addr.__u6_addr.__u6_addr16[7],sa->sin6_scope_id,_ai->ai_next?[self dumpv6Addrinfo:_ai->ai_next]:@"nil"];
190 @implementation ITInetSocket(Private)
191 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
193 struct addrinfo hints;
195 const char *portNam = [namedPort cString], *hostCStr = [host cString];
198 hints.ai_family = PF_UNSPEC;
199 hints.ai_socktype = SOCK_STREAM;
200 hints.ai_protocol = IPPROTO_TCP;
201 hints.ai_addrlen = 0;
202 hints.ai_canonname = NULL;
203 hints.ai_addr = NULL;
204 hints.ai_next = NULL;
206 err = getaddrinfo(hostCStr,portNam,&hints,&ai);
208 NSLog(@"%s, h %@ p %@",gai_strerror(err),host,namedPort);
209 NSLog(ai?[self dumpv6Addrinfo:ai]:@"");
211 [self realDoConnection];
214 -(void)realDoConnection
216 sockfd = socket(ai_cur->ai_addr->sa_family,SOCK_STREAM,IPPROTO_TCP);
217 [self spinoffReadLoop];
220 -(void)spinoffReadLoop
222 NSPort *p1 = [NSPort port], *p2 = [NSPort port];
223 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:p1 sendPort:p2];
224 NSArray *par = [NSArray arrayWithObjects:p2,p1,nil];
225 [dcon setRootObject:delegate];
226 [NSThread detachNewThreadSelector:@selector(socketReadLoop:) toTarget:self withObject:par]; //spawn read thread
229 -(void)socketReadLoop:(id)data
231 NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
232 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]];
233 NSProxy *dp = [[dcon rootProxy] retain];
234 char *buf = malloc(bufs);
235 unsigned long readLen = 0;
237 [readPipe setDelegate:dp];
238 NSLog(@"Connecting");
239 err = connect(sockfd,ai_cur->ai_addr,ai_cur->ai_addrlen);
243 [(id)dp errorOccured:ITInetCouldNotConnect during:ITInetSocketConnecting onSocket:self];
246 NSLog(@"Sending finishedConnecting");
247 [(id)dp finishedConnecting:self];
250 while (!actionflag && !dieflag)
253 readLen = recv(sockfd,buf,bufs,0);
254 if (readLen == -1) {[(id)dp errorOccured:ITInetConnectionDropped during:ITInetSocketReading onSocket:self];goto dieaction;}
257 NSLog(@"Doing writeData to readPipe");
258 [readPipe writeBytes:buf len:readLen];
260 ap = [[NSAutoreleasePool alloc] init];
280 NSLog(@"Emptying writePipe");
281 NSData *d = [writePipe readAllData];
282 write(sockfd,[d bytes],[d length]);
284 ap = [[NSAutoreleasePool alloc] init];