More socket work
authorAlexander Strange <astrange@ithinksw.com>
Sun, 16 Mar 2003 19:57:20 +0000 (19:57 +0000)
committerAlexander Strange <astrange@ithinksw.com>
Sun, 16 Mar 2003 19:57:20 +0000 (19:57 +0000)
ITAppleEventCenter.m
ITByteStream.h
ITByteStream.m
ITInetSocket.h
ITInetSocket.m

index 535154b..5f485e0 100755 (executable)
@@ -1,12 +1,6 @@
 #import "ITAppleEventCenter.h"
 
 #import "ITAppleEventCenter.h"
 
-Boolean MyAEIdleCallback (
-                                        EventRecord * theEvent,
-                                        SInt32 * sleepTime,
-                                        RgnHandle * mouseRgn
-                                        );
-
-Boolean MyAEIdleCallback (
+static Boolean MyAEIdleCallback (
                                         EventRecord * theEvent,
                                         SInt32 * sleepTime,
                                         RgnHandle * mouseRgn
                                         EventRecord * theEvent,
                                         SInt32 * sleepTime,
                                         RgnHandle * mouseRgn
index 3d726b1..eeb9c76 100755 (executable)
@@ -19,5 +19,6 @@
 -(id) initWithStream:(ITByteStream*)stream;
 -(int) availableDataLength;
 -(NSData*) readDataOfLength:(int)length;
 -(id) initWithStream:(ITByteStream*)stream;
 -(int) availableDataLength;
 -(NSData*) readDataOfLength:(int)length;
+-(NSData*) readAllData;
 -(void) writeData:(in NSData*)data;
 @end
 -(void) writeData:(in NSData*)data;
 @end
index eff6eb7..4d34eb0 100755 (executable)
     return ret;
 }
 
     return ret;
 }
 
+-(NSData*) readAllData
+{
+    NSData *ret;
+    [lock lock];
+    ret = [data autorelease];
+    data = [[NSMutableData alloc] init];
+    [lock unlock];
+    return ret;
+}
+
 -(void) writeData:(NSData*)_data
 {
     [lock lock];
 -(void) writeData:(NSData*)_data
 {
     [lock lock];
index 2f25958..aaf55b9 100755 (executable)
@@ -70,7 +70,7 @@ typedef enum {
  * @discussion The delegate should check [sender readPipe] to get the data.
  * @param sender The socket that the messages came from.
  */
  * @discussion The delegate should check [sender readPipe] to get the data.
  * @param sender The socket that the messages came from.
  */
-- (void) dataRecieved:(ITInetSocket *)sender;
+- (void) dataRecieved:(in ITInetSocket *)sender;
 /*!
  * @method errorOccured:during:onSocket:
  * @abstract Alerts the delegate of an error condition.
 /*!
  * @method errorOccured:during:onSocket:
  * @abstract Alerts the delegate of an error condition.
@@ -79,14 +79,14 @@ typedef enum {
  * @param state What the socket was doing when the error occured.
  * @param sender The socket the error occured on.
  */
  * @param state What the socket was doing when the error occured.
  * @param sender The socket the error occured on.
  */
-- (void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(ITInetSocket*)sender;
+- (void) errorOccured:(ITInetSocketError)err during:(ITInetSocketState)state onSocket:(in ITInetSocket*)sender;
 /*!
  * @method finishedConnecting:
  * @abstract Alerts the delegate of a successful connection attempt.
  * @discussion The delegate should send whatever initial data is required for the protocol (nickname for IRC, etc.)
  * @param sender The socket that established the connection.
  */
 /*!
  * @method finishedConnecting:
  * @abstract Alerts the delegate of a successful connection attempt.
  * @discussion The delegate should send whatever initial data is required for the protocol (nickname for IRC, etc.)
  * @param sender The socket that established the connection.
  */
-- (void) finishedConnecting:(ITInetSocket *)sender;
+- (void) finishedConnecting:(in ITInetSocket *)sender;
 @end
 
 /*!
 @end
 
 /*!
@@ -96,19 +96,48 @@ typedef enum {
  */
 @interface ITInetSocket : NSObject {
     @public
  */
 @interface ITInetSocket : NSObject {
     @public
+    /*!
+     * @var sockfd
+       * @abstract KLWONZ
+     */
     int sockfd;
     int port;
     int sockfd;
     int port;
-    id delegate;
+    unsigned short bufs;
+    int dieflag;
+    int actionflag;
+    id <ITInetSocketDelegate,NSObject> delegate;
     struct addrinfo *ai, *ai_cur;
     ITByteStream *readPipe, *writePipe;
     ITInetSocketState state;
     NSArray *sarr;
 }
     struct addrinfo *ai, *ai_cur;
     ITByteStream *readPipe, *writePipe;
     ITInetSocketState state;
     NSArray *sarr;
 }
-+(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate>)d;
--(id) initWithFD:(int)fd delegate:(id <ITInetSocketDelegate>)d;
--(id) initWithDelegate:(id <ITInetSocketDelegate>)d;
+/*!
+ * @method startAutoconnectingToService:delegate:
+ * @abstract Automatically creates sockets whenever a certain type of Rendezvous service appears.
+ * @discussion The auto-created sockets will send finishedConnecting: to the delegate.
+ * @param type The type of Rendezvous service to listen on.
+ * @param d The delegate that the sockets will belong to.
+ */
++(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d;
+/*!
+ * @method initWithFD:delegate:
+ * @abstract Wraps a socket around an existing socket descriptor.
+ * @discussion The socket will start listening on the descriptor as normal.
+ * @param fd The descriptor.
+ * @param d The delegate for the socket.
+ */
+-(id) initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d;
+/*!
+ * @method initWithDelegate:
+ * @abstract Creates a new socket.
+ * @discussion The socket will not be connected to anything.
+ * @param d The delegate of the socket.
+ */
+-(id) initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d;
 
 -(id <ITInetSocketDelegate>)delegate;
 
 -(id <ITInetSocketDelegate>)delegate;
+-(unsigned short)bufferSize;
+-(void)setBufferSize:(unsigned short)bufs;
 -(void) connectToHost:(NSString*)host onPort:(short)port;
 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
 -(void) connectWithSockaddrArray:(NSArray*)arr;
 -(void) connectToHost:(NSString*)host onPort:(short)port;
 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)port;
 -(void) connectWithSockaddrArray:(NSArray*)arr;
index 4e40628..7d3989b 100755 (executable)
@@ -10,7 +10,7 @@
 #import "ITServiceBrowserDelegate.h"
 #import <sys/socket.h>
 #import <arpa/inet.h>
 #import "ITServiceBrowserDelegate.h"
 #import <sys/socket.h>
 #import <arpa/inet.h>
-
+#import <unistd.h>
 
 @interface ITInetSocket(Debugging)
 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
 
 @interface ITInetSocket(Debugging)
 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
 
 @interface ITInetSocket(Private)
 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
 
 @interface ITInetSocket(Private)
 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
+-(void)spinoffReadLoop;
+-(void)socketReadLoop:(id)data;
 @end
 
 @implementation ITInetSocket
 @end
 
 @implementation ITInetSocket
-+(void)startAutoconnectingToService:(NSString*)type delegate:(id)d
++(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d
 {
     NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
     ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
 {
     NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
     ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
@@ -30,7 +32,7 @@
     [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
 }
 
     [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
 }
 
--(id)initWithFD:(int)fd delegate:(id)d
+-(id)initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d
 {
     if (self = [super init])
           {
 {
     if (self = [super init])
           {
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
           sarr = nil;
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
           sarr = nil;
+          bufs = 512;
+          actionflag = dieflag = 0;
           }
     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
+-(id)initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d
 {
     if (self = [super init])
           {
 {
     if (self = [super init])
           {
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
           sarr = nil;
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
           sarr = nil;
+          bufs = 512;
+          actionflag = dieflag = 0;
           }
     return self;
 }
 
           }
     return self;
 }
 
+
+-(void) dealloc
+{
+    shutdown(sockfd,2);
+    [delegate release];
+    [writePipe release];
+    [readPipe release];
+    if (!sarr) freeaddrinfo(ai);
+    [sarr release];
+}
+
 -(void) connectToHost:(NSString*)host onPort:(short)thePort
 {
     if (state == ITInetSocketDisconnected)
 -(void) connectToHost:(NSString*)host onPort:(short)thePort
 {
     if (state == ITInetSocketDisconnected)
 
 -(void) connectWithSockaddrArray:(NSArray*)arr
 {
 
 -(void) connectWithSockaddrArray:(NSArray*)arr
 {
-    NSEnumerator *e = [arr objectEnumerator];
-    NSData *d;
-    struct addrinfo *a;
-    ai = malloc(sizeof(struct addrinfo));
-    a = ai;
-    while (d = [e nextObject])
+    if (state == ITInetSocketDisconnected)
+          {
+          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;
+                 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;
+          }
           }
 }
 
           }
 }
 
+-(void)disconnect
+{
+}
+
+-(void)retryConnection
+{
+}
+
 -(ITInetSocketState)state
 {
     return state;
 }
 -(ITInetSocketState)state
 {
     return state;
 }
+-(id <ITInetSocketDelegate>)delegate
+{
+    return delegate;
+}
+
+-(unsigned short)bufferSize
+{
+    return bufs;
+}
+
+-(void)setBufferSize:(unsigned short)_bufs
+{
+    bufs = _bufs;
+}
 @end
 
 @implementation ITInetSocket(Debugging)
 @end
 
 @implementation ITInetSocket(Debugging)
           }
           NSLog([self dumpv6Addrinfo:ai]);
 }
           }
           NSLog([self dumpv6Addrinfo:ai]);
 }
+
+-(void)spinoffReadLoop
+{
+    NSPort *p1 = [NSPort port], *p2 = [NSPort port];
+    NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:p1 sendPort:p2];
+    NSArray *par = [NSArray arrayWithObjects:p2,p1,nil];
+    [dcon setRootObject:delegate];
+    [NSThread detachNewThreadSelector:@selector(socketReadLoop:) toTarget:self withObject:par]; //spawn read thread
+}
+
+-(void)socketReadLoop:(id)data
+{
+    NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
+    NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]];
+    NSProxy *dp = [dcon rootProxy];
+    char *buf = malloc(bufs);
+    unsigned long readLen = 0;
+
+lstart:
+    do
+          {
+                 NSData *d = [NSData alloc];
+                 readLen = recv(sockfd,buf,bufs,0);
+                 [d initWithBytesNoCopy:buf length:readLen];
+                 [readPipe writeData:d];
+                 [d release];
+                 [dp dataRecieved:self];
+          }
+    while (!actionflag);
+    actionflag = 0;
+    if (dieflag)
+          {
+          free(buf);
+          [dcon release];
+          [ap release];
+          dieflag = 0;
+          return;
+          }
+
+    {
+          NSData *d = [writePipe readAllData];
+          write(sockfd,[d bytes],[d length]);
+          goto lstart;
+          }
+}
 @end
\ No newline at end of file
 @end
\ No newline at end of file