Permalink
Browse files

Initial checkin of Version 0.9 of the original NetSocket class (last …

…version I got from Dustin).
  • Loading branch information...
0 parents commit 56fa2d5a9b1a6106925a153bec6ef95799cf3ecc @uliwitness committed Jan 25, 2011
Showing with 1,221 additions and 0 deletions.
  1. +94 −0 NetSocket.h
  2. +1,127 −0 NetSocket.m
@@ -0,0 +1,94 @@
+
+// NetSocket
+// NetSocket.h
+// Version 0.9
+// Created by Dustin Mierau
+
+#import <Foundation/Foundation.h>
+#import <netinet/in.h>
+
+@interface NetSocket : NSObject
+{
+ CFSocketRef mCFSocketRef;
+ CFRunLoopSourceRef mCFSocketRunLoopSourceRef;
+ id mDelegate;
+ NSTimer* mConnectionTimer;
+ BOOL mSocketConnected;
+ BOOL mSocketListening;
+ NSMutableData* mOutgoingBuffer;
+ NSMutableData* mIncomingBuffer;
+}
+
+// Creation
++ (NetSocket*)netsocket;
++ (NetSocket*)netsocketListeningOnRandomPort;
++ (NetSocket*)netsocketListeningOnPort:(UInt16)inPort;
++ (NetSocket*)netsocketConnectedToHost:(NSString*)inHostname port:(UInt16)inPort;
+
+// Delegate
+- (id)delegate;
+- (void)setDelegate:(id)inDelegate;
+
+// Opening and Closing
+- (BOOL)open;
+- (void)close;
+
+// Runloop Scheduling
+- (BOOL)scheduleOnCurrentRunLoop;
+- (BOOL)scheduleOnRunLoop:(NSRunLoop*)inRunLoop;
+
+// Listening
+- (BOOL)listenOnRandomPort;
+- (BOOL)listenOnPort:(UInt16)inPort;
+- (BOOL)listenOnPort:(UInt16)inPort maxPendingConnections:(int)inMaxPendingConnections;
+
+// Connecting
+- (BOOL)connectToHost:(NSString*)inHostname port:(UInt16)inPort;
+- (BOOL)connectToHost:(NSString*)inHostname port:(UInt16)inPort timeout:(NSTimeInterval)inTimeout;
+
+// Peeking
+- (NSData*)peekData;
+
+// Reading
+- (unsigned)read:(void*)inBuffer amount:(unsigned)inAmount;
+- (unsigned)readOntoData:(NSMutableData*)inData;
+- (unsigned)readOntoData:(NSMutableData*)inData amount:(unsigned)inAmount;
+- (unsigned)readOntoString:(NSMutableString*)inString encoding:(NSStringEncoding)inEncoding amount:(unsigned)inAmount;
+- (NSData*)readData;
+- (NSData*)readData:(unsigned)inAmount;
+- (NSString*)readString:(NSStringEncoding)inEncoding;
+- (NSString*)readString:(NSStringEncoding)inEncoding amount:(unsigned)inAmount;
+
+// Writing
+- (void)write:(const void*)inBytes length:(unsigned)inLength;
+- (void)writeData:(NSData*)inData;
+- (void)writeString:(NSString*)inString encoding:(NSStringEncoding)inEncoding;
+
+// Properties
+- (NSString*)remoteHost;
+- (UInt16)remotePort;
+- (NSString*)localHost;
+- (UInt16)localPort;
+- (BOOL)isConnected;
+- (BOOL)isListening;
+- (unsigned)incomingBufferLength;
+- (unsigned)outgoingBufferLength;
+- (CFSocketNativeHandle)nativeSocketHandle;
+- (CFSocketRef)cfsocketRef;
+
+// Convenience methods
++ (void)ignoreBrokenPipes;
++ (NSString*)stringWithSocketAddress:(struct in_addr*)inAddress;
+
+@end
+
+#pragma mark -
+
+@interface NSObject (NetSocketDelegate)
+- (void)netsocketConnected:(NetSocket*)inNetSocket;
+- (void)netsocket:(NetSocket*)inNetSocket connectionTimedOut:(NSTimeInterval)inTimeout;
+- (void)netsocketDisconnected:(NetSocket*)inNetSocket;
+- (void)netsocket:(NetSocket*)inNetSocket connectionAccepted:(NetSocket*)inNewNetSocket;
+- (void)netsocket:(NetSocket*)inNetSocket dataAvailable:(unsigned)inAmount;
+- (void)netsocketDataSent:(NetSocket*)inNetSocket;
+@end
Oops, something went wrong.

0 comments on commit 56fa2d5

Please sign in to comment.