THRIFT-549. Make socket client compatible with iPhone SDK as well as OS X
authorAndrew McGeachie <geechorama@apache.org>
Fri, 24 Jul 2009 15:58:07 +0000 (15:58 +0000)
committerAndrew McGeachie <geechorama@apache.org>
Fri, 24 Jul 2009 15:58:07 +0000 (15:58 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@797545 13f79535-47bb-0310-9956-ffa450edef68

lib/cocoa/src/transport/TSocketClient.m

index cc50bec..b4183fb 100644 (file)
  * specific language governing permissions and limitations
  * under the License.
  */
-
-#import <Foundation/Foundation.h>
 #import "TSocketClient.h"
+#import <CFNetwork/CFSocketStream.h>
 
 @implementation TSocketClient
 
 - (id) initWithHostname: (NSString *) hostname
                    port: (int) port
 {
-  NSInputStream * input = nil;
-  NSOutputStream * output = nil;
-
-  [NSStream getStreamsToHost: [NSHost hostWithName: hostname]
-            port: port
-            inputStream: &input
-            outputStream: &output];
-
-  self = [super initWithInputStream: input outputStream: output];
-  [input open];
-  [output open];
-
-  return self;
+       NSInputStream * inputStream = NULL;
+       NSOutputStream * outputStream = NULL;
+       CFReadStreamRef readStream = NULL;
+       CFWriteStreamRef writeStream = NULL;
+       CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)hostname, port, &readStream, &writeStream);
+       if (readStream && writeStream) {
+               CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+               CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+               
+               inputStream = (NSInputStream *)readStream;
+               [inputStream retain];
+               [inputStream setDelegate:self];
+               [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+               [inputStream open];
+               
+               outputStream = (NSOutputStream *)writeStream;
+               [outputStream retain];
+               [outputStream setDelegate:self];
+               [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+               [outputStream open];
+       }
+       
+       self = [super initWithInputStream: inputStream outputStream: outputStream];
+       
+       return self;
 }