blob: 256ecf3bc849a39829db7c3b16b7a45729bf0dc7 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee77575e62007-09-24 19:24:53 +000019#import "TSocketClient.h"
Jake Farrell9689d892011-12-06 01:07:17 +000020#import "TObjective-C.h"
Andrew McGeachieb80024d2010-04-28 17:43:45 +000021
22#if !TARGET_OS_IPHONE
23#import <CoreServices/CoreServices.h>
24#else
25#import <CFNetwork/CFNetwork.h>
26#endif
Mark Slee77575e62007-09-24 19:24:53 +000027
28@implementation TSocketClient
29
30- (id) initWithHostname: (NSString *) hostname
31 port: (int) port
32{
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000033 NSInputStream * inputStream = NULL;
34 NSOutputStream * outputStream = NULL;
35 CFReadStreamRef readStream = NULL;
36 CFWriteStreamRef writeStream = NULL;
Jake Farrell9689d892011-12-06 01:07:17 +000037 CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (bridge_stub CFStringRef)hostname, port, &readStream, &writeStream);
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000038 if (readStream && writeStream) {
39 CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
40 CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
41
Jake Farrell9689d892011-12-06 01:07:17 +000042 inputStream = (bridge_stub NSInputStream *)readStream;
43 [inputStream retain_stub];
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000044 [inputStream setDelegate:self];
45 [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
46 [inputStream open];
47
Jake Farrell9689d892011-12-06 01:07:17 +000048 outputStream = (bridge_stub NSOutputStream *)writeStream;
49 [outputStream retain_stub];
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000050 [outputStream setDelegate:self];
51 [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
52 [outputStream open];
Jake Farrell9689d892011-12-06 01:07:17 +000053 CFRelease(readStream);
54 CFRelease(writeStream);
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000055 }
56
57 self = [super initWithInputStream: inputStream outputStream: outputStream];
58
59 return self;
Mark Slee77575e62007-09-24 19:24:53 +000060}
61
62
63@end
64
65
66