# HG changeset patch # User Jens Alfke # Date 1212104436 25200 # Node ID 6f539dd9921cdb79b677e9859b81526f218e9d7a # Parent 5936db2c1987a3af5bb992070fbc3a9e0de84589 Got it to build on iPhone. (Haven't tried running it yet.) diff -r 5936db2c1987 -r 6f539dd9921c BLIP/BLIP.png Binary file BLIP/BLIP.png has changed diff -r 5936db2c1987 -r 6f539dd9921c BLIP/BLIPDispatcher.h --- a/BLIP/BLIPDispatcher.h Sun May 25 13:43:03 2008 -0700 +++ b/BLIP/BLIPDispatcher.h Thu May 29 16:40:36 2008 -0700 @@ -10,8 +10,9 @@ @class MYTarget, BLIPMessage; -/** Routes BLIP messages to targets based on a series of rule predicates. - Every BLIPConnection has a BLIPDispatcher, which is initially empty but you can add rules +/** Routes BLIP messages to targets based on a series of rules. + + Every BLIPConnection has a BLIPDispatcher, which is initially empty, but you can add rules to it. Every BLIPListener also has a dispatcher, which is inherited as the parent by every @@ -33,11 +34,13 @@ the parent, if there is one. */ @property (retain) BLIPDispatcher *parent; +/** Convenience method that adds a rule that compares a property against a string. */ +- (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key; + +#if ! TARGET_OS_IPHONE /* NSPredicate is not available on iPhone, unfortunately */ /** Adds a new rule, to call a given target method if a given predicate matches the message. */ - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate; - -/** Convenience method that adds a rule that compares a property against a string. */ -- (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key; +#endif /** Removes all rules with the given target method. */ - (void) removeTarget: (MYTarget*)target; diff -r 5936db2c1987 -r 6f539dd9921c BLIP/BLIPDispatcher.m --- a/BLIP/BLIPDispatcher.m Sun May 25 13:43:03 2008 -0700 +++ b/BLIP/BLIPDispatcher.m Thu May 29 16:40:36 2008 -0700 @@ -39,11 +39,13 @@ @synthesize parent=_parent; +#if ! TARGET_OS_IPHONE - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate { [_targets addObject: target]; [_predicates addObject: predicate]; } +#endif - (void) removeTarget: (MYTarget*)target @@ -58,12 +60,29 @@ - (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key { +#if TARGET_OS_IPHONE + Assert(target); + [_predicates addObject: $array(key,value)]; + [_targets addObject: target]; +#else [self addTarget: target forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key] rightExpression: [NSExpression expressionForConstantValue: value] modifier: NSDirectPredicateModifier type: NSEqualToPredicateOperatorType options: 0]]; +#endif +} + + +static BOOL testPredicate( id predicate, NSDictionary *properties ) { +#if TARGET_OS_IPHONE + NSString *key = [predicate objectAtIndex: 0]; + NSString *value = [predicate objectAtIndex: 1]; + return $equal( [properties objectForKey: key], value ); +#else + return [(NSPredicate*)predicate evaluateWithObject: properties]; +#endif } @@ -72,8 +91,8 @@ NSDictionary *properties = message.properties.allProperties; NSUInteger n = _predicates.count; for( NSUInteger i=0; i available ) { // Properties not complete yet. *usedLength = 0; @@ -307,7 +307,7 @@ NSUInteger length = data.length - sizeof(UInt16); if( length > 0xFFFF ) return nil; - *(UInt16*)[data mutableBytes] = EndianU16_NtoB((UInt16)length); + *(UInt16*)[data mutableBytes] = NSSwapHostShortToBig((UInt16)length); return data; } diff -r 5936db2c1987 -r 6f539dd9921c BLIP/BLIPReader.m --- a/BLIP/BLIPReader.m Sun May 25 13:43:03 2008 -0700 +++ b/BLIP/BLIPReader.m Thu May 29 16:40:36 2008 -0700 @@ -63,10 +63,10 @@ - (NSString*) _validateHeader { // Convert header to native byte order: - _curHeader.magic = EndianU32_BtoN(_curHeader.magic); - _curHeader.number= EndianU32_BtoN(_curHeader.number); - _curHeader.flags = EndianU16_BtoN(_curHeader.flags); - _curHeader.size = EndianU16_BtoN(_curHeader.size); + _curHeader.magic = NSSwapBigIntToHost(_curHeader.magic); + _curHeader.number= NSSwapBigIntToHost(_curHeader.number); + _curHeader.flags = NSSwapBigShortToHost(_curHeader.flags); + _curHeader.size = NSSwapBigShortToHost(_curHeader.size); if( _curHeader.magic != kBLIPFrameHeaderMagicNumber ) return $sprintf(@"Incorrect magic number (%08X not %08X)", diff -r 5936db2c1987 -r 6f539dd9921c BLIP/BLIPTest.m --- a/BLIP/BLIPTest.m Sun May 25 13:43:03 2008 -0700 +++ b/BLIP/BLIPTest.m Thu May 29 16:40:36 2008 -0700 @@ -233,7 +233,7 @@ Assert(listenerIdentity); _listener.SSLProperties = $mdict({kTCPPropertySSLCertificates, $array((id)listenerIdentity)}, {kTCPPropertySSLAllowsAnyRoot,$true}, - {kTCPPropertySSLClientSideAuthentication, $object(kTryAuthenticate)}); + {kTCPPropertySSLClientSideAuthentication, $object(kTCPTryAuthenticate)}); } Assert( [_listener open] ); Log(@"%@ is listening...",self); diff -r 5936db2c1987 -r 6f539dd9921c TCP/TCPConnection.h --- a/TCP/TCPConnection.h Sun May 25 13:43:03 2008 -0700 +++ b/TCP/TCPConnection.h Thu May 29 16:40:36 2008 -0700 @@ -7,6 +7,7 @@ // #import "TCPEndpoint.h" +#import @class IPAddress; @class TCPReader, TCPWriter, TCPListener; @protocol TCPConnectionDelegate; diff -r 5936db2c1987 -r 6f539dd9921c TCP/TCPConnection.m --- a/TCP/TCPConnection.m Sun May 25 13:43:03 2008 -0700 +++ b/TCP/TCPConnection.m Thu May 29 16:40:36 2008 -0700 @@ -14,6 +14,15 @@ #import "ExceptionUtils.h" +#if TARGET_OS_IPHONE +// SecureTransport.h is missing on iPhone, with its SSL constants: +enum{ + errSSLClosedAbort = -9806, /* connection closed via error */ +}; +#endif + + + NSString* const TCPErrorDomain = @"TCP"; @@ -62,10 +71,20 @@ { NSInputStream *input = nil; NSOutputStream *output = nil; +#if TARGET_OS_IPHONE + // +getStreamsToHost: is missing for some stupid reason on iPhone. Grrrrrrrrrr. + CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)address.hostname, address.port, + (CFReadStreamRef*)&input, (CFWriteStreamRef*)&output); + if( input ) + [(id)CFMakeCollectable(input) autorelease]; + if( output ) + [(id)CFMakeCollectable(output) autorelease]; +#else [NSStream getStreamsToHost: [NSHost hostWithAddress: address.ipv4name] port: address.port inputStream: &input outputStream: &output]; +#endif return [self _initWithAddress: address inputStream: input outputStream: output]; //FIX: Support localPort! } diff -r 5936db2c1987 -r 6f539dd9921c TCP/TCPEndpoint.h --- a/TCP/TCPEndpoint.h Sun May 25 13:43:03 2008 -0700 +++ b/TCP/TCPEndpoint.h Thu May 29 16:40:36 2008 -0700 @@ -7,13 +7,23 @@ // #import +#if TARGET_OS_IPHONE +#include +#else #import +#endif // SSL properties: #define kTCPPropertySSLCertificates ((NSString*)kCFStreamSSLCertificates) #define kTCPPropertySSLAllowsAnyRoot ((NSString*)kCFStreamSSLAllowsAnyRoot) -extern NSString* const kTCPPropertySSLClientSideAuthentication; // value is SSLAuthenticate enum + +extern NSString* const kTCPPropertySSLClientSideAuthentication; // value is TCPAuthenticate enum +typedef enum { + kTCPNeverAuthenticate, /* skip client authentication */ + kTCPAlwaysAuthenticate, /* require it */ + kTCPTryAuthenticate /* try to authenticate, but not error if client has no cert */ +} TCPAuthenticate; // these MUST have same values as SSLAuthenticate enum in SecureTransport.h! /** Abstract base class of TCPConnection and TCPListener. diff -r 5936db2c1987 -r 6f539dd9921c TCP/TCPListener.m --- a/TCP/TCPListener.m Sun May 25 13:43:03 2008 -0700 +++ b/TCP/TCPListener.m Thu May 29 16:40:36 2008 -0700 @@ -129,7 +129,7 @@ if( ! _ipv4socket ) { if( error.code==EADDRINUSE && _pickAvailablePort && _port<0xFFFF ) { LogTo(BLIPVerbose,@"%@: port busy, trying %hu...",self,_port+1); - self.port++; // try the next port + self.port += 1; // try the next port } else { if( outError ) *outError = error; return [self _failedToOpen: error]; diff -r 5936db2c1987 -r 6f539dd9921c maindocs.h --- a/maindocs.h Sun May 25 13:43:03 2008 -0700 +++ b/maindocs.h Thu May 29 16:40:36 2008 -0700 @@ -11,7 +11,9 @@ /*! \mainpage MYNetwork: Mooseyard Networking Library, With BLIP Protocol Implementation
By Jens Alfke
- + + + \section intro_sec Introduction MYNetwork is a set of Objective-C networking classes for Cocoa applications on Mac OS X. @@ -39,6 +41,19 @@ \section blipdesc What's BLIP? + + + + +
+ Google Groups +
+ BLIP Protocol +
+ Visit this group +
+ BLIP is a message-oriented network protocol that lets the two peers on either end of a TCP socket send request and response messages to each other. It's a generic protocol, in that the requests and responses can contain any kind of data you like. BLIP was inspired by The implementation supports SSL connections (with optional client-side certificates), and Bonjour service advertising. - + \section config Configuration MYNetwork requires Mac OS X 10.5 or later, since it uses Objective-C 2 features like