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:@""];
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;
52 [self spinoffReadLoop];
56 -(id)initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d
58 if (self = [super init])
60 state = ITInetSocketDisconnected;
62 delegate = [d retain];
64 writePipe = [[ITByteStream alloc] initWithDelegate:self];
65 readPipe = [[ITByteStream alloc] initWithDelegate:d];
69 actionflag = dieflag = 0;
82 if (!sarr) freeaddrinfo(ai);
86 -(void) connectToHost:(NSString*)host onPort:(short)thePort
88 if (state == ITInetSocketDisconnected)
90 NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
91 [self doConnSetupWithHost:host namedPort:nport];
95 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
97 if (state == ITInetSocketDisconnected)
99 [self doConnSetupWithHost:host namedPort:_port];
103 -(void) connectWithSockaddrArray:(NSArray*)arr
105 if (state == ITInetSocketDisconnected)
107 NSEnumerator *e = [arr objectEnumerator];
110 ai = malloc(sizeof(struct addrinfo));
112 while (d = [e nextObject])
114 struct sockaddr *s = (struct sockaddr*)[d bytes];
115 bzero(a,sizeof(struct addrinfo));
116 a->ai_family = s->sa_family;
118 a->ai_next = malloc(sizeof(struct addrinfo));
122 [self realDoConnection];
128 NSLog(@"Got a disconnect");
132 -(void)retryConnection
134 ai_cur = ai_cur->ai_next?ai_cur->ai_next:ai_cur;
136 [self realDoConnection];
139 -(ITInetSocketState)state
143 -(id <ITInetSocketDelegate>)delegate
148 -(unsigned short)bufferSize
153 -(void)setBufferSize:(unsigned short)_bufs
158 -(void)newDataAdded:(ITByteStream*)sender
160 NSLog(@"writePipe got something");
162 do {} while (actionflag == 1);
163 NSLog(@"thread saw actionFlag");
167 @implementation ITInetSocket(Debugging)
168 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
183 "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
188 NSString *nsfmt = [NSString stringWithCString:cfmt];
189 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)_ai->ai_addr;
190 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"];
196 @implementation ITInetSocket(Private)
197 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
199 struct addrinfo hints;
201 const char *portNam = [namedPort cString], *hostCStr = [host cString];
202 state = ITInetSocketConnecting;
204 hints.ai_family = PF_UNSPEC;
205 hints.ai_socktype = SOCK_STREAM;
206 hints.ai_protocol = IPPROTO_TCP;
207 hints.ai_addrlen = 0;
208 hints.ai_canonname = NULL;
209 hints.ai_addr = NULL;
210 hints.ai_next = NULL;
212 err = getaddrinfo(hostCStr,portNam,&hints,&ai);
214 NSLog(@"%s, h %@ p %@",gai_strerror(err),host,namedPort);
215 NSLog(ai?[self dumpv6Addrinfo:ai]:@"");
217 [self realDoConnection];
220 -(void)realDoConnection
222 sockfd = socket(ai_cur->ai_family,SOCK_STREAM,IPPROTO_TCP);
223 [self spinoffReadLoop];
226 -(void)spinoffReadLoop
228 NSPort *p1 = [NSPort port], *p2 = [NSPort port];
229 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:p1 sendPort:p2];
230 NSArray *par = [NSArray arrayWithObjects:p2,p1,nil];
231 [dcon setRootObject:delegate];
232 [NSThread detachNewThreadSelector:@selector(socketReadLoop:) toTarget:self withObject:par]; //spawn read thread
235 -(void)socketReadLoop:(id)data
237 NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
238 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]];
239 NSProxy *dp = [[dcon rootProxy] retain];
240 char *buf = malloc(bufs);
241 unsigned long readLen = 0;
243 [readPipe setDelegate:dp];
245 NSLog(@"Connecting");
246 err = connect(sockfd,ai_cur->ai_addr,ai_cur->ai_addrlen);
250 [(id)dp errorOccured:ITInetCouldNotConnect during:ITInetSocketConnecting onSocket:self];
254 NSLog(@"Sending finishedConnecting");
255 [(id)dp finishedConnecting:self];
257 state = ITInetSocketListening;
258 while (!actionflag && !dieflag)
260 readLen = recv(sockfd,buf,bufs,0);
261 state = ITInetSocketReading;
262 if (readLen == -1) {[(id)dp errorOccured:ITInetConnectionDropped during:ITInetSocketReading onSocket:self];goto dieaction;}
265 NSLog(@"Doing writeData to readPipe");
266 [readPipe writeBytes:buf len:readLen];
268 ap = [[NSAutoreleasePool alloc] init];
271 state = ITInetSocketListening;
277 state = ITInetSocketDisconnected;
289 state = ITInetSocketWriting;
290 NSLog(@"Emptying writePipe");
291 NSData *d = [writePipe readAllData];
292 write(sockfd,[d bytes],[d length]);
294 ap = [[NSAutoreleasePool alloc] init];